Adding a early version of the passive component to handle communication between the NETCONF subsystem and the Dynamic Config Store (as well as other apps).

Change-Id: I61890f057825f40e4db4a73a04b063788b6e6a0c
diff --git a/apps/netconf/client/BUCK b/apps/netconf/client/BUCK
index b31c727..49070d1 100644
--- a/apps/netconf/client/BUCK
+++ b/apps/netconf/client/BUCK
@@ -1,9 +1,23 @@
+APPS = [
+  'org.onosproject.yang',
+]
+
 COMPILE_DEPS = [
   '//lib:CORE_DEPS',
+  '//lib:onos-yang-model',
   '//lib:onos-yang-runtime',
-  '//protocols/netconf/api:onos-protocols-netconf-api'
+  '//protocols/netconf/api:onos-protocols-netconf-api',
+  '//utils/osgi:onlab-osgi',
 ]
 
 osgi_jar_with_tests (
   deps = COMPILE_DEPS,
+)
+
+onos_app (
+  title = 'Dynamic Config Netconf App',
+  category = 'Utility',
+  url = 'http://onosproject.org',
+  description = 'Netconf support for Dynamic configuration service.',
+  required_apps = APPS,
 )
\ No newline at end of file
diff --git a/apps/netconf/client/src/main/java/org/onosproject/netconf/client/api/NetconfTranslator.java b/apps/netconf/client/src/main/java/org/onosproject/netconf/client/NetconfTranslator.java
similarity index 66%
rename from apps/netconf/client/src/main/java/org/onosproject/netconf/client/api/NetconfTranslator.java
rename to apps/netconf/client/src/main/java/org/onosproject/netconf/client/NetconfTranslator.java
index 973cdb9..b30a495 100644
--- a/apps/netconf/client/src/main/java/org/onosproject/netconf/client/api/NetconfTranslator.java
+++ b/apps/netconf/client/src/main/java/org/onosproject/netconf/client/NetconfTranslator.java
@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-package org.onosproject.netconf.client.api;
+package org.onosproject.netconf.client;
 
 import org.onosproject.net.DeviceId;
+import org.onosproject.yang.model.ResourceData;
 import com.google.common.annotations.Beta;
-import org.onosproject.yang.runtime.CompositeData;
 
 import java.io.IOException;
 
@@ -29,47 +29,60 @@
 @Beta
 public interface NetconfTranslator {
     /**
-     * Retrieves and returns the configuration of the specified device in the scope
-     * specified by the filter provided by the supplied {@link CompositeData}.
+     * Retrieves and returns the configuration of the specified device.
      *
      * @param deviceId the deviceID of the device to be contacted
-     * @return a {@link CompositeData} containing the requested configuration
+     * @return a {@link ResourceData} containing the requested configuration
      * @throws IOException if serialization fails or the netconf subsystem is
      * unable to handle the request
      */
-    /*FIXME note in the comments I believe that the composite data should
-    provide the filter but I am unsure what data the yang runtime will provide */
     /*TODO a future version of this API will support an optional filter type.*/
-    CompositeData getDeviceConfig(DeviceId deviceId) throws IOException;
+    ResourceData getDeviceConfig(DeviceId deviceId) throws IOException;
 
 
     /**
      * Adds to, overwrites, or deletes the selected device's configuration in the scope
-     * and manner specified by the CompositeData.
+     * and manner specified by the ResourceData.
      *
      * @param deviceId the deviceID fo the device to be contacted
-     * @param compositeData the representation of the configuration to be pushed
+     * @param resourceData the representation of the configuration to be pushed
      *                      to the device via NETCONF as well as the filter to
      *                      be used.
+     * @param operationType specifier for the type of edit operation to be
+     *                      performed (see enum for details)
      * @return a boolean, true if the operation succeeded, false otherwise
      * @throws IOException if serialization fails or the netconf subsystem is
      * unable to handle the request
      */
-    boolean editDeviceConfig(DeviceId deviceId, CompositeData compositeData) throws IOException;
+    boolean editDeviceConfig(DeviceId deviceId, ResourceData resourceData,
+                             OperationType operationType) throws IOException;
 
     /* FIXME eventually expose the copy, delete, lock and unlock netconf methods */
 
     /**
-     * Returns the configuration and running statistics from the specified device
-     * in the scope specified by the filter provided by the {@link CompositeData}.
+     * Returns the configuration and running statistics from the specified device.
      *
      * @param deviceId the deviceID of the device to be contacted.
-     * @return a {@link CompositeData} containing the requested configuration
+     * @return a {@link ResourceData} containing the requested configuration
      * and statistics
      * @throws IOException if serialization fails or the netconf subsystem is
      * unable to handle the request
      */
     /*TODO a future version of this API will support an optional filter type.*/
 
-    CompositeData getDeviceState(DeviceId deviceId) throws IOException;
+    ResourceData getDeviceState(DeviceId deviceId) throws IOException;
+
+    /**
+     * Specifiers for the operations types when calling editConfig.
+     */
+    public static enum OperationType {
+        /**
+         * Deletes the specified nodes.
+         */
+        DELETE,
+        /**
+         * Replaces the specified nodes.
+         */
+        REPLACE
+    }
 }
