ONOS-6078
Netconf : Active component
Change-Id: I147193091880c026e198fc723cfe054e5dbea69f
diff --git a/apps/netconfsb/BUCK b/apps/netconfsb/BUCK
new file mode 100644
index 0000000..907e4be
--- /dev/null
+++ b/apps/netconfsb/BUCK
@@ -0,0 +1,15 @@
+BUNDLES = [
+ '//apps/netconfsb/client:onos-apps-netconfsb-client',
+ '//apps/netconfsb/storeadapter:onos-apps-netconfsb-storeadapter',
+]
+
+onos_app (
+ app_name = 'org.onosproject.netconfsb',
+ title = 'NETCONF Application Module',
+ category = 'Utility',
+ url = 'http://onosproject.org',
+ description = """This application provides an interface for monitoring and modifying datastores
+ of NETCONF devices using Yang data. It uses the YangRuntime to serialize outbound
+ messages from Yang into NETCONF and deserialize received messages.""",
+ included_bundles = BUNDLES,
+)
diff --git a/apps/netconfsb/client/BUCK b/apps/netconfsb/client/BUCK
new file mode 100644
index 0000000..49070d1
--- /dev/null
+++ b/apps/netconfsb/client/BUCK
@@ -0,0 +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',
+ '//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/netconfsb/client/src/main/java/org/onosproject/netconf/client/NetconfTranslator.java b/apps/netconfsb/client/src/main/java/org/onosproject/netconf/client/NetconfTranslator.java
new file mode 100644
index 0000000..b30a495
--- /dev/null
+++ b/apps/netconfsb/client/src/main/java/org/onosproject/netconf/client/NetconfTranslator.java
@@ -0,0 +1,88 @@
+/*
+ * 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 org.onosproject.net.DeviceId;
+import org.onosproject.yang.model.ResourceData;
+import com.google.common.annotations.Beta;
+
+import java.io.IOException;
+
+/**
+ * Interface for making some standard calls to NETCONF devices with
+ * serialization performed according the appropriate device's YANG model.
+ */
+@Beta
+public interface NetconfTranslator {
+ /**
+ * Retrieves and returns the configuration of the specified device.
+ *
+ * @param deviceId the deviceID of the device to be contacted
+ * @return a {@link ResourceData} containing the requested configuration
+ * @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.*/
+ ResourceData getDeviceConfig(DeviceId deviceId) throws IOException;
+
+
+ /**
+ * Adds to, overwrites, or deletes the selected device's configuration in the scope
+ * and manner specified by the ResourceData.
+ *
+ * @param deviceId the deviceID fo the device to be contacted
+ * @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, 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.
+ *
+ * @param deviceId the deviceID of the device to be contacted.
+ * @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.*/
+
+ 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/netconfsb/client/src/main/java/org/onosproject/netconf/client/impl/NetconfTranslatorImpl.java b/apps/netconfsb/client/src/main/java/org/onosproject/netconf/client/impl/NetconfTranslatorImpl.java
new file mode 100644
index 0000000..c74ea54
--- /dev/null
+++ b/apps/netconfsb/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/netconfsb/client/src/main/java/org/onosproject/netconf/client/impl/package-info.java b/apps/netconfsb/client/src/main/java/org/onosproject/netconf/client/impl/package-info.java
new file mode 100644
index 0000000..94ad193
--- /dev/null
+++ b/apps/netconfsb/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/netconfsb/client/src/main/java/org/onosproject/netconf/client/package-info.java b/apps/netconfsb/client/src/main/java/org/onosproject/netconf/client/package-info.java
new file mode 100644
index 0000000..e331918
--- /dev/null
+++ b/apps/netconfsb/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/netconfsb/client/src/test/java/org/onosproject/netconf/client/NetconfTranslatorImplTest.java b/apps/netconfsb/client/src/test/java/org/onosproject/netconf/client/NetconfTranslatorImplTest.java
new file mode 100644
index 0000000..e489692
--- /dev/null
+++ b/apps/netconfsb/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));
+ }
+}
diff --git a/apps/netconfsb/storeadapter/BUCK b/apps/netconfsb/storeadapter/BUCK
new file mode 100644
index 0000000..6444a6c
--- /dev/null
+++ b/apps/netconfsb/storeadapter/BUCK
@@ -0,0 +1,30 @@
+COMPILE_DEPS = [
+ '//lib:CORE_DEPS',
+ '//lib:onos-yang-model',
+ '//lib:onos-yang-runtime',
+ '//apps/config:onos-apps-config',
+ '//protocols/netconf/api:onos-protocols-netconf-api',
+ '//apps/netconfsb/client:onos-apps-netconfsb-client'
+
+
+]
+
+osgi_jar_with_tests (
+ deps = COMPILE_DEPS,
+)
+
+BUNDLES = [
+ '//apps/netconfsb/client:onos-apps-netconfsb-client',
+ '//apps/netconfsb/storeadapter:onos-apps-netconfsb-storeadapter',
+]
+
+onos_app (
+ app_name = 'org.onosproject.netconfsb',
+ title = 'NETCONF Application Module',
+ category = 'Utility',
+ url = 'http://onosproject.org',
+ description = """This application provides an interface for monitoring and modifying datastores
+ of NETCONF devices using Yang data. It uses the YangRuntime to serialize outbound
+ messages from Yang into NETCONF and deserialize received messages.""",
+ included_bundles = BUNDLES,
+)
diff --git a/apps/netconfsb/storeadapter/src/main/java/org/onosproject/netconf/storeadapter/NetconfActiveComponent.java b/apps/netconfsb/storeadapter/src/main/java/org/onosproject/netconf/storeadapter/NetconfActiveComponent.java
new file mode 100644
index 0000000..8d824f7
--- /dev/null
+++ b/apps/netconfsb/storeadapter/src/main/java/org/onosproject/netconf/storeadapter/NetconfActiveComponent.java
@@ -0,0 +1,217 @@
+/*
+ * 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.storeadapter;
+
+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.onosproject.config.DynamicConfigEvent;
+import org.onosproject.config.DynamicConfigListener;
+import org.onosproject.config.DynamicConfigService;
+import org.onosproject.config.Filter;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.resource.Resource;
+import org.onosproject.netconf.client.NetconfTranslator;
+import org.onosproject.netconf.client.NetconfTranslator.OperationType;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfController;
+import java.net.URI;
+import java.net.URISyntaxException;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.LeafNode;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.runtime.DefaultResourceData;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+
+@Beta
+@Component(immediate = true)
+public class NetconfActiveComponent implements DynamicConfigListener {
+
+ private static final Logger log = LoggerFactory.getLogger(NetconfActiveComponent.class);
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected DynamicConfigService cfgService;
+ public static final String DEVNMSPACE = "namespace1";
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected NetconfTranslator netconfTranslator;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected MastershipService mastershipService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected NetconfController controller;
+
+ private ResourceId resId = new ResourceId.Builder()
+ .addBranchPointSchema("device", DEVNMSPACE )
+ .build();
+ @Activate
+ protected void activate() {
+ cfgService.addListener(this);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ cfgService.removeListener(this);
+ log.info("Stopped");
+ }
+
+ @Override
+ public boolean isRelevant(DynamicConfigEvent event) {
+ if (event.subject().equals(resId)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public boolean isMaster(DeviceId deviceId) {
+ if (mastershipService.isLocalMaster(deviceId)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public void event(DynamicConfigEvent event) {
+ Filter filt = new Filter();
+ DataNode node = cfgService.readNode(event.subject(), filt);
+ DeviceId deviceId = getDeviceId(node);
+ if (!isMaster(deviceId)) {
+ log.info("NetConfListener: not master, ignoring config for {}", event.type());
+ return;
+ }
+ initiateConnection(deviceId);
+ switch (event.type()) {
+ case NODE_ADDED:
+ case NODE_UPDATED:
+ case NODE_REPLACED:
+ configUpdate(node, deviceId, event.subject());
+ break;
+ case NODE_DELETED:
+ configDelete(node, deviceId, event.subject());
+ break;
+ case UNKNOWN_OPRN:
+ default:
+ log.warn("NetConfListener: unknown event: {}", event.type());
+ break;
+ }
+ }
+
+ /**
+ * Performs the delete operation corresponding to the passed event.
+ * @param node a relevant dataNode
+ * @param deviceId the deviceId of the device to be updated
+ * @param resourceId the resourceId of the root of the subtree to be edited
+ * @return true if the update succeeds false otherwise
+ */
+ private boolean configDelete(DataNode node, DeviceId deviceId, ResourceId resourceId) {
+ return parseAndEdit(node, deviceId, resourceId, OperationType.DELETE);
+ }
+
+ /**
+ * Performs the update operation corresponding to the passed event.
+ * @param node a relevant dataNode
+ * @param deviceId the deviceId of the device to be updated
+ * @param resourceId the resourceId of the root of the subtree to be edited
+ * @return true if the update succeeds false otherwise
+ */
+ private boolean configUpdate(DataNode node, DeviceId deviceId, ResourceId resourceId) {
+ return parseAndEdit(node, deviceId, resourceId, OperationType.REPLACE);
+ }
+
+ /**
+ * Parses the incoming event and pushes configuration to the effected
+ * device.
+ * @param node the dataNode effecting a particular device of which this node
+ * is master
+ * @param deviceId the deviceId of the device to be modified
+ * @param resourceId the resourceId of the root of the subtree to be edited
+ * @param operationType the type of editing to be performed
+ * @return true if the operation succeeds, false otherwise
+ */
+ private boolean parseAndEdit(DataNode node, DeviceId deviceId,
+ ResourceId resourceId,
+ NetconfTranslator.OperationType operationType) {
+ try {
+ return netconfTranslator.editDeviceConfig(
+ deviceId,
+ DefaultResourceData.builder()
+ .addDataNode(node)
+ .resourceId(resourceId)
+ .build(),
+ operationType);
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ /**
+ * Retrieves device id from Data node.
+ *
+ * @param node the node associated with the event
+ * @return the deviceId of the effected device
+ */
+ public DeviceId getDeviceId(DataNode node) {
+ String[] temp;
+ String ip, port;
+ if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
+ temp = ((LeafNode) node).asString().split("\\:");
+ if (temp.length != 3) {
+ throw new RuntimeException(new NetconfException("Invalid device id form, cannot apply"));
+ }
+ ip = temp[1];
+ port = temp[2];
+ } else {
+ throw new RuntimeException(new NetconfException("Invalid device id type, cannot apply"));
+ }
+ try {
+ return DeviceId.deviceId(new URI("netconf", ip + ":" + port, (String) null));
+ } catch (URISyntaxException var4) {
+ throw new IllegalArgumentException("Unable to build deviceID for device " + ip + ":" + port, var4);
+ }
+ }
+
+ /**
+ * Inititates a Netconf connection to the device.
+ *
+ * @param deviceId of the added device
+ */
+ private void initiateConnection(DeviceId deviceId) {
+ if (controller.getNetconfDevice(deviceId) == null) {
+ try {
+ //if (this.isReachable(deviceId)) {
+ this.controller.connectDevice(deviceId);
+ //}
+ } catch (Exception ex) {
+ throw new RuntimeException(new NetconfException("Can\'t " +
+ "connect to NETCONF device on " + deviceId + ":" + deviceId, ex));
+ }
+ }
+ }
+}
\ No newline at end of file