YANG runtime service api updation to allow protocols to provide annotations in protocol data during decode operation.

Change-Id: Ie08dfb7227faee41ffd557c620c1f6591f646d75
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/DefaultRuntimeContext.java b/runtime/src/main/java/org/onosproject/yang/runtime/DefaultRuntimeContext.java
new file mode 100644
index 0000000..274c8d5
--- /dev/null
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/DefaultRuntimeContext.java
@@ -0,0 +1,121 @@
+/*
+ * 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;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static java.util.Objects.hash;
+
+/**
+ * Representation of default implementation of runtime context.
+ */
+public class DefaultRuntimeContext implements RuntimeContext {
+
+    private String dataFormat;
+    private List<Annotation> annotations;
+
+    /**
+     * Creates an instance of data node.
+     *
+     * @param b data node builder
+     */
+    protected DefaultRuntimeContext(Builder b) {
+        dataFormat = b.dataFormat;
+        annotations = b.annotations;
+    }
+
+    @Override
+    public String getDataFormat() {
+        return dataFormat;
+    }
+
+    @Override
+    public List<Annotation> getProtocolAnnotations() {
+        return annotations;
+    }
+
+    @Override
+    public int hashCode() {
+        return hash(dataFormat, annotations);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof DefaultRuntimeContext) {
+            DefaultRuntimeContext that = (DefaultRuntimeContext) obj;
+            return Objects.equals(dataFormat, that.dataFormat) &&
+                    Objects.equals(annotations, that.annotations);
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(getClass())
+                .add("dataFormat", dataFormat)
+                .add("annotations", annotations)
+                .toString();
+    }
+
+    /**
+     * Retrieves a new runtime context builder.
+     *
+     * @return runtime context builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * Represents implementation of runtime context builder.
+     */
+    public static class Builder implements RuntimeContext.Builder {
+
+        private String dataFormat;
+        private List<Annotation> annotations;
+
+        /**
+         * Creates an instance of runtime context builder.
+         */
+        public Builder() {
+            annotations = new LinkedList<>();
+        }
+
+        @Override
+        public RuntimeContext.Builder setDataFormat(String df) {
+            dataFormat = df;
+            return this;
+        }
+
+        @Override
+        public RuntimeContext.Builder addAnnotation(Annotation a) {
+            annotations.add(a);
+            return this;
+        }
+
+        @Override
+        public RuntimeContext build() {
+            return new DefaultRuntimeContext(this);
+        }
+    }
+}
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/DefaultYangSerializerContext.java b/runtime/src/main/java/org/onosproject/yang/runtime/DefaultYangSerializerContext.java
index 9da0b01..1ee5b1f 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/DefaultYangSerializerContext.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/DefaultYangSerializerContext.java
@@ -18,24 +18,34 @@
 
 import org.onosproject.yang.model.SchemaContext;
 