diff --git a/apps/netconf/client/src/main/java/org/onosproject/netconf/client/impl/NetconfTranslatorImpl.java b/apps/netconf/client/src/main/java/org/onosproject/netconf/client/impl/NetconfTranslatorImpl.java
new file mode 100644
index 0000000..c74ea54
--- /dev/null
+++ b/apps/netconf/client/src/main/java/org/onosproject/netconf/client/impl/NetconfTranslatorImpl.java
@@ -0,0 +1,372 @@
+/*
+ * 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.netconf.client.impl;
+
+import org.onosproject.cluster.NodeId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.netconf.client.NetconfTranslator;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfDevice;
+import org.onosproject.netconf.NetconfSession;
+
+import org.onosproject.yang.model.ResourceData;
+import org.onosproject.yang.model.KeyLeaf;
+import org.onosproject.yang.model.ListKey;
+import org.onosproject.yang.model.LeafListKey;
+import org.onosproject.yang.model.SchemaId;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.NodeKey;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.InnerNode;
+import org.onosproject.yang.model.LeafNode;
+import org.onosproject.yang.model.SchemaContext;
+
+import org.onosproject.yang.runtime.DefaultCompositeData;
+import org.onosproject.yang.runtime.DefaultRuntimeContext;
+import org.onosproject.yang.runtime.YangRuntimeService;
+import org.onosproject.yang.runtime.YangModelRegistry;
+import org.onosproject.yang.runtime.DefaultCompositeStream;
+import org.onosproject.yang.runtime.CompositeStream;
+import org.onosproject.yang.runtime.DefaultAnnotatedNodeInfo;
+import org.onosproject.yang.runtime.DefaultAnnotation;
+import org.onosproject.yang.runtime.DefaultResourceData;
+import org.onosproject.yang.runtime.YangSerializerContext;
+import org.onosproject.yang.runtime.DefaultYangSerializerContext;
+/*FIXME these imports are not visible using OSGI*/
+import org.onosproject.yang.runtime.helperutils.SerializerHelper;
+
+import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE;
+import static org.onosproject.yang.runtime.helperutils.SerializerHelper.addDataNode;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.List;
+import java.util.Iterator;
+
+import com.google.common.annotations.Beta;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+
+import org.osgi.service.component.ComponentContext;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/*TODO once the API's are finalized this comment will be made more specified.*/
+/**
+ * Translator which accepts data types defined for the DynamicConfigService and
+ * makes the appropriate calls to NETCONF devices before encoding and returning
+ * responses in formats suitable for the DynamicConfigService.
+ *
+ * NOTE: This entity does not ensure you are the master of a device you attempt
+ * to contact. If you are not the master an error will be thrown because there
+ * will be no session available.
+ */
+@Beta
+@Component(immediate = true)
+public class NetconfTranslatorImpl implements NetconfTranslator {
+
+    private final Logger log = LoggerFactory
+            .getLogger(getClass());
+
+    private NodeId localNodeId;
+
+    private static final String GET_CONFIG_MESSAGE_REGEX =
+            "<data>\n?\\s*(.*?)\n?\\s*</data>";
+    private static final int GET_CONFIG_CORE_MESSAGE_GROUP = 1;
+    private static final Pattern GET_CONFIG_CORE_MESSAGE_PATTERN =
+            Pattern.compile(GET_CONFIG_MESSAGE_REGEX, Pattern.DOTALL);
+    private static final String GET_CORE_MESSAGE_REGEX = "<data>\n?\\s*(.*?)\n?\\s*</data>";
+    private static final int GET_CORE_MESSAGE_GROUP = 1;
+    private static final Pattern GET_CORE_MESSAGE_PATTERN =
+            Pattern.compile(GET_CORE_MESSAGE_REGEX, Pattern.DOTALL);
+
+    private static final String NETCONF_1_0_BASE_NAMESPACE =
+            "urn:ietf:params:xml:ns:netconf:base:1.0";
+
+    private static final String GET_URI = "urn:ietf:params:xml:ns:yang:" +
+            "yrt-ietf-network:networks/network/node";
+    private static final String XML_ENCODING_SPECIFIER = "XML";
+    private static final String OP_SPECIFIER = "xc:operation";
+    private static final String REPLACE_OP_SPECIFIER = "replace";
+    private static final String DELETE_OP_SPECIFIER = "delete";
+    private static final String XMLNS_XC_SPECIFIER = "xmlns:xc";
+    private static final String XMLNS_SPECIFIER = "xmlns";
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetconfController netconfController;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected YangRuntimeService yangRuntimeService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected YangModelRegistry yangModelRegistry;
+
+    @Activate
+    public void activate(ComponentContext context) {
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        log.info("Stopped");
+    }
+
+    @Override
+    public ResourceData getDeviceConfig(DeviceId deviceId) throws IOException {
+        NetconfSession session = getNetconfSession(deviceId);
+        /*FIXME "running" will be replaced with an enum once netconf supports multiple datastores.*/
+        String reply = session.getConfig("running");
+        Matcher protocolStripper = GET_CONFIG_CORE_MESSAGE_PATTERN.matcher(reply);
+        reply = protocolStripper.group(GET_CONFIG_CORE_MESSAGE_GROUP);
+        return yangRuntimeService.decode(
+                new DefaultCompositeStream(
+                        null,
+                        /*FIXME is UTF_8 the appropriate encoding? */
+                        new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))),
+                new DefaultRuntimeContext.Builder()
+                        .setDataFormat(XML_ENCODING_SPECIFIER)
+                        .addAnnotation(
+                                new DefaultAnnotation(XMLNS_SPECIFIER,
+                                                      NETCONF_1_0_BASE_NAMESPACE))
+                        .build()).resourceData();
+    }
+
+    @Override
+    public boolean editDeviceConfig(DeviceId deviceId, ResourceData resourceData,
+                                    NetconfTranslator.OperationType operationType) throws IOException {
+        NetconfSession session = getNetconfSession(deviceId);
+        ResourceData modifiedPathResourceData = getResourceData(resourceData.resourceId(),
+                                                  resourceData.dataNodes(),
+                                                       new DefaultYangSerializerContext(
+                                                               (SchemaContext) yangModelRegistry, null));
+        DefaultCompositeData.Builder compositeDataBuilder = DefaultCompositeData
+                .builder()
+                .resourceData(modifiedPathResourceData);
+        for (DataNode node : resourceData.dataNodes()) {
+            ResourceId resourceId = resourceData.resourceId();
+            if (operationType != OperationType.DELETE) {
+                resourceId = getAnnotatedNodeResourceId(resourceData.resourceId(), node);
+            }
+            if (resourceId != null) {
+                DefaultAnnotatedNodeInfo.Builder annotatedNodeInfo = DefaultAnnotatedNodeInfo.builder();
+                annotatedNodeInfo.resourceId(resourceId);
+                annotatedNodeInfo.addAnnotation(new DefaultAnnotation(OP_SPECIFIER,
+                                                                      operationType == OperationType.DELETE ?
+                                                                          DELETE_OP_SPECIFIER : REPLACE_OP_SPECIFIER));
+                compositeDataBuilder.addAnnotatedNodeInfo(annotatedNodeInfo.build());
+            }
+        }
+        CompositeStream config = yangRuntimeService.encode(compositeDataBuilder.build(),
+                                                           new DefaultRuntimeContext.Builder()
+                                                                   .setDataFormat(XML_ENCODING_SPECIFIER)
+                                                                   .addAnnotation(
+                                                                           new DefaultAnnotation(
+                                                                                   XMLNS_XC_SPECIFIER,
+                                                                                   NETCONF_1_0_BASE_NAMESPACE))
+                                                                   .build());
+        /* FIXME need to fix to string conversion. */
+        if (session.editConfig(streamToString(config.resourceData()))) {
+           /* NOTE: a failure to edit is reflected as a NetconfException.*/
+            return true;
+        }
+        log.warn("Editing of the netconf device: {} failed.", deviceId);
+        return false;
+    }
+
+    @Override
+    public ResourceData getDeviceState(DeviceId deviceId) throws IOException {
+        NetconfSession session = getNetconfSession(deviceId);
+        /*TODO the first parameter will come into use if get is required to support filters.*/
+        String reply = session.get(null, null);
+        Matcher protocolStripper = GET_CORE_MESSAGE_PATTERN.matcher(reply);
+        reply = protocolStripper.group(GET_CORE_MESSAGE_GROUP);
+        return yangRuntimeService.decode(new DefaultCompositeStream(
+                null,
+                /*FIXME is UTF_8 the appropriate encoding? */
+                new ByteArrayInputStream(reply.toString().getBytes(StandardCharsets.UTF_8))),
+                                         new DefaultRuntimeContext.Builder()
+                                                 .setDataFormat(XML_ENCODING_SPECIFIER)
+                                                 .addAnnotation(
+                                                         new DefaultAnnotation(
+                                                                 XMLNS_SPECIFIER,
+                                                                 NETCONF_1_0_BASE_NAMESPACE))
+                                                 .build()).resourceData();
+        /* NOTE: a failure to get is reflected as a NetconfException.*/
+    }
+
+    /**
+     * Returns a session for the specified deviceId if this node is its master,
+     * returns null otherwise.
+     * @param deviceId the id of node for witch we wish to retrieve a session
+     * @return a NetconfSession with the specified node or null
+     */
+    private NetconfSession getNetconfSession(DeviceId deviceId) {
+        NetconfDevice device = netconfController.getNetconfDevice(deviceId);
+        checkNotNull(device, "The specified deviceId could not be found by the NETCONF controller.");
+        NetconfSession session = device.getSession();
+        checkNotNull(session, "A session could not be retrieved for the specified deviceId.");
+        return session;
+    }
+
+    /**
+     * Accepts a stream and converts it to a string.
+     *
+     * @param stream the stream to be converted
+     * @return a string with the same sequence of characters as the stream
+     * @throws IOException if reading from the stream fails
+     */
+    private String streamToString(InputStream stream) throws IOException {
+        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
+        StringBuilder builder = new StringBuilder();
+
+        String nextLine = reader.readLine();
+        while (nextLine != null) {
+            builder.append(nextLine);
+            nextLine = reader.readLine();
+        }
+        return builder.toString();
+    }
+
+    /**
+     * Returns resource data having resource id as "/" and data node tree
+     * starting from "/" by creating data nodes for given resource id(parent
+     * node for given list of nodes) and list of child nodes.
+     *
+     * This api will be used in encode flow only.
+     *
+     * @param rid   resource identifier till parent node
+     * @param nodes list of data nodes
+     * @param cont  yang serializer context
+     * @return resource data.
+     */
+    public static ResourceData getResourceData(
+            ResourceId rid, List<DataNode> nodes, YangSerializerContext cont) {
+        List<NodeKey> keys = rid.nodeKeys();
+        Iterator<NodeKey> it = keys.iterator();
+        DataNode.Builder dbr = SerializerHelper.initializeDataNode(cont);
+
+        // checking the resource id weather it is getting started from / or not
+        if (it.next().schemaId().name() != "/") {
+            //exception
+        }
+
+        while (it.hasNext()) {
+            NodeKey nodekey = it.next();
+            SchemaId sid = nodekey.schemaId();
+            dbr = addDataNode(dbr, sid.name(), sid.namespace(),
+                              null, null);
+            if (nodekey instanceof ListKey) {
+                for (KeyLeaf keyLeaf : ((ListKey) nodekey).keyLeafs()) {
+                    String val;
+                    if (keyLeaf.leafValue() == null) {
+                        val = null;
+                    } else {
+                        val = keyLeaf.leafValAsString();
+                    }
+                    dbr = addDataNode(dbr, keyLeaf.leafSchema().name(),
+                                      sid.namespace(), val,
+                                      SINGLE_INSTANCE_LEAF_VALUE_NODE);
+                }
+            }
+        }
+
+        if (dbr instanceof LeafNode.Builder &&
+                (nodes != null || !nodes.isEmpty())) {
+            //exception "leaf/leaf-list can not have child node"
+        }
+
+        if (nodes != null && !nodes.isEmpty()) {
+            // adding the parent node for given list of nodes
+            for (DataNode node : nodes) {
+                dbr = ((InnerNode.Builder) dbr).addNode(node);
+            }
+        }
+/*FIXME this can be uncommented for use with versions of onos-yang-tools newer than 1.12.0-b6 */
+//        while (dbr.parent() != null) {
+//            dbr = SerializerHelper.exitDataNode(dbr);
+//        }
+
+        ResourceData.Builder resData = DefaultResourceData.builder();
+
+        resData.addDataNode(dbr.build());
+        resData.resourceId(null);
+        return resData.build();
+    }
+    /**
+     * Returns resource id for annotated data node by adding resource id of top
+     * level data node to given resource id.
+     *
+     * Annotation will be added to node based on the updated resource id.
+     * This api will be used in encode flow only.
+     *
+     * @param rid   resource identifier till parent node
+     * @param node  data node
+     * @return updated resource id.
+     */
+    public static ResourceId getAnnotatedNodeResourceId(ResourceId rid,
+                                                        DataNode node) {
+
+        String val;
+        ResourceId.Builder rIdBldr = ResourceId.builder();
+        try {
+            rIdBldr = rid.copyBuilder();
+        } catch (CloneNotSupportedException e) {
+            e.printStackTrace();
+        }
+        DataNode.Type type = node.type();
+        NodeKey k = node.key();
+        SchemaId sid = k.schemaId();
+
+        switch (type) {
+
+            case MULTI_INSTANCE_LEAF_VALUE_NODE:
+                val = ((LeafListKey) k).value().toString();
+                rIdBldr.addLeafListBranchPoint(sid.name(), sid.namespace(), val);
+                break;
+
+            case MULTI_INSTANCE_NODE:
+                rIdBldr.addBranchPointSchema(sid.name(), sid.namespace());
+// Preparing the list of key values for multiInstanceNode
+                for (KeyLeaf keyLeaf : ((ListKey) node.key()).keyLeafs()) {
+                    val = keyLeaf.leafValAsString();
+                    rIdBldr.addKeyLeaf(keyLeaf.leafSchema().name(), sid.namespace(), val);
+                }
+                break;
+            case SINGLE_INSTANCE_LEAF_VALUE_NODE:
+            case SINGLE_INSTANCE_NODE:
+                rIdBldr.addBranchPointSchema(sid.name(), sid.namespace());
+                break;
+
+            default:
+                throw new IllegalArgumentException();
+        }
+        return rIdBldr.build();
+    }
+}
diff --git a/apps/netconf/client/src/main/java/org/onosproject/netconf/client/impl/package-info.java b/apps/netconf/client/src/main/java/org/onosproject/netconf/client/impl/package-info.java
new file mode 100644
index 0000000..94ad193
--- /dev/null
+++ b/apps/netconf/client/src/main/java/org/onosproject/netconf/client/impl/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+
+/**
+ * Translation utility for communication between outside entities and the
+ * NETCONF southbound.
+ */
+package org.onosproject.netconf.client.impl;
diff --git a/apps/netconf/client/src/main/java/org/onosproject/netconf/client/package-info.java b/apps/netconf/client/src/main/java/org/onosproject/netconf/client/package-info.java
new file mode 100644
index 0000000..e331918
--- /dev/null
+++ b/apps/netconf/client/src/main/java/org/onosproject/netconf/client/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+/**
+ * Interface for translation utility for communication between yang entities
+ * and the NETCONF southbound.
+ */
+package org.onosproject.netconf.client;
diff --git a/apps/netconf/client/src/test/java/org/onosproject/netconf/client/NetconfTranslatorImplTest.java b/apps/netconf/client/src/test/java/org/onosproject/netconf/client/NetconfTranslatorImplTest.java
new file mode 100644
index 0000000..e489692
--- /dev/null
+++ b/apps/netconf/client/src/test/java/org/onosproject/netconf/client/NetconfTranslatorImplTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.netconf.client;
+
+import com.google.common.annotations.Beta;
+import org.junit.Test;
+import org.onosproject.netconf.client.impl.NetconfTranslatorImpl;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Test class for {@link NetconfTranslatorImpl}
+ */
+@Beta
+public class NetconfTranslatorImplTest {
+    private static final String CORE_GET_CONFIG_MESSAGE_REGEX =
+            "<data>\n?\\s*(.*?)\n?\\s*</data>";
+    private static final int GET_CONFIG_CORE_MESSAGE_GROUP = 1;
+    private static final Pattern GET_CONFIG_CORE_MESSAGE_PATTERN =
+            Pattern.compile(CORE_GET_CONFIG_MESSAGE_REGEX, Pattern.DOTALL);
+    private static final String GET_CORE_MESSAGE_REGEX = "<data>\n?\\s*(.*?)\n?\\s*</data>";
+    private static final int GET_CORE_MESSAGE_GROUP = 1;
+    private static final Pattern GET_CORE_MESSAGE_PATTERN =
+            Pattern.compile(GET_CORE_MESSAGE_REGEX, Pattern.DOTALL);
+
+    private static final String SAMPLE_GET_REPLY = "<rpc-reply message-id=\"101\"\n" +
+            "       xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
+            "    <data>\n" +
+            "      <t:top xmlns:t=\"http://example.com/schema/1.2/stats\">\n" +
+            "        <t:interfaces>\n" +
+            "          <t:interface t:ifName=\"eth0\">\n" +
+            "            <t:ifInOctets>45621</t:ifInOctets>\n" +
+            "            <t:ifOutOctets>774344</t:ifOutOctets>\n" +
+            "          </t:interface>\n" +
+            "        </t:interfaces>\n" +
+            "      </t:top>\n" +
+            "    </data>\n" +
+            "  </rpc-reply>";
+    private static final String CORRECT_FILTERED_GET_REPLY = "<t:top xmlns:t=\"http://example.com/schema/1.2/stats\">\n" +
+            "        <t:interfaces>\n" +
+            "          <t:interface t:ifName=\"eth0\">\n" +
+            "            <t:ifInOctets>45621</t:ifInOctets>\n" +
+            "            <t:ifOutOctets>774344</t:ifOutOctets>\n" +
+            "          </t:interface>\n" +
+            "        </t:interfaces>\n" +
+            "      </t:top>";
+    private static final String SAMPLE_GET_CONFIG_REPLY = "<rpc-reply message-id=\"101\"\n" +
+            "          xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
+            "       <data>\n" +
+            "         <top xmlns=\"http://example.com/schema/1.2/config\">\n" +
+            "           <users>\n" +
+            "             <user>\n" +
+            "               <name>root</name>\n" +
+            "               <type>superuser</type>\n" +
+            "               <full-name>Charlie Root</full-name>         <company-info>\n" +
+            "                 <dept>1</dept>\n" +
+            "                 <id>1</id>\n" +
+            "               </company-info>\n" +
+            "             </user>\n" +
+            "             <!-- additional <user> elements appear here... -->\n" +
+            "           </users>\n" +
+            "         </top>\n" +
+            "       </data>\n" +
+            "     </rpc-reply>";
+    private static final String CORRECT_FILTERED_GET_CONFIG_REPLY =
+            "<top xmlns=\"http://example.com/schema/1.2/config\">\n" +
+            "           <users>\n" +
+            "             <user>\n" +
+            "               <name>root</name>\n" +
+            "               <type>superuser</type>\n" +
+            "               <full-name>Charlie Root</full-name>         <company-info>\n" +
+            "                 <dept>1</dept>\n" +
+            "                 <id>1</id>\n" +
+            "               </company-info>\n" +
+            "             </user>\n" +
+            "             <!-- additional <user> elements appear here... -->\n" +
+            "           </users>\n" +
+            "         </top>";
+
+
+    @Test
+    public void testRegex() {
+        //Basic check for the getConfig regex.
+        Matcher matcher = GET_CONFIG_CORE_MESSAGE_PATTERN.matcher(SAMPLE_GET_CONFIG_REPLY);
+        matcher.find();
+//        System.out.println(matcher.group(1));
+//        System.out.println(DESIRED_SUBSTRING_GET_CONFIG);
+        assertEquals("Messages did not match", CORRECT_FILTERED_GET_CONFIG_REPLY, matcher.group(GET_CONFIG_CORE_MESSAGE_GROUP));
+        //Basic check for the get regex.
+        matcher = GET_CORE_MESSAGE_PATTERN.matcher(SAMPLE_GET_REPLY);
+        matcher.find();
+//        System.out.println(matcher.group(1));
+//        System.out.println(DESIRED_SUBSTRING_GET_CONFIG);
+        assertEquals("Messages did not match", CORRECT_FILTERED_GET_REPLY, matcher.group(GET_CORE_MESSAGE_GROUP));
+    }
+}