Add device driver for ODTN Phase1.0

Change-Id: Ie1b224f3bc5896d0b4d547b5f90257fc589347da
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/behaviour/OdtnTerminalDeviceDriver.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/behaviour/OdtnTerminalDeviceDriver.java
new file mode 100644
index 0000000..46179ed
--- /dev/null
+++ b/apps/odtn/api/src/main/java/org/onosproject/odtn/behaviour/OdtnTerminalDeviceDriver.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.odtn.behaviour;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+/**
+ * Device driver interface for ODTN Phase1.0.
+ */
+@Beta
+public interface OdtnTerminalDeviceDriver {
+
+    enum Operation {
+        CREATE("create"),
+        MERGE("merge"),
+        DELETE("delete");
+
+        private final String value;
+
+        Operation(String op) {
+            this.value = op;
+        }
+
+        public String value() {
+            return this.value;
+        }
+
+    }
+
+    /**
+     * Configure terminal device.
+     *
+     * @param did    Device ID
+     * @param client side port of transceiver to enable/disable
+     * @param line   side port of transceiver to enable/disable
+     * @param enable or disable
+     */
+    void apply(DeviceId did, PortNumber client, PortNumber line, boolean enable);
+}
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/YangToolUtil.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/YangToolUtil.java
index 0ec9985..b0d56cb 100644
--- a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/YangToolUtil.java
+++ b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/YangToolUtil.java
@@ -294,6 +294,7 @@
      * @return DataNode
      */
     public static DataNode toDataNode(ModelObject input) {
+        // FIXME this converter will work with root-level nodes only.
         initStaticContext();
         ModelObjectData modelData = DefaultModelObjectData.builder()
                 .addModelObject(input)
diff --git a/apps/odtn/service/src/main/java/org/onosproject/odtn/impl/ServiceApplicationComponent.java b/apps/odtn/service/src/main/java/org/onosproject/odtn/impl/ServiceApplicationComponent.java
index a663cc8..ff813e5 100644
--- a/apps/odtn/service/src/main/java/org/onosproject/odtn/impl/ServiceApplicationComponent.java
+++ b/apps/odtn/service/src/main/java/org/onosproject/odtn/impl/ServiceApplicationComponent.java
@@ -26,6 +26,7 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
 import org.onosproject.net.Link;
 import org.onosproject.net.config.ConfigFactory;
 import org.onosproject.net.config.NetworkConfigEvent;
@@ -45,6 +46,7 @@
 import org.onosproject.odtn.internal.DcsBasedTapiConnectivityRpc;
 import org.onosproject.odtn.internal.DcsBasedTapiDataProducer;
 import org.onosproject.odtn.internal.TapiDataProducer;
+import org.onosproject.odtn.internal.DefaultOdtnTerminalDeviceDriver;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -247,6 +249,26 @@
 
             log.info("type: {}", event.type());
             log.info("subject: {}", event.subject());
+            DeviceId did = ((ConnectPoint) event.subject()).deviceId();
+
+            DefaultOdtnTerminalDeviceDriver driver = DefaultOdtnTerminalDeviceDriver.create();
+            TerminalDeviceConfig config;
+
+            switch (event.type()) {
+                case CONFIG_ADDED:
+                case CONFIG_UPDATED:
+                    config = (TerminalDeviceConfig) event.config().get();
+                    log.info("config: {}", config);
+                    driver.apply(did, config.clientCp().port(), config.subject().port(), config.isEnabled());
+                    break;
+                case CONFIG_REMOVED:
+                    config = (TerminalDeviceConfig) event.prevConfig().get();
+                    log.info("config: {}", config);
+                    driver.apply(did, config.clientCp().port(), config.subject().port(), false);
+                    break;
+                default:
+                    log.error("Unsupported event type.");
+            }
         }
     }
 
