Ray Milkey
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;
/**
* Annotations JSON codec.
*/
public class AnnotationsCodec extends JsonCodec<Annotations> {
public final class AnnotationsCodec extends JsonCodec<Annotations> {
@Override
public ObjectNode encode(Annotations annotations, CodecContext context) {
......
......@@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Application JSON codec.
*/
public class ApplicationCodec extends JsonCodec<Application> {
public final class ApplicationCodec extends JsonCodec<Application> {
@Override
public ObjectNode encode(Application app, CodecContext context) {
......
......@@ -27,7 +27,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Connection point JSON codec.
*/
public class ConnectPointCodec extends JsonCodec<ConnectPoint> {
public final class ConnectPointCodec extends JsonCodec<ConnectPoint> {
@Override
public ObjectNode encode(ConnectPoint point, CodecContext context) {
......
......@@ -31,7 +31,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Connectivity intent codec.
*/
public class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> {
public final class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> {
@Override
public ObjectNode encode(ConnectivityIntent intent, CodecContext context) {
......
......@@ -36,7 +36,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Constraint JSON codec.
*/
public class ConstraintCodec extends JsonCodec<Constraint> {
public final class ConstraintCodec extends JsonCodec<Constraint> {
/**
* Encodes a latency constraint.
......
......@@ -29,7 +29,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Criterion codec.
*/
public class CriterionCodec extends JsonCodec<Criterion> {
public final class CriterionCodec extends JsonCodec<Criterion> {
protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class);
......
......@@ -25,7 +25,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Device JSON codec.
*/
public class DeviceCodec extends AnnotatedCodec<Device> {
public final class DeviceCodec extends AnnotatedCodec<Device> {
@Override
public ObjectNode encode(Device device, CodecContext context) {
......
......@@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Ethernet codec.
*/
public class EthernetCodec extends JsonCodec<Ethernet> {
public final class EthernetCodec extends JsonCodec<Ethernet> {
protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class);
......
......@@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Flow entry JSON codec.
*/
public class FlowEntryCodec extends JsonCodec<FlowEntry> {
public final class FlowEntryCodec extends JsonCodec<FlowEntry> {
@Override
public ObjectNode encode(FlowEntry flowEntry, CodecContext context) {
......
......@@ -29,7 +29,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Host JSON codec.
*/
public class HostCodec extends AnnotatedCodec<Host> {
public final class HostCodec extends AnnotatedCodec<Host> {
@Override
public ObjectNode encode(Host host, CodecContext context) {
......
......@@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Host JSON codec.
*/
public class HostLocationCodec extends JsonCodec<HostLocation> {
public final class HostLocationCodec extends JsonCodec<HostLocation> {
@Override
public ObjectNode encode(HostLocation hostLocation, CodecContext context) {
......
......@@ -27,7 +27,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Host to host intent codec.
*/
public class HostToHostIntentCodec extends JsonCodec<HostToHostIntent> {
public final class HostToHostIntentCodec extends JsonCodec<HostToHostIntent> {
@Override
public ObjectNode encode(HostToHostIntent intent, CodecContext context) {
......
......@@ -33,7 +33,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Instruction codec.
*/
public class InstructionCodec extends JsonCodec<Instruction> {
public final class InstructionCodec extends JsonCodec<Instruction> {
protected static final Logger log = LoggerFactory.getLogger(InstructionCodec.class);
......
......@@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Intent JSON codec.
*/
public class IntentCodec extends JsonCodec<Intent> {
public final class IntentCodec extends JsonCodec<Intent> {
@Override
public ObjectNode encode(Intent intent, CodecContext context) {
......
......@@ -27,7 +27,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Link JSON codec.
*/
public class LinkCodec extends AnnotatedCodec<Link> {
public final class LinkCodec extends AnnotatedCodec<Link> {
@Override
public ObjectNode encode(Link link, CodecContext context) {
......
......@@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Path JSON codec.
*/
public class PathCodec extends AnnotatedCodec<Path> {
public final class PathCodec extends AnnotatedCodec<Path> {
@Override
public ObjectNode encode(Path path, CodecContext context) {
checkNotNull(path, "Path cannot be null");
......
......@@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Point to point intent codec.
*/
public class PointToPointIntentCodec extends JsonCodec<PointToPointIntent> {
public final class PointToPointIntentCodec extends JsonCodec<PointToPointIntent> {
@Override
public ObjectNode encode(PointToPointIntent intent, CodecContext context) {
......
......@@ -25,7 +25,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Device port JSON codec.
*/
public class PortCodec extends AnnotatedCodec<Port> {
public final class PortCodec extends AnnotatedCodec<Port> {
@Override
public ObjectNode encode(Port port, CodecContext context) {
......
......@@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Topology cluster JSON codec.
*/
public class TopologyClusterCodec extends JsonCodec<TopologyCluster> {
public final class TopologyClusterCodec extends JsonCodec<TopologyCluster> {
@Override
public ObjectNode encode(TopologyCluster cluster, CodecContext context) {
......
......@@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Topology JSON codec.
*/
public class TopologyCodec extends JsonCodec<Topology> {
public final class TopologyCodec extends JsonCodec<Topology> {
@Override
public ObjectNode encode(Topology topology, CodecContext context) {
......
......@@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Traffic selector codec.
*/
public class TrafficSelectorCodec extends JsonCodec<TrafficSelector> {
public final class TrafficSelectorCodec extends JsonCodec<TrafficSelector> {
@Override
public ObjectNode encode(TrafficSelector selector, CodecContext context) {
checkNotNull(selector, "Traffic selector cannot be null");
......
......@@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Traffic treatment codec.
*/
public class TrafficTreatmentCodec extends JsonCodec<TrafficTreatment> {
public final class TrafficTreatmentCodec extends JsonCodec<TrafficTreatment> {
@Override
public ObjectNode encode(TrafficTreatment treatment, CodecContext context) {
checkNotNull(treatment, "Traffic treatment cannot be null");
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.codec.impl;
import org.junit.Test;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
/**
* Tests to assure that the codec classes follow the contract of having
* no local context.
*/
public class ImmutableCodecsTest {
/**
* Checks that the codec classes adhere to the contract that there cannot
* be any local context in a codec.
*/
@Test
public void checkImmutability() {
assertThatClassIsImmutableBaseClass(AnnotatedCodec.class);
assertThatClassIsImmutable(AnnotationsCodec.class);
assertThatClassIsImmutable(ApplicationCodec.class);
assertThatClassIsImmutable(ConnectivityIntentCodec.class);
assertThatClassIsImmutable(ConnectPointCodec.class);
assertThatClassIsImmutable(ConstraintCodec.class);
assertThatClassIsImmutable(CriterionCodec.class);
assertThatClassIsImmutable(DeviceCodec.class);
assertThatClassIsImmutable(EthernetCodec.class);
assertThatClassIsImmutable(FlowEntryCodec.class);
assertThatClassIsImmutable(HostCodec.class);
assertThatClassIsImmutable(HostLocationCodec.class);
assertThatClassIsImmutable(HostToHostIntentCodec.class);
assertThatClassIsImmutable(InstructionCodec.class);
assertThatClassIsImmutable(IntentCodec.class);
assertThatClassIsImmutable(LinkCodec.class);
assertThatClassIsImmutable(PathCodec.class);
assertThatClassIsImmutable(PointToPointIntentCodec.class);
assertThatClassIsImmutable(PortCodec.class);
assertThatClassIsImmutable(TopologyClusterCodec.class);
assertThatClassIsImmutable(TopologyCodec.class);
assertThatClassIsImmutable(TrafficSelectorCodec.class);
assertThatClassIsImmutable(TrafficTreatmentCodec.class);
}
}