[ONOS-5793] YANG runtime API fixes

Change-Id: Id2f9ac337b0ffd7cbb250514c52de3397747f5a6
diff --git a/compiler/plugin/utils/src/main/java/org/onosproject/yang/compiler/plugin/utils/YangApacheUtils.java b/compiler/plugin/utils/src/main/java/org/onosproject/yang/compiler/plugin/utils/YangApacheUtils.java
index 24797b8..1f91d96 100644
--- a/compiler/plugin/utils/src/main/java/org/onosproject/yang/compiler/plugin/utils/YangApacheUtils.java
+++ b/compiler/plugin/utils/src/main/java/org/onosproject/yang/compiler/plugin/utils/YangApacheUtils.java
@@ -92,11 +92,12 @@
     /**
      * Returns YANG model for application.
      *
-     * @param path path for metadata file
+     * @param path     path for metadata file
+     * @param curNodes curNodes YANG nodes
      * @return YANG model
      */
-    public static YangModel processYangModel(String path, List<YangNode>
-            curNodes) {
+    public static YangModel processYangModel(String path,
+                                             List<YangNode> curNodes) {
         DefaultYangModel model = new DefaultYangModel();
         YangModuleId id;
         for (YangSchemaNode node : curNodes) {
diff --git a/model/src/main/java/org/onosproject/yang/model/DataNode.java b/model/src/main/java/org/onosproject/yang/model/DataNode.java
index cb56d51..1727195 100644
--- a/model/src/main/java/org/onosproject/yang/model/DataNode.java
+++ b/model/src/main/java/org/onosproject/yang/model/DataNode.java
@@ -48,6 +48,7 @@
          */
         MULTI_INSTANCE_LEAF_VALUE_NODE
     }
+
     /**
      * Type of node in data store.
      */
@@ -123,7 +124,7 @@
         protected Object AppInfo;
 
         /**
-         * Parent data node
+         * Parent data node.
          */
         protected InnerNode.Builder parent;
 
diff --git a/model/src/main/java/org/onosproject/yang/model/LeafListKey.java b/model/src/main/java/org/onosproject/yang/model/LeafListKey.java
index b7d082c..1758d2e 100644
--- a/model/src/main/java/org/onosproject/yang/model/LeafListKey.java
+++ b/model/src/main/java/org/onosproject/yang/model/LeafListKey.java
@@ -113,6 +113,7 @@
          * Adds the value for for the leaf list node identifier.
          *
          * @param val leaf list value
+         * @return builder
          */
         LeafListKeyBuilder value(Object val) {
             this.val = val;
diff --git a/model/src/main/java/org/onosproject/yang/model/ResourceId.java b/model/src/main/java/org/onosproject/yang/model/ResourceId.java
index 98999ee..d81911d 100644
--- a/model/src/main/java/org/onosproject/yang/model/ResourceId.java
+++ b/model/src/main/java/org/onosproject/yang/model/ResourceId.java
@@ -73,6 +73,7 @@
      * provide mutability of resource identifier using builder pattern.
      *
      * @return data node builder
+     * @throws CloneNotSupportedException when clone is not supported
      */
     public Builder copyBuilder() throws CloneNotSupportedException {
         return new Builder(this);
@@ -128,6 +129,7 @@
          * identifier.
          *
          * @param id old resource identifier
+         * @throws CloneNotSupportedException when clone is not supported
          */
         public Builder(ResourceId id) throws CloneNotSupportedException {
             nodeKeyList = new LinkedList<>();
@@ -141,6 +143,7 @@
          *
          * @param id resource identifier to be appended
          * @return builder
+         * @throws CloneNotSupportedException when clone is not supported
          */
         public Builder append(ResourceId id) throws CloneNotSupportedException {
             processCurKey();
diff --git a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/AnnotatedNodeInfo.java b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/AnnotatedNodeInfo.java
index 2ed3c4d..d897cb9 100644
--- a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/AnnotatedNodeInfo.java
+++ b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/AnnotatedNodeInfo.java
@@ -21,7 +21,7 @@
 import java.util.List;
 
 /**
- * Representation of annotated nodes information.
+ * Abstraction of an entity that represents annotated nodes information.
  */
 public interface AnnotatedNodeInfo {
 
@@ -33,13 +33,6 @@
     ResourceId resourceId();
 
     /**
-     * Sets resource identifier of the annotated node.
-     *
-     * @param id resource identifier
-     */
-    void resourceId(ResourceId id);
-
-    /**
      * Returns annotations associated with the node.
      *
      * @return annotations
@@ -47,10 +40,34 @@
     List<Annotation> annotations();
 
     /**
-     * Adds annotation to a node.
-     *
-     * @param annotation annotation information
+     * Abstraction of an entity that represents builder of annotated node
+     * information.
      */
-    void addAnnotation(Annotation annotation);
+    interface Builder {
+
+
+        /**
+         * Sets resource identifier of the annotated node.
+         *
+         * @param id resource identifier
+         * @return builder
+         */
+        Builder resourceId(ResourceId id);
+
+        /**
+         * Adds annotation to a node.
+         *
+         * @param annotation annotation information
+         * @return builder
+         */
+        Builder addAnnotation(Annotation annotation);
+
+        /**
+         * Builds an instance of annotated node information.
+         *
+         * @return annotated node info
+         */
+        AnnotatedNodeInfo build();
+    }
 }
 
diff --git a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/Annotation.java b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/Annotation.java
index 317e5e7..e2db85d 100644
--- a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/Annotation.java
+++ b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/Annotation.java
@@ -17,7 +17,7 @@
 package org.onosproject.yang.runtime.api;
 
 /**
- * Representation of annotated attribute.
+ * Abstraction of an entity that represents annotated attribute.
  */
 public interface Annotation {
 
@@ -29,23 +29,9 @@
     String name();
 
     /**
-     * Sets name of the annotated attribute.
-     *
-     * @param name of the attribute
-     */
-    void name(String name);
-
-    /**
      * Returns value of the annotation.
      *
      * @return annotation value
      */
     String value();
-
-    /**
-     * Sets value of the annotation.
-     *
-     * @param value value of the annotation
-     */
-    void value(String value);
 }
diff --git a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/CompositeData.java b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/CompositeData.java
index 8809609..ea9667f 100644
--- a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/CompositeData.java
+++ b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/CompositeData.java
@@ -16,42 +16,54 @@
 
 package org.onosproject.yang.runtime.api;
 
-import org.onosproject.yang.model.DataNode;
-import org.onosproject.yang.model.ResourceId;
-
 import java.util.List;
 
 /**
- * Representation of an entity which is comprised of composite data having
- * resource identifier with list of data node.
+ * Abstraction of an entity that is composition of resource data and
+ * associated annotations information.
  */
 public interface CompositeData {
 
     /**
-     * Returns list of data nodes.
+     * Returns resource node.
      *
-     * @return list of data nodes
+     * @return resource node
      */
-    List<DataNode> dataNodes();
+    ResourceData resourceData();
 
     /**
-     * Adds a data node.
+     * Returns annotated nodes information.
      *
-     * @param node data node
+     * @return annotated nodes information
      */
-    void addDataNode(DataNode node);
+    List<AnnotatedNodeInfo> annotatedNodesInfo();
 
     /**
-     * Returns resource identifier.
-     *
-     * @return resource identifier
+     * Abstraction of an entity that represents builder of composite data.
      */
-    ResourceId resourceId();
+    interface Builder {
 
-    /**
-     * Sets resource identifier.
-     *
-     * @param identifier resource identifier
-     */
-    void resourceId(ResourceId identifier);
+        /**
+         * Sets resource node.
+         *
+         * @param node resource node
+         * @return builder
+         */
+        Builder resourceData(ResourceData node);
+
+        /**
+         * Adds information about annotated node.
+         *
+         * @param info annotated node information
+         * @return builder
+         */
+        Builder addAnnotatedNodeInfo(AnnotatedNodeInfo info);
+
+        /**
+         * Builds an instance of decoded output.
+         *
+         * @return decoded output
+         */
+        CompositeData build();
+    }
 }
diff --git a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/CompositeStream.java b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/CompositeStream.java
index 7468362..066e230 100644
--- a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/CompositeStream.java
+++ b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/CompositeStream.java
@@ -19,8 +19,8 @@
 import java.io.InputStream;
 
 /**
- * Representation of composite stream comprising of resource identifier and
- * its data.
+ * Abstraction of an entity that is composition of resource identifier and
+ * data.
  */
 public interface CompositeStream {
 
@@ -28,28 +28,14 @@
      * Retrieves the resource identifier stream on which the operation is being
      * performed.
      *
-     * @return string representation of the resource being identified
+     * @return uri as per RFC 3986
      */
     String resourceId();
 
     /**
-     * Sets resource identifier on which operation is to be performed.
-     *
-     * @param uri resource identifier string as per RFC 3986
-     */
-    void resourceId(String uri);
-
-    /**
      * Retrieves the resource data stream in the protocol encoding format.
      *
      * @return resource data
      */
     InputStream resourceData();
-
-    /**
-     * Sets resource data stream in the protocol encoding format.
-     *
-     * @param stream resource data
-     */
-    void resourceData(InputStream stream);
 }
diff --git a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/DataFormat.java b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/DataFormat.java
deleted file mode 100644
index 1ba26fb..0000000
--- a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/DataFormat.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright 2017-present 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.yang.runtime.api;
-
-/**
- * Abstraction of configuration data-format descriptor.
- */
-public interface DataFormat {
-
-    // TODO: should this be an enum instead with choices fixed at compile-time?
-    // name, e.g. "XML", "JSON"
-
-}
diff --git a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/DecodedOutput.java b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/DecodedOutput.java
deleted file mode 100644
index 600acaa..0000000
--- a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/DecodedOutput.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright 2017-present 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.yang.runtime.api;
-
-import java.util.List;
-
-/**
- * Representation of parameters which are provided by serializers as decoding
- * output.
- */
-public interface DecodedOutput {
-
-    /**
-     * Returns composite node.
-     *
-     * @return composite node
-     */
-    CompositeData getCompositeNode();
-
-    /**
-     * Sets composite node.
-     *
-     * @param node composite node
-     */
-    void setCompositeNode(CompositeData node);
-
-    /**
-     * Returns annotated nodes information.
-     *
-     * @return annotated nodes information
-     */
-    List<AnnotatedNodeInfo> getAnnotatedNodesInfo();
-
-    /**
-     * Adds information about annotated node.
-     *
-     * @param info annotated node information
-     */
-    void addAnnotatedNodeInfo(AnnotatedNodeInfo info);
-}
diff --git a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/ResourceData.java b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/ResourceData.java
new file mode 100644
index 0000000..05e2137
--- /dev/null
+++ b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/ResourceData.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2017-present 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.yang.runtime.api;
+
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.ResourceId;
+
+import java.util.List;
+
+/**
+ * Abstraction of an entity that is representation of resource data.
+ */
+public interface ResourceData {
+
+    /**
+     * Returns list of data nodes.
+     *
+     * @return list of data nodes
+     */
+    List<DataNode> dataNodes();
+
+    /**
+     * Returns resource identifier.
+     *
+     * @return resource identifier
+     */
+    ResourceId resourceId();
+
+    /**
+     * Abstraction of an entity that represents builder of composite data.
+     */
+    interface Builder {
+
+        /**
+         * Adds a data node.
+         *
+         * @param node data node
+         * @return builder
+         */
+        Builder addDataNode(DataNode node);
+
+        /**
+         * Sets resource identifier.
+         *
+         * @param identifier resource identifier
+         * @return builder
+         */
+        Builder resourceId(ResourceId identifier);
+
+        /**
+         * Builds an instance of composite data.
+         *
+         * @return composite data
+         */
+        CompositeStream build();
+    }
+}
diff --git a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangRuntimeService.java b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangRuntimeService.java
index 747907c..e75d329 100644
--- a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangRuntimeService.java
+++ b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangRuntimeService.java
@@ -16,8 +16,6 @@
 
 package org.onosproject.yang.runtime.api;
 
-import java.util.List;
-
 /**
  * Service for encoding and decoding between internal and external model
  * representations.
@@ -37,20 +35,16 @@
      * <p>
      * Also protocols like NETCONF will have decorations around the input stream
      * which will be reported back to protocol in output. Produced
-     * annotations will be in order of preorder traversal.
+     * annotations will be in order of pre-order traversal.
      *
-     * @param external      composite input stream carrying external
-     *                      representation of configuration data
-     * @param dataFormat    data format of the provided external representation
-     * @param nodeAnnotated true if annotations are expected, false otherwise
+     * @param external   composite input stream carrying external
+     *                   representation of configuration data
+     * @param dataFormat data format of the provided external representation
      * @return in-memory representation of configuration data with decorated
      * node information
      * @throws YangRuntimeException when fails to perform decode operation
      */
-    // FIXME: This needs to be revised for symmetry with encode and mutability
-    // FIXME: Entities exchanged across this boundary ought not to be mutable
-    DecodedOutput decode(CompositeStream external, String dataFormat,
-                         boolean nodeAnnotated);
+    CompositeData decode(CompositeStream external, String dataFormat);
 
     /**
      * Encodes the internal in-memory representation of a configuration model
@@ -68,17 +62,14 @@
      * form of body without URI.
      * <p>
      * Also protocols like NETCONF would like to provide additional
-     * decorations for the node. These decoration should be in preorder
+     * decorations for the node. These decoration should be in pre-order
      * traversal order.
      *
      * @param internal   in-memory representation of configuration data
      * @param dataFormat expected data format of the external representation
-     * @param info       decorated node information
      * @return input stream carrying external representation of
      * configuration data
      * @throws YangRuntimeException when fails to perform encode operation
      */
-    // FIXME: Entities exchanged across this boundary ought not to be mutable
-    CompositeStream encode(CompositeData internal, String dataFormat,
-                           List<AnnotatedNodeInfo> info);
+    CompositeStream encode(CompositeData internal, String dataFormat);
 }
diff --git a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangSerializer.java b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangSerializer.java
index 5021a27..46e026d 100644
--- a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangSerializer.java
+++ b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangSerializer.java
@@ -18,8 +18,6 @@
 
 import org.onosproject.yang.model.DataNode;
 
-import java.util.List;
-
 /**
  * Abstraction of entity capable of encoding and decoding arbitrary
  * {@link DataNode} structures, which are in-memory representations of
@@ -51,7 +49,7 @@
      * <p>
      * Also protocols like NETCONF will have decorations around the input stream
      * which will be reported back to protocol in output. Produced
-     * annotations will be in order of preorder traversal.
+     * annotations will be in order of pre-order traversal.
      *
      * @param external composite input stream carrying external
      *                 representation of configuration data
@@ -60,7 +58,7 @@
      * node information
      * @throws YangRuntimeException when fails to perform decode operation
      */
-    DecodedOutput decode(CompositeStream external, YangSerializerContext context);
+    CompositeData decode(CompositeStream external, YangSerializerContext context);
 
     /**
      * Encodes the internal in-memory representation of a configuration model
@@ -78,14 +76,14 @@
      * form of body without URI.
      * <p>
      * Also protocols like NETCONF would like to provide additional
-     * decorations for the node. These decoration should be in preorder
+     * decorations for the node. These decoration should be in pre-order
      * traversal order.
      *
      * @param internal in-memory representation of configuration data
-     * @param info     decorated node information
+     * @param context  YANG serializer context
      * @return input stream carrying external representation of
      * configuration data
      * @throws YangRuntimeException when fails to perform decode operation
      */
-    CompositeStream encode(CompositeData internal, List<AnnotatedNodeInfo> info);
+    CompositeStream encode(CompositeData internal, YangSerializerContext context);
 }
diff --git a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangSerializerContext.java b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangSerializerContext.java
index 790d8fd..1feee33 100644
--- a/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangSerializerContext.java
+++ b/runtime/api/src/main/java/org/onosproject/yang/runtime/api/YangSerializerContext.java
@@ -24,13 +24,6 @@
 public interface YangSerializerContext {
 
     /**
-     * Provides information about whether node annotation is expected.
-     *
-     * @return true if node can be annotated, false otherwise
-     */
-    boolean isNodeAnnotated();
-
-    /**
      * Returns schema context of root node "/".
      *
      * @return schema context provider