diff --git a/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/DefaultOdtnTerminalDeviceDriver.java b/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/DefaultOdtnTerminalDeviceDriver.java
new file mode 100644
index 0000000..8f75100
--- /dev/null
+++ b/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/DefaultOdtnTerminalDeviceDriver.java
@@ -0,0 +1,162 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.odtn.internal;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.netconf.NetconfDevice;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.odtn.behaviour.ConfigurableTransceiver;
+import org.onosproject.odtn.behaviour.OdtnTerminalDeviceDriver;
+import org.onosproject.odtn.behaviour.PlainTransceiver;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.osgi.DefaultServiceDirectory.getService;
+
+import static org.onosproject.odtn.utils.YangToolUtil.toCharSequence;
+import static org.onosproject.odtn.utils.YangToolUtil.toDocument;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.onlab.util.XmlString;
+import org.onosproject.netconf.NetconfController;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import com.google.common.io.CharSource;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Device driver implementation for ODTN Phase1.0.
+ * <p>
+ * NETCONF SB should be provided by DCS, but currently DCS SB driver have
+ * some critical problem to configure actual devices and netconf servers,
+ * as a workaround this posts netconf edit-config directly.
+ */
+public final class DefaultOdtnTerminalDeviceDriver implements OdtnTerminalDeviceDriver {
+
+    protected final Logger log = LoggerFactory.getLogger(getClass());
+
+    private DeviceService deviceService;
+
+    private DefaultOdtnTerminalDeviceDriver() {
+    }
+
+    public static DefaultOdtnTerminalDeviceDriver create() {
+        DefaultOdtnTerminalDeviceDriver self = new DefaultOdtnTerminalDeviceDriver();
+        self.deviceService = getService(DeviceService.class);
+        return self;
+    }
+
+    @Override
+    public void apply(DeviceId did, PortNumber client, PortNumber line, boolean enable) {
+
+        checkNotNull(did);
+        checkNotNull(client);
+        checkNotNull(line);
+
+        List<CharSequence> nodes = new ArrayList<>();
+
+        ConfigurableTransceiver transceiver =
+                Optional.ofNullable(did)
+                        .map(deviceService::getDevice)
+                        .filter(device -> device.is(ConfigurableTransceiver.class))
+                        .map(device -> device.as(ConfigurableTransceiver.class))
+                        .orElseGet(() -> new PlainTransceiver());
+
+        nodes.addAll(transceiver.enable(client, line, enable));
+        if (nodes.size() == 0) {
+            log.warn("Nothing to be configured.");
+            return;
+        }
+
+        Document doc = buildEditConfigBody(nodes);
+        configureDevice(did, doc);
+    }
+
+    private Document buildEditConfigBody(List<CharSequence> nodes) {
+
+        Document doc;
+        try {
+            doc = DocumentBuilderFactory.newInstance()
+                    .newDocumentBuilder().newDocument();
+        } catch (ParserConfigurationException e) {
+            log.error("Unexpected error", e);
+            throw new IllegalStateException(e);
+        }
+
+        Element config = addEditConfigEnvelope(doc);
+
+        for (CharSequence node : nodes) {
+            Document ldoc = toDocument(CharSource.wrap(node));
+            Element cfgRoot = ldoc.getDocumentElement();
+
+            cfgRoot.setAttribute("xc:operation", Operation.MERGE.value());
+
+            // move (or copy) node to another Document
+            config.appendChild(Optional.ofNullable(doc.adoptNode(cfgRoot))
+                    .orElseGet(() -> doc.importNode(cfgRoot, true)));
+
+        }
+
+        log.info("XML:\n{}", XmlString.prettifyXml(toCharSequence(doc)));
+        return doc;
+    }
+
+    private Element addEditConfigEnvelope(Document doc) {
+
+        // netconf rpc boilerplate part without message-id
+        Element rpc = doc.createElementNS("urn:ietf:params:xml:ns:netconf:base:1.0", "rpc");
+        doc.appendChild(rpc);
+        Element editConfig = doc.createElement("edit-config");
+        rpc.appendChild(editConfig);
+        Element target = doc.createElement("target");
+        editConfig.appendChild(target);
+        target.appendChild(doc.createElement("running"));
+
+        Element config = doc.createElement("config");
+        config.setAttributeNS("http://www.w3.org/2000/xmlns/",
+                "xmlns:xc",
+                "urn:ietf:params:xml:ns:netconf:base:1.0");
+        editConfig.appendChild(config);
+
+        return config;
+    }
+
+    private void configureDevice(DeviceId did, Document doc) {
+
+        NetconfController ctr = getService(NetconfController.class);
+        Optional.ofNullable(ctr.getNetconfDevice(did))
+                .map(NetconfDevice::getSession)
+                .ifPresent(session -> {
+                    try {
+                        session.rpc(toCharSequence(doc, false).toString()).join();
+                    } catch (NetconfException e) {
+                        log.error("Exception thrown", e);
+                    }
+                });
+    }
+
+}