Merge remote-tracking branch 'origin/master'
diff --git a/core/api/src/main/java/org/onlab/onos/codec/CodecService.java b/core/api/src/main/java/org/onlab/onos/codec/CodecService.java
new file mode 100644
index 0000000..c6dbda2
--- /dev/null
+++ b/core/api/src/main/java/org/onlab/onos/codec/CodecService.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.onlab.onos.codec;
+
+import java.util.Set;
+
+/**
+ * Service for registering and retrieving JSON codecs for various entities.
+ */
+public interface CodecService {
+
+ /**
+ * Returns the set of classes with currently registered codecs.
+ *
+ * @return set of entity classes
+ */
+ Set<Class<?>> getCodecs();
+
+ /**
+ * Returns the JSON codec for the specified entity class.
+ *
+ * @param entityClass entity class
+ * @return JSON codec; null if no codec available for the class
+ */
+ JsonCodec getCodec(Class<?> entityClass);
+
+ /**
+ * Registers the specified JSON codec for the given entity class.
+ *
+ * @param entityClass entity class
+ * @param codec JSON codec
+ */
+ void registerCodec(Class<?> entityClass, JsonCodec codec);
+
+ /**
+ * Unregisters the JSON codec for the specified entity class.
+ *
+ * @param entityClass entity class
+ */
+ void unregisterCodec(Class<?> entityClass);
+
+}
diff --git a/core/api/src/main/java/org/onlab/onos/codec/JsonCodec.java b/core/api/src/main/java/org/onlab/onos/codec/JsonCodec.java
new file mode 100644
index 0000000..4338cde
--- /dev/null
+++ b/core/api/src/main/java/org/onlab/onos/codec/JsonCodec.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.onlab.onos.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Abstraction of a codec capable for encoding/decoding arbitrary objects to/from JSON.
+ */
+public abstract class JsonCodec<T> {
+
+ /**
+ * Encodes the specified entity into JSON.
+ *
+ * @param entity entity to encode
+ * @param mapper object mapper
+ * @return JSON node
+ * @throws java.lang.UnsupportedOperationException if the codec does not
+ * support encode operations
+ */
+ public abstract ObjectNode encode(T entity, ObjectMapper mapper);
+
+ /**
+ * Decodes the specified entity from JSON.
+ *
+ * @param json JSON to decode
+ * @return decoded entity
+ * @throws java.lang.UnsupportedOperationException if the codec does not
+ * support decode operations
+ */
+ public abstract T decode(ObjectNode json);
+
+ /**
+ * Encodes the collection of the specified entities.
+ *
+ * @param entities collection of entities to encode
+ * @param mapper object mapper
+ * @return JSON array
+ * @throws java.lang.UnsupportedOperationException if the codec does not
+ * support encode operations
+ */
+ public ArrayNode encode(Iterable<T> entities, ObjectMapper mapper) {
+ ArrayNode result = mapper.createArrayNode();
+ for (T entity : entities) {
+ result.add(encode(entity, mapper));
+ }
+ return result;
+ }
+
+ /**
+ * Decodes the specified JSON array into a collection of entities.
+ *
+ * @param json JSON array to decode
+ * @return collection of decoded entities
+ * @throws java.lang.UnsupportedOperationException if the codec does not
+ * support decode operations
+ */
+ public List<T> decode(ArrayNode json) {
+ List<T> result = new ArrayList<>();
+ for (JsonNode node : json) {
+ result.add(decode((ObjectNode) node));
+ }
+ return result;
+ }
+
+}
diff --git a/core/api/src/main/java/org/onlab/onos/codec/package-info.java b/core/api/src/main/java/org/onlab/onos/codec/package-info.java
new file mode 100644
index 0000000..33d6d8c
--- /dev/null
+++ b/core/api/src/main/java/org/onlab/onos/codec/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+/**
+ * Base JSON codec abstraction and a service for tracking various JSON codecs.
+ */
+package org.onlab.onos.codec;
\ No newline at end of file
diff --git a/core/api/src/test/java/org/onlab/onos/codec/JsonCodecTest.java b/core/api/src/test/java/org/onlab/onos/codec/JsonCodecTest.java
new file mode 100644
index 0000000..f3a10e3
--- /dev/null
+++ b/core/api/src/test/java/org/onlab/onos/codec/JsonCodecTest.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.onlab.onos.codec;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.Objects;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Test of the base JSON codec abstraction.
+ */
+public class JsonCodecTest {
+
+ private static class Foo {
+ final String name;
+
+ Foo(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ final Foo other = (Foo) obj;
+ return Objects.equals(this.name, other.name);
+ }
+ }
+
+ private static class FooCodec extends JsonCodec<Foo> {
+ @Override
+ public ObjectNode encode(Foo entity, ObjectMapper mapper) {
+ return mapper.createObjectNode().put("name", entity.name);
+ }
+
+ @Override
+ public Foo decode(ObjectNode json) {
+ return new Foo(json.get("name").asText());
+ }
+ }
+
+ @Test
+ public void encode() {
+ Foo f1 = new Foo("foo");
+ Foo f2 = new Foo("bar");
+ FooCodec codec = new FooCodec();
+ ImmutableList<Foo> entities = ImmutableList.of(f1, f2);
+ ArrayNode json = codec.encode(entities, new ObjectMapper());
+ List<Foo> foos = codec.decode(json);
+ assertEquals("incorrect encode/decode", entities, foos);
+ }
+
+}
\ No newline at end of file