[ONOS-5884]YANG Serializer: Implement XML serializer.
Change-Id: Idffda88938d9c6c7b7c7814127a340bd2dc35039
diff --git a/serializers/json/pom.xml b/serializers/json/pom.xml
index 7a9fc74..e512f8a 100644
--- a/serializers/json/pom.xml
+++ b/serializers/json/pom.xml
@@ -60,6 +60,11 @@
<artifactId>jackson-annotations</artifactId>
<version>2.8.6</version>
</dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-yang-serializers-utils</artifactId>
+ <version>1.12-SNAPSHOT</version>
+ </dependency>
<!-- TODO: remove
<dependency>
<groupId>org.onosproject</groupId>
diff --git a/serializers/json/src/main/java/org/onosproject/yang/serializers/json/DecoderUtils.java b/serializers/json/src/main/java/org/onosproject/yang/serializers/json/DecoderUtils.java
index 753b699..1e91cd7 100644
--- a/serializers/json/src/main/java/org/onosproject/yang/serializers/json/DecoderUtils.java
+++ b/serializers/json/src/main/java/org/onosproject/yang/serializers/json/DecoderUtils.java
@@ -17,66 +17,22 @@
package org.onosproject.yang.serializers.json;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.google.common.base.Splitter;
-import com.google.common.collect.Lists;
import org.onosproject.yang.model.DataNode;
import org.onosproject.yang.model.ResourceId;
import org.onosproject.yang.runtime.YangSerializerContext;
import org.onosproject.yang.runtime.helperutils.SerializerHelper;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
-import java.util.ArrayList;
-import java.util.List;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
/**
* Utilities for parsing URI and JSON strings.
*/
public final class DecoderUtils {
- private static final Splitter SLASH_SPLITTER = Splitter.on('/');
- private static final Splitter COMMA_SPLITTER = Splitter.on(',');
- private static final String EQUAL = "=";
- private static final String COMMA = ",";
- private static final String COLON = ":";
- private static final String SLASH = "/";
- private static final String URI_ENCODING_CHAR_SET = "ISO-8859-1";
- private static final String ERROR_LIST_MSG = "List/Leaf-list node should be " +
- "in format \"nodeName=key\"or \"nodeName=instance-value\"";
-
// no instantiation
private DecoderUtils() {
}
/**
- * Converts a URI string to resource identifier.
- *
- * @param uriString given URI
- * @param context YANG schema context information
- * @return resource ID
- */
- public static ResourceId convertUriToRid(String uriString,
- YangSerializerContext context) {
- if (uriString == null || uriString.isEmpty()) {
- return null;
- }
-
- List<String> paths = urlPathArgsDecode(SLASH_SPLITTER.split(uriString));
-
- if (!paths.isEmpty()) {
- ResourceId.Builder ridBuilder =
- SerializerHelper.initializeResourceId(context);
- processPathSegments(paths, ridBuilder);
- return ridBuilder.build();
- }
-
- return null;
- }
-
- /**
* Converts JSON data to a data node. This method should be used when
* the URI corresponding to the JSON body is null (Thus the caller can
* only provide a serializer context rather than a resource ID).
@@ -125,157 +81,4 @@
return dataNodeBuilder.build();
}
-
- /**
- * Converts a list of path from the original format to ISO-8859-1 code.
- *
- * @param paths the original paths
- * @return list of decoded paths
- */
- public static List<String> urlPathArgsDecode(Iterable<String> paths) {
- try {
- List<String> decodedPathArgs = new ArrayList<>();
- for (String pathArg : paths) {
- String decode = URLDecoder.decode(pathArg,
- URI_ENCODING_CHAR_SET);
- decodedPathArgs.add(decode);
- }
- return decodedPathArgs;
- } catch (UnsupportedEncodingException e) {
- throw new SerializerException("Invalid URL path arg '" +
- paths + "': ", e);
- }
- }
-
- private static ResourceId.Builder processPathSegments(List<String> paths,
- ResourceId.Builder builder) {
- if (paths.isEmpty()) {
- return builder;
- }
-
- boolean isLastSegment = paths.size() == 1;
-
- String segment = paths.iterator().next();
- processSinglePathSegment(segment, builder);
-
- if (isLastSegment) {
- // We have hit the base case of recursion.
- return builder;
- }
-
- /*
- * Chop off the first segment, and recursively process the rest
- * of the path segments.
- */
- List<String> remainPaths = paths.subList(1, paths.size());
- processPathSegments(remainPaths, builder);
-
- return builder;
- }
-
- private static void processSinglePathSegment(String pathSegment,
- ResourceId.Builder builder) {
- if (pathSegment.contains(COLON)) {
- processPathSegmentWithNamespace(pathSegment, builder);
- } else {
- processPathSegmentWithoutNamespace(pathSegment, builder);
- }
- }
-
- private static void processPathSegmentWithNamespace(String pathSegment,
- ResourceId.Builder builder) {
-
- String nodeName = getLatterSegment(pathSegment, COLON);
- String namespace = getPreSegment(pathSegment, COLON);
- addNodeNameToRid(nodeName, namespace, builder);
- }
-
- private static void processPathSegmentWithoutNamespace(String nodeName,
- ResourceId.Builder builder) {
- addNodeNameToRid(nodeName, null, builder);
- }
-
- private static void addNodeNameToRid(String nodeName,
- String namespace,
- ResourceId.Builder builder) {
- if (nodeName.contains(EQUAL)) {
- addListOrLeafList(nodeName, namespace, builder);
- } else {
- addLeaf(nodeName, namespace, builder);
- }
- }
-
- private static void addListOrLeafList(String path,
- String namespace,
- ResourceId.Builder builder) {
- String nodeName = getPreSegment(path, EQUAL);
- String keyStr = getLatterSegment(path, EQUAL);
- if (keyStr == null) {
- throw new SerializerException(ERROR_LIST_MSG);
- }
-
- if (keyStr.contains(COMMA)) {
- List<String> keys = Lists.
- newArrayList(COMMA_SPLITTER.split(keyStr));
- SerializerHelper.addToResourceId(builder, nodeName, namespace, keys);
- } else {
- SerializerHelper.addToResourceId(builder, nodeName, namespace,
- Lists.newArrayList(keyStr));
- }
- }
-
- private static void addLeaf(String nodeName,
- String namespace,
- ResourceId.Builder builder) {
- checkNotNull(nodeName);
- SerializerHelper.addToResourceId(builder, nodeName, namespace, "");
- }
-
- /**
- * Returns the previous segment of a path which is separated by a split char.
- * For example:
- * <pre>
- * "foo:bar", ":" --> "foo"
- * </pre>
- *
- * @param path the original path string
- * @param splitChar char used to split the path
- * @return the previous segment of the path
- */
- private static String getPreSegment(String path, String splitChar) {
- int idx = path.indexOf(splitChar);
- if (idx == -1) {
- return null;
- }
-
- if (path.indexOf(splitChar, idx + 1) != -1) {
- return null;
- }
-
- return path.substring(0, idx);
- }
-
- /**
- * Returns the latter segment of a path which is separated by a split char.
- * For example:
- * <pre>
- * "foo:bar", ":" --> "bar"
- * </pre>
- *
- * @param path the original path string
- * @param splitChar char used to split the path
- * @return the latter segment of the path
- */
- private static String getLatterSegment(String path, String splitChar) {
- int idx = path.indexOf(splitChar);
- if (idx == -1) {
- return path;
- }
-
- if (path.indexOf(splitChar, idx + 1) != -1) {
- return null;
- }
-
- return path.substring(idx + 1);
- }
}
diff --git a/serializers/json/src/main/java/org/onosproject/yang/serializers/json/EncoderUtils.java b/serializers/json/src/main/java/org/onosproject/yang/serializers/json/EncoderUtils.java
index 00022e5..c0e7ccc 100644
--- a/serializers/json/src/main/java/org/onosproject/yang/serializers/json/EncoderUtils.java
+++ b/serializers/json/src/main/java/org/onosproject/yang/serializers/json/EncoderUtils.java
@@ -17,17 +17,11 @@
package org.onosproject.yang.serializers.json;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.google.common.base.Splitter;
import org.onosproject.yang.model.DataNode;
import org.onosproject.yang.model.InnerNode;
-import org.onosproject.yang.model.KeyLeaf;
-import org.onosproject.yang.model.LeafListKey;
-import org.onosproject.yang.model.ListKey;
import org.onosproject.yang.model.NodeKey;
-import org.onosproject.yang.model.ResourceId;
import java.util.Iterator;
-import java.util.List;
import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
@@ -45,98 +39,12 @@
* Utilities for converting Data Nodes into JSON format.
*/
public final class EncoderUtils {
- private static final Splitter SLASH_SPLITTER = Splitter.on('/');
- private static final Splitter COMMA_SPLITTER = Splitter.on(',');
- private static final String EQUAL = "=";
- private static final String COMMA = ",";
- private static final String COLON = ":";
- private static final String SLASH = "/";
// no instantiation
private EncoderUtils() {
}
/**
- * Converts a resource identifier to URI string.
- *
- * @param rid resource identifier
- * @return URI
- */
- public static String convertRidToUri(ResourceId rid) {
- if (rid == null) {
- return null;
- }
-
- StringBuilder uriBuilder = new StringBuilder();
- List<NodeKey> nodeKeyList = rid.nodeKeys();
- String curNameSpace = null;
- for (NodeKey key : nodeKeyList) {
- curNameSpace = addNodeKeyToUri(key, curNameSpace, uriBuilder);
- }
- return uriBuilder.toString();
- }
-
- private static String addNodeKeyToUri(NodeKey key,
- String curNameSpace,
- StringBuilder uriBuilder) {
- String newNameSpace;
- if (key instanceof LeafListKey) {
- newNameSpace = addLeafListNodeToUri((LeafListKey) key, curNameSpace, uriBuilder);
- } else if (key instanceof ListKey) {
- newNameSpace = addListNodeToUri((ListKey) key, curNameSpace, uriBuilder);
- } else {
- uriBuilder.append(SLASH);
- newNameSpace = addNodeNameToUri(key, curNameSpace, uriBuilder);
- }
- return newNameSpace;
- }
-
- private static String addLeafListNodeToUri(LeafListKey key,
- String curNameSpace,
- StringBuilder uriBuilder) {
-
- String newNameSpace = addNodeNameToUri(key, curNameSpace, uriBuilder);
- uriBuilder.append(EQUAL);
- uriBuilder.append(key.asString());
- return newNameSpace;
- }
-
- private static String addListNodeToUri(ListKey key,
- String curNameSpace,
- StringBuilder uriBuilder) {
- String newNameSpace = addNodeNameToUri(key, curNameSpace, uriBuilder);
- uriBuilder.append(EQUAL);
- String prefix = "";
- for (KeyLeaf keyLeaf : key.keyLeafs()) {
- uriBuilder.append(prefix);
- prefix = COMMA;
- uriBuilder.append(keyLeaf.leafValue().toString());
- }
-
- return newNameSpace;
- }
-
- private static String addNodeNameToUri(NodeKey key,
- String curNameSpace,
- StringBuilder uriBuilder) {
- String nodeName = key.schemaId().name();
- String newNameSpace = key.schemaId().namespace();
-
- uriBuilder.append(nodeName);
-
- if (newNameSpace == null) {
- return curNameSpace;
- }
-
- if (!newNameSpace.equals(curNameSpace)) {
- uriBuilder.append(COLON);
- uriBuilder.append(newNameSpace);
- }
-
- return newNameSpace;
- }
-
- /**
* Converts a data node to JSON data.
*
* @param dataNode given data node
diff --git a/serializers/json/src/main/java/org/onosproject/yang/serializers/json/JsonSerializer.java b/serializers/json/src/main/java/org/onosproject/yang/serializers/json/JsonSerializer.java
index f635a50..40608b7 100644
--- a/serializers/json/src/main/java/org/onosproject/yang/serializers/json/JsonSerializer.java
+++ b/serializers/json/src/main/java/org/onosproject/yang/serializers/json/JsonSerializer.java
@@ -37,9 +37,9 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.yang.serializers.json.DecoderUtils.convertJsonToDataNode;
-import static org.onosproject.yang.serializers.json.DecoderUtils.convertUriToRid;
import static org.onosproject.yang.serializers.json.EncoderUtils.convertDataNodeToJson;
-import static org.onosproject.yang.serializers.json.EncoderUtils.convertRidToUri;
+import static org.onosproject.yang.serializers.utils.SerializersUtil.convertRidToUri;
+import static org.onosproject.yang.serializers.utils.SerializersUtil.convertUriToRid;
import static org.slf4j.LoggerFactory.getLogger;
/**
@@ -64,8 +64,8 @@
public CompositeData decode(CompositeStream compositeStream,
YangSerializerContext yangSerializerContext) {
try {
- ResourceId rid = convertUriToRid(compositeStream.resourceId(),
- yangSerializerContext);
+ ResourceId.Builder rIdBuilder = convertUriToRid(
+ compositeStream.resourceId(), yangSerializerContext);
ObjectNode rootNode = null;
@@ -83,9 +83,9 @@
* and in this case the resourceId builder which was constructed
* for a URL, needs to be given as an Input parameter.
*/
- if (rid != null) {
+ if (rIdBuilder != null) {
dataNode = convertJsonToDataNode(rootNode,
- new ResourceId.Builder(rid));
+ rIdBuilder);
} else {
dataNode = convertJsonToDataNode(rootNode,
@@ -93,13 +93,8 @@
}
ResourceData resourceData = DefaultResourceData.builder().
- addDataNode(dataNode).resourceId(rid).build();
+ addDataNode(dataNode).resourceId(rIdBuilder.build()).build();
return DefaultCompositeData.builder().resourceData(resourceData).build();
- } catch (CloneNotSupportedException e) {
- log.error("ERROR: JsonProcessingException {}",
- e.getMessage());
- log.debug("Exception in decode:", e);
- throw new SerializerException("JSON serializer decode failure");
} catch (JsonProcessingException e) {
log.error("ERROR: JsonProcessingException {}",
e.getMessage());