Committed by
Gerrit Code Review
Make JSON codecs immutable
The contract for JSON codecs requires that they have no local context. Make them all immutable, and test that they stay immutable. Change-Id: Ia3930b844ede231940da05699f5f08605f946e0c
Showing
23 changed files
with
80 additions
and
22 deletions
... | @@ -23,7 +23,7 @@ import org.onosproject.net.Annotations; | ... | @@ -23,7 +23,7 @@ import org.onosproject.net.Annotations; |
23 | /** | 23 | /** |
24 | * Annotations JSON codec. | 24 | * Annotations JSON codec. |
25 | */ | 25 | */ |
26 | -public class AnnotationsCodec extends JsonCodec<Annotations> { | 26 | +public final class AnnotationsCodec extends JsonCodec<Annotations> { |
27 | 27 | ||
28 | @Override | 28 | @Override |
29 | public ObjectNode encode(Annotations annotations, CodecContext context) { | 29 | public ObjectNode encode(Annotations annotations, CodecContext context) { | ... | ... |
... | @@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
26 | /** | 26 | /** |
27 | * Application JSON codec. | 27 | * Application JSON codec. |
28 | */ | 28 | */ |
29 | -public class ApplicationCodec extends JsonCodec<Application> { | 29 | +public final class ApplicationCodec extends JsonCodec<Application> { |
30 | 30 | ||
31 | @Override | 31 | @Override |
32 | public ObjectNode encode(Application app, CodecContext context) { | 32 | public ObjectNode encode(Application app, CodecContext context) { | ... | ... |
... | @@ -27,7 +27,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -27,7 +27,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
27 | /** | 27 | /** |
28 | * Connection point JSON codec. | 28 | * Connection point JSON codec. |
29 | */ | 29 | */ |
30 | -public class ConnectPointCodec extends JsonCodec<ConnectPoint> { | 30 | +public final class ConnectPointCodec extends JsonCodec<ConnectPoint> { |
31 | 31 | ||
32 | @Override | 32 | @Override |
33 | public ObjectNode encode(ConnectPoint point, CodecContext context) { | 33 | public ObjectNode encode(ConnectPoint point, CodecContext context) { | ... | ... |
... | @@ -31,7 +31,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -31,7 +31,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
31 | /** | 31 | /** |
32 | * Connectivity intent codec. | 32 | * Connectivity intent codec. |
33 | */ | 33 | */ |
34 | -public class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> { | 34 | +public final class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> { |
35 | 35 | ||
36 | @Override | 36 | @Override |
37 | public ObjectNode encode(ConnectivityIntent intent, CodecContext context) { | 37 | public ObjectNode encode(ConnectivityIntent intent, CodecContext context) { | ... | ... |
... | @@ -36,7 +36,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -36,7 +36,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
36 | /** | 36 | /** |
37 | * Constraint JSON codec. | 37 | * Constraint JSON codec. |
38 | */ | 38 | */ |
39 | -public class ConstraintCodec extends JsonCodec<Constraint> { | 39 | +public final class ConstraintCodec extends JsonCodec<Constraint> { |
40 | 40 | ||
41 | /** | 41 | /** |
42 | * Encodes a latency constraint. | 42 | * Encodes a latency constraint. | ... | ... |
... | @@ -29,7 +29,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -29,7 +29,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
29 | /** | 29 | /** |
30 | * Criterion codec. | 30 | * Criterion codec. |
31 | */ | 31 | */ |
32 | -public class CriterionCodec extends JsonCodec<Criterion> { | 32 | +public final class CriterionCodec extends JsonCodec<Criterion> { |
33 | 33 | ||
34 | protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class); | 34 | protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class); |
35 | 35 | ... | ... |
... | @@ -25,7 +25,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -25,7 +25,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
25 | /** | 25 | /** |
26 | * Device JSON codec. | 26 | * Device JSON codec. |
27 | */ | 27 | */ |
28 | -public class DeviceCodec extends AnnotatedCodec<Device> { | 28 | +public final class DeviceCodec extends AnnotatedCodec<Device> { |
29 | 29 | ||
30 | @Override | 30 | @Override |
31 | public ObjectNode encode(Device device, CodecContext context) { | 31 | public ObjectNode encode(Device device, CodecContext context) { | ... | ... |
... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
28 | /** | 28 | /** |
29 | * Ethernet codec. | 29 | * Ethernet codec. |
30 | */ | 30 | */ |
31 | -public class EthernetCodec extends JsonCodec<Ethernet> { | 31 | +public final class EthernetCodec extends JsonCodec<Ethernet> { |
32 | 32 | ||
33 | protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class); | 33 | protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class); |
34 | 34 | ... | ... |
... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
28 | /** | 28 | /** |
29 | * Flow entry JSON codec. | 29 | * Flow entry JSON codec. |
30 | */ | 30 | */ |
31 | -public class FlowEntryCodec extends JsonCodec<FlowEntry> { | 31 | +public final class FlowEntryCodec extends JsonCodec<FlowEntry> { |
32 | 32 | ||
33 | @Override | 33 | @Override |
34 | public ObjectNode encode(FlowEntry flowEntry, CodecContext context) { | 34 | public ObjectNode encode(FlowEntry flowEntry, CodecContext context) { | ... | ... |
... | @@ -29,7 +29,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -29,7 +29,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
29 | /** | 29 | /** |
30 | * Host JSON codec. | 30 | * Host JSON codec. |
31 | */ | 31 | */ |
32 | -public class HostCodec extends AnnotatedCodec<Host> { | 32 | +public final class HostCodec extends AnnotatedCodec<Host> { |
33 | 33 | ||
34 | @Override | 34 | @Override |
35 | public ObjectNode encode(Host host, CodecContext context) { | 35 | public ObjectNode encode(Host host, CodecContext context) { | ... | ... |
... | @@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
26 | /** | 26 | /** |
27 | * Host JSON codec. | 27 | * Host JSON codec. |
28 | */ | 28 | */ |
29 | -public class HostLocationCodec extends JsonCodec<HostLocation> { | 29 | +public final class HostLocationCodec extends JsonCodec<HostLocation> { |
30 | 30 | ||
31 | @Override | 31 | @Override |
32 | public ObjectNode encode(HostLocation hostLocation, CodecContext context) { | 32 | public ObjectNode encode(HostLocation hostLocation, CodecContext context) { | ... | ... |
... | @@ -27,7 +27,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -27,7 +27,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
27 | /** | 27 | /** |
28 | * Host to host intent codec. | 28 | * Host to host intent codec. |
29 | */ | 29 | */ |
30 | -public class HostToHostIntentCodec extends JsonCodec<HostToHostIntent> { | 30 | +public final class HostToHostIntentCodec extends JsonCodec<HostToHostIntent> { |
31 | 31 | ||
32 | @Override | 32 | @Override |
33 | public ObjectNode encode(HostToHostIntent intent, CodecContext context) { | 33 | public ObjectNode encode(HostToHostIntent intent, CodecContext context) { | ... | ... |
... | @@ -33,7 +33,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -33,7 +33,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
33 | /** | 33 | /** |
34 | * Instruction codec. | 34 | * Instruction codec. |
35 | */ | 35 | */ |
36 | -public class InstructionCodec extends JsonCodec<Instruction> { | 36 | +public final class InstructionCodec extends JsonCodec<Instruction> { |
37 | 37 | ||
38 | protected static final Logger log = LoggerFactory.getLogger(InstructionCodec.class); | 38 | protected static final Logger log = LoggerFactory.getLogger(InstructionCodec.class); |
39 | 39 | ... | ... |
... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
28 | /** | 28 | /** |
29 | * Intent JSON codec. | 29 | * Intent JSON codec. |
30 | */ | 30 | */ |
31 | -public class IntentCodec extends JsonCodec<Intent> { | 31 | +public final class IntentCodec extends JsonCodec<Intent> { |
32 | 32 | ||
33 | @Override | 33 | @Override |
34 | public ObjectNode encode(Intent intent, CodecContext context) { | 34 | public ObjectNode encode(Intent intent, CodecContext context) { | ... | ... |
... | @@ -27,7 +27,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -27,7 +27,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
27 | /** | 27 | /** |
28 | * Link JSON codec. | 28 | * Link JSON codec. |
29 | */ | 29 | */ |
30 | -public class LinkCodec extends AnnotatedCodec<Link> { | 30 | +public final class LinkCodec extends AnnotatedCodec<Link> { |
31 | 31 | ||
32 | @Override | 32 | @Override |
33 | public ObjectNode encode(Link link, CodecContext context) { | 33 | public ObjectNode encode(Link link, CodecContext context) { | ... | ... |
... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
28 | /** | 28 | /** |
29 | * Path JSON codec. | 29 | * Path JSON codec. |
30 | */ | 30 | */ |
31 | -public class PathCodec extends AnnotatedCodec<Path> { | 31 | +public final class PathCodec extends AnnotatedCodec<Path> { |
32 | @Override | 32 | @Override |
33 | public ObjectNode encode(Path path, CodecContext context) { | 33 | public ObjectNode encode(Path path, CodecContext context) { |
34 | checkNotNull(path, "Path cannot be null"); | 34 | checkNotNull(path, "Path cannot be null"); | ... | ... |
... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
28 | /** | 28 | /** |
29 | * Point to point intent codec. | 29 | * Point to point intent codec. |
30 | */ | 30 | */ |
31 | -public class PointToPointIntentCodec extends JsonCodec<PointToPointIntent> { | 31 | +public final class PointToPointIntentCodec extends JsonCodec<PointToPointIntent> { |
32 | 32 | ||
33 | @Override | 33 | @Override |
34 | public ObjectNode encode(PointToPointIntent intent, CodecContext context) { | 34 | public ObjectNode encode(PointToPointIntent intent, CodecContext context) { | ... | ... |
... | @@ -25,7 +25,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -25,7 +25,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
25 | /** | 25 | /** |
26 | * Device port JSON codec. | 26 | * Device port JSON codec. |
27 | */ | 27 | */ |
28 | -public class PortCodec extends AnnotatedCodec<Port> { | 28 | +public final class PortCodec extends AnnotatedCodec<Port> { |
29 | 29 | ||
30 | @Override | 30 | @Override |
31 | public ObjectNode encode(Port port, CodecContext context) { | 31 | public ObjectNode encode(Port port, CodecContext context) { | ... | ... |
... | @@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
26 | /** | 26 | /** |
27 | * Topology cluster JSON codec. | 27 | * Topology cluster JSON codec. |
28 | */ | 28 | */ |
29 | -public class TopologyClusterCodec extends JsonCodec<TopologyCluster> { | 29 | +public final class TopologyClusterCodec extends JsonCodec<TopologyCluster> { |
30 | 30 | ||
31 | @Override | 31 | @Override |
32 | public ObjectNode encode(TopologyCluster cluster, CodecContext context) { | 32 | public ObjectNode encode(TopologyCluster cluster, CodecContext context) { | ... | ... |
... | @@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
26 | /** | 26 | /** |
27 | * Topology JSON codec. | 27 | * Topology JSON codec. |
28 | */ | 28 | */ |
29 | -public class TopologyCodec extends JsonCodec<Topology> { | 29 | +public final class TopologyCodec extends JsonCodec<Topology> { |
30 | 30 | ||
31 | @Override | 31 | @Override |
32 | public ObjectNode encode(Topology topology, CodecContext context) { | 32 | public ObjectNode encode(Topology topology, CodecContext context) { | ... | ... |
... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
28 | /** | 28 | /** |
29 | * Traffic selector codec. | 29 | * Traffic selector codec. |
30 | */ | 30 | */ |
31 | -public class TrafficSelectorCodec extends JsonCodec<TrafficSelector> { | 31 | +public final class TrafficSelectorCodec extends JsonCodec<TrafficSelector> { |
32 | @Override | 32 | @Override |
33 | public ObjectNode encode(TrafficSelector selector, CodecContext context) { | 33 | public ObjectNode encode(TrafficSelector selector, CodecContext context) { |
34 | checkNotNull(selector, "Traffic selector cannot be null"); | 34 | checkNotNull(selector, "Traffic selector cannot be null"); | ... | ... |
... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull; |
28 | /** | 28 | /** |
29 | * Traffic treatment codec. | 29 | * Traffic treatment codec. |
30 | */ | 30 | */ |
31 | -public class TrafficTreatmentCodec extends JsonCodec<TrafficTreatment> { | 31 | +public final class TrafficTreatmentCodec extends JsonCodec<TrafficTreatment> { |
32 | @Override | 32 | @Override |
33 | public ObjectNode encode(TrafficTreatment treatment, CodecContext context) { | 33 | public ObjectNode encode(TrafficTreatment treatment, CodecContext context) { |
34 | checkNotNull(treatment, "Traffic treatment cannot be null"); | 34 | checkNotNull(treatment, "Traffic treatment cannot be null"); | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.codec.impl; | ||
17 | + | ||
18 | +import org.junit.Test; | ||
19 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; | ||
20 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; | ||
21 | + | ||
22 | +/** | ||
23 | + * Tests to assure that the codec classes follow the contract of having | ||
24 | + * no local context. | ||
25 | + */ | ||
26 | +public class ImmutableCodecsTest { | ||
27 | + | ||
28 | + /** | ||
29 | + * Checks that the codec classes adhere to the contract that there cannot | ||
30 | + * be any local context in a codec. | ||
31 | + */ | ||
32 | + @Test | ||
33 | + public void checkImmutability() { | ||
34 | + assertThatClassIsImmutableBaseClass(AnnotatedCodec.class); | ||
35 | + assertThatClassIsImmutable(AnnotationsCodec.class); | ||
36 | + assertThatClassIsImmutable(ApplicationCodec.class); | ||
37 | + assertThatClassIsImmutable(ConnectivityIntentCodec.class); | ||
38 | + assertThatClassIsImmutable(ConnectPointCodec.class); | ||
39 | + assertThatClassIsImmutable(ConstraintCodec.class); | ||
40 | + assertThatClassIsImmutable(CriterionCodec.class); | ||
41 | + assertThatClassIsImmutable(DeviceCodec.class); | ||
42 | + assertThatClassIsImmutable(EthernetCodec.class); | ||
43 | + assertThatClassIsImmutable(FlowEntryCodec.class); | ||
44 | + assertThatClassIsImmutable(HostCodec.class); | ||
45 | + assertThatClassIsImmutable(HostLocationCodec.class); | ||
46 | + assertThatClassIsImmutable(HostToHostIntentCodec.class); | ||
47 | + assertThatClassIsImmutable(InstructionCodec.class); | ||
48 | + assertThatClassIsImmutable(IntentCodec.class); | ||
49 | + assertThatClassIsImmutable(LinkCodec.class); | ||
50 | + assertThatClassIsImmutable(PathCodec.class); | ||
51 | + assertThatClassIsImmutable(PointToPointIntentCodec.class); | ||
52 | + assertThatClassIsImmutable(PortCodec.class); | ||
53 | + assertThatClassIsImmutable(TopologyClusterCodec.class); | ||
54 | + assertThatClassIsImmutable(TopologyCodec.class); | ||
55 | + assertThatClassIsImmutable(TrafficSelectorCodec.class); | ||
56 | + assertThatClassIsImmutable(TrafficTreatmentCodec.class); | ||
57 | + } | ||
58 | +} |
-
Please register or login to post a comment