+import java.util.List;
+
 /**
  * Represents YANG serializer context implementation.
  */
 public class DefaultYangSerializerContext implements YangSerializerContext {
 
     private SchemaContext rootContext;
+    private List<Annotation> annotations;
 
     /**
      * Creates an instance of YANG serializer context.
      *
      * @param c root's schema context
+     * @param a dependent annotations
      */
-    public DefaultYangSerializerContext(SchemaContext c) {
+    public DefaultYangSerializerContext(SchemaContext c, List<Annotation> a) {
         rootContext = c;
+        annotations = a;
     }
 
     @Override
     public SchemaContext getContext() {
         return null;
     }
+
+    @Override
+    public List<Annotation> getProtocolAnnotations() {
+        return annotations;
+    }
 }
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/RuntimeContext.java b/runtime/src/main/java/org/onosproject/yang/runtime/RuntimeContext.java
new file mode 100644
index 0000000..c769a0b
--- /dev/null
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/RuntimeContext.java
@@ -0,0 +1,98 @@
+/*
+ * 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;
+
+import java.util.List;
+
+/*
+ * Example usage of getProtocolAnnotations.
+ * Reference 6241:
+ * <rpc message-id="101"
+ * xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+ * <edit-config>
+ * <target>
+ * <running/>
+ * </target>
+ * <config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
+ * <top xmlns="http://example.com/schema/1.2/config">
+ * <interface xc:operation="replace">
+ * <name>Ethernet0/0</name>
+ * <mtu>1500</mtu>
+ * <address>
+ * <name>192.0.2.4</name>
+ * <prefix-length>24</prefix-length>
+ * </address>
+ * </interface>
+ * </top>
+ * </config>
+ * </edit-config>
+ * </rpc>
+ * In above example annotation inside <config> is associated with protocol.
+ * YANG related data has dependency on the same.
+ */
+
+/**
+ * Abstraction of an entity that is representation of YANG runtime service
+ * context information.
+ */
+public interface RuntimeContext {
+
+    /**
+     * Returns data format.
+     *
+     * @return data format
+     */
+    String getDataFormat();
+
+    /**
+     * Returns dependent annotations which are present as a part of protocol
+     * specific information and YANG related data could have dependency on
+     * the same.
+     *
+     * @return list of annotations
+     */
+    List<Annotation> getProtocolAnnotations();
+
+    /**
+     * Abstraction of runtime context builder.
+     */
+    interface Builder {
+
+        /**
+         * Sets data format.
+         *
+         * @param dataFormat data format
+         * @return builder
+         */
+        Builder setDataFormat(String dataFormat);
+
+        /**
+         * Adds an annotation.
+         *
+         * @param annotation annotation
+         * @return builder
+         */
+        Builder addAnnotation(Annotation annotation);
+
+        /**
+         * Builds an instance of runtime context.
+         *
+         * @return runtime context
+         */
+        RuntimeContext build();
+    }
+}
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/YangRuntimeService.java b/runtime/src/main/java/org/onosproject/yang/runtime/YangRuntimeService.java
index 1d54ff7..a66d5fa 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/YangRuntimeService.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/YangRuntimeService.java
@@ -37,14 +37,14 @@
      * which will be reported back to protocol in output. Produced
      * 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 external composite input stream carrying external
+     *                 representation of configuration data
+     * @param context  additional YANG runtime context information
      * @return in-memory representation of configuration data with decorated
      * node information
      * @throws YangRuntimeException when fails to perform decode operation
      */
-    CompositeData decode(CompositeStream external, String dataFormat);
+    CompositeData decode(CompositeStream external, RuntimeContext context);
 
     /**
      * Encodes the internal in-memory representation of a configuration model
@@ -65,11 +65,11 @@
      * 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 internal in-memory representation of configuration data
+     * @param context  additional YANG runtime context information
      * @return input stream carrying external representation of
      * configuration data
      * @throws YangRuntimeException when fails to perform encode operation
      */
-    CompositeStream encode(CompositeData internal, String dataFormat);
+    CompositeStream encode(CompositeData internal, RuntimeContext context);
 }
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/YangSerializerContext.java b/runtime/src/main/java/org/onosproject/yang/runtime/YangSerializerContext.java
index a5972e6..d7ab7d3 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/YangSerializerContext.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/YangSerializerContext.java
@@ -18,6 +18,35 @@
 
 import org.onosproject.yang.model.SchemaContext;
 
+import java.util.List;
+
+/*
+ * Example usage of getProtocolAnnotations.
+ * Reference 6241:
+ * <rpc message-id="101"
+ * xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+ * <edit-config>
+ * <target>
+ * <running/>
+ * </target>
+ * <config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
+ * <top xmlns="http://example.com/schema/1.2/config">
+ * <interface xc:operation="replace">
+ * <name>Ethernet0/0</name>
+ * <mtu>1500</mtu>
+ * <address>
+ * <name>192.0.2.4</name>
+ * <prefix-length>24</prefix-length>
+ * </address>
+ * </interface>
+ * </top>
+ * </config>
+ * </edit-config>
+ * </rpc>
+ * In above example annotation inside <config> is associated with protocol.
+ * YANG related data has dependency on the same.
+ */
+
 /**
  * Representation of a context for decoding YANG models via serializers.
  */
@@ -29,4 +58,13 @@
      * @return schema context provider
      */
     SchemaContext getContext();
+
+    /**
+     * Returns dependent annotations which are present as a part of protocol
+     * specific information and YANG related data could have dependency on
+     * the same.
+     *
+     * @return list of annotations
+     */
+    List<Annotation> getProtocolAnnotations();
 }
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/yrhimpl/DefaultYangRuntimeHandler.java b/runtime/src/main/java/org/onosproject/yang/runtime/yrhimpl/DefaultYangRuntimeHandler.java
index 22667dd..074a06d 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/yrhimpl/DefaultYangRuntimeHandler.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/yrhimpl/DefaultYangRuntimeHandler.java
@@ -20,13 +20,14 @@
 import org.onosproject.yang.runtime.CompositeData;
 import org.onosproject.yang.runtime.CompositeStream;
 import org.onosproject.yang.runtime.DefaultYangSerializerContext;
+import org.onosproject.yang.runtime.RuntimeContext;
 import org.onosproject.yang.runtime.YangRuntimeException;
 import org.onosproject.yang.runtime.YangRuntimeService;
 import org.onosproject.yang.runtime.YangSerializer;
 import org.onosproject.yang.runtime.YangSerializerContext;
 import org.onosproject.yang.runtime.YangSerializerRegistry;
-import org.onosproject.yang.runtime.ysrimpl.DefaultYangSerializerRegistry;
 import org.onosproject.yang.runtime.ymrimpl.DefaultYangModelRegistry;
+import org.onosproject.yang.runtime.ysrimpl.DefaultYangSerializerRegistry;
 import org.slf4j.Logger;
 
 import static org.slf4j.LoggerFactory.getLogger;
@@ -41,7 +42,6 @@
     private static final String NR = " is not registered.";
     private YangSerializerRegistry registry;
     private SchemaContext rootContext;
-    private YangSerializerContext serializerContext;
 
     /**
      * Creates a new YANG runtime manager.
@@ -53,19 +53,24 @@
                                      SchemaContext c) {
         registry = r;
         rootContext = c;
-        serializerContext = new DefaultYangSerializerContext(rootContext);
     }
 
     @Override
-    public CompositeData decode(CompositeStream external, String dataFormat) {
-        YangSerializer ys = getRegisteredSerializer(dataFormat);
-        return ys.decode(external, serializerContext);
+    public CompositeData decode(CompositeStream external, RuntimeContext c) {
+        YangSerializer ys = getRegisteredSerializer(c.getDataFormat());
+        YangSerializerContext sc =
+                new DefaultYangSerializerContext(rootContext,
+                                                 c.getProtocolAnnotations());
+        return ys.decode(external, sc);
     }
 
     @Override
-    public CompositeStream encode(CompositeData internal, String dataFormat) {
-        YangSerializer ys = getRegisteredSerializer(dataFormat);
-        return ys.encode(internal, serializerContext);
+    public CompositeStream encode(CompositeData internal, RuntimeContext c) {
+        YangSerializer ys = getRegisteredSerializer(c.getDataFormat());
+        YangSerializerContext sc =
+                new DefaultYangSerializerContext(rootContext,
+                                                 c.getProtocolAnnotations());
+        return ys.encode(internal, sc);
     }
 
     /**
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/RuntimeHandlerTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/RuntimeHandlerTest.java
index 1d6369b..2f93f2b 100644
--- a/runtime/src/test/java/org/onosproject/yang/runtime/impl/RuntimeHandlerTest.java
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/RuntimeHandlerTest.java
@@ -19,6 +19,7 @@
 import org.junit.Test;
 import org.onosproject.yang.runtime.CompositeData;
 import org.onosproject.yang.runtime.CompositeStream;
+import org.onosproject.yang.runtime.DefaultRuntimeContext;
 import org.onosproject.yang.runtime.YangRuntimeService;
 import org.onosproject.yang.runtime.YangSerializer;
 import org.onosproject.yang.runtime.yrhimpl.DefaultYangRuntimeHandler;
@@ -51,11 +52,13 @@
     @Test
     public void validateRuntimeHandler() {
         register(XML);
-        CompositeData dd = s.decode(null, XML);
+        CompositeData dd = s.decode(null, DefaultRuntimeContext.builder()
+                .setDataFormat(XML).build());
         assertThat(TESTSTREAM, is(dd.resourceData().resourceId().nodeKeys()
                                           .get(0).schemaId().name()));
 
-        CompositeStream cs = s.encode(null, XML);
+        CompositeStream cs = s.encode(null, DefaultRuntimeContext.builder()
+                .setDataFormat(XML).build());
         assertThat(TESTSTREAM, is(cs.resourceId()));
     }
 
@@ -65,7 +68,8 @@
     @Test(expected = RuntimeException.class)
     public void validateRuntimeHandlerError() throws RuntimeException {
         register(XML);
-        CompositeData dd = s.decode(null, JSON);
+        CompositeData dd = s.decode(null, DefaultRuntimeContext.builder()
+                .setDataFormat(JSON).build());
         assertThat(TESTSTREAM, is(dd.resourceData().resourceId().nodeKeys()
                                           .get(0).schemaId().name()));
     }
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/TestYangSerializerContext.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/TestYangSerializerContext.java
index 5e1ebe6..bd34087 100644
--- a/runtime/src/test/java/org/onosproject/yang/runtime/impl/TestYangSerializerContext.java
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/TestYangSerializerContext.java
@@ -17,9 +17,12 @@
 package org.onosproject.yang.runtime.impl;
 
 import org.onosproject.yang.model.SchemaContext;
+import org.onosproject.yang.runtime.Annotation;
 import org.onosproject.yang.runtime.YangSerializerContext;
 import org.onosproject.yang.runtime.ymrimpl.DefaultYangModelRegistry;
 
+import java.util.List;
+
 /**
  * Tests the default schema context provider methods.
  */
@@ -35,4 +38,9 @@
         DefaultYangModelRegistry registry = schemaProvider.registry();
         return registry;
     }
+
+    @Override
+    public List<Annotation> getProtocolAnnotations() {
+        return null;
+    }
 }