Add device config change request using NetCfg

Change-Id: I01ae9caf298e606333ea31b2b480d744a2815c76
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/config/TerminalDeviceConfig.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/config/TerminalDeviceConfig.java
new file mode 100644
index 0000000..5cc5301
--- /dev/null
+++ b/apps/odtn/api/src/main/java/org/onosproject/odtn/config/TerminalDeviceConfig.java
@@ -0,0 +1,112 @@
+/*
+ * 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.config;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.config.BaseConfig;
+import org.onosproject.odtn.utils.tapi.TapiNepRef;
+
+import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OdtnPortType.CLIENT;
+import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OdtnPortType.LINE;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class TerminalDeviceConfig extends BaseConfig<ConnectPoint> {
+
+    protected final Logger log = LoggerFactory.getLogger(getClass());
+
+    /**
+     * {@value #CONFIG_KEY} : a netcfg ConfigKey for {@link TerminalDeviceConfig}.
+     */
+    public static final String CONFIG_KEY = "odtn-terminal-device";
+
+    private static final String CLIENT_PORT = "client";
+    private static final String ENABLE = "enable";
+
+    /**
+     * Create a TerminalDeviceConfig for ODTN.
+     */
+    public TerminalDeviceConfig() {
+        super();
+    }
+
+    /**
+     * Create a TerminalDeviceConfig for ODTN.
+     *
+     * @param cp ConnectPoint
+     */
+    public TerminalDeviceConfig(ConnectPoint cp) {
+        ObjectMapper mapper = new ObjectMapper();
+        init(cp, CONFIG_KEY, mapper.createObjectNode(), mapper, null);
+    }
+
+    @Override
+    public boolean isValid() {
+        return isConnectPoint(CLIENT_PORT, FieldPresence.MANDATORY) &&
+                isBoolean(ENABLE, FieldPresence.MANDATORY);
+    }
+
+    public ConnectPoint clientCp() {
+        String cp = get(CLIENT_PORT, "");
+        return ConnectPoint.deviceConnectPoint(cp);
+    }
+
+    public TerminalDeviceConfig clientCp(ConnectPoint cp) {
+        String val = String.format("%s/%d", cp.deviceId(), cp.port().toLong());
+        setOrClear(CLIENT_PORT, val);
+        return this;
+    }
+
+    public Boolean isEnabled() {
+        return get(ENABLE, false);
+    }
+
+    public TerminalDeviceConfig enable() {
+        setOrClear(ENABLE, true);
+        return this;
+    }
+
+    public TerminalDeviceConfig disable() {
+        setOrClear(ENABLE, false);
+        return this;
+    }
+
+    /**
+     * Factory method in order to emit NetCfg event from onos inner call.
+     *
+     * @param line   side NodeEdgePoint of connection
+     * @param client side NodeEdgePoint of connection
+     * @return Config object for NetCfg
+     */
+    public static TerminalDeviceConfig create(TapiNepRef line, TapiNepRef client) {
+
+        if (line.getPortType() != LINE) {
+            throw new IllegalArgumentException("Argument line must be a LINE type.");
+        }
+        if (client.getPortType() != CLIENT) {
+            throw new IllegalArgumentException("Argument client must be a CLIENT type.");
+        }
+
+        TerminalDeviceConfig self = new TerminalDeviceConfig(line.getConnectPoint());
+        self.clientCp(client.getConnectPoint());
+        self.enable();
+        return self;
+    }
+}
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/config/package-info.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/config/package-info.java
new file mode 100644
index 0000000..8db93f9
--- /dev/null
+++ b/apps/odtn/api/src/main/java/org/onosproject/odtn/config/package-info.java
@@ -0,0 +1,19 @@
+/*
+ * 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 to place ODTN NetCfg Config classes.
+ */
+package org.onosproject.odtn.config;
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/DcsBasedTapiNepRef.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/DcsBasedTapiNepRef.java
deleted file mode 100644
index e5fcf2d..0000000
--- a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/DcsBasedTapiNepRef.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.utils.tapi;
-
-import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.node.OwnedNodeEdgePoint;
-import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.topology.Node;
-import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.topologycontext.Topology;
-
-/**
- * TAPI Nep reference class which has a factory method using DCS modelObject.
- */
-public final class DcsBasedTapiNepRef extends TapiNepRef {
-
-    private DcsBasedTapiNepRef(Topology topology, Node node, OwnedNodeEdgePoint nep) {
-        super(topology.uuid().toString(), node.uuid().toString(), nep.uuid().toString());
-    }
-
-    public static DcsBasedTapiNepRef create(Topology topology, Node node, OwnedNodeEdgePoint nep) {
-        return new DcsBasedTapiNepRef(topology, node, nep);
-    }
-
-}
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/DcsBasedTapiNodeRef.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/DcsBasedTapiNodeRef.java
deleted file mode 100644
index b6260a7..0000000
--- a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/DcsBasedTapiNodeRef.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.utils.tapi;
-
-import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.topology.Node;
-import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.topologycontext.Topology;
-
-/**
- * TAPI Node reference class which has a factory method using DCS modelObject.
- */
-public final class DcsBasedTapiNodeRef extends TapiNodeRef {
-
-    private DcsBasedTapiNodeRef(Topology topology, Node node) {
-        super(topology.uuid().toString(), node.uuid().toString());
-    }
-
-    public static DcsBasedTapiNodeRef create(Topology topology, Node node) {
-        return new DcsBasedTapiNodeRef(topology, node);
-    }
-}
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/DcsBasedTapiObjectRefFactory.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/DcsBasedTapiObjectRefFactory.java
new file mode 100644
index 0000000..e1d1889
--- /dev/null
+++ b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/DcsBasedTapiObjectRefFactory.java
@@ -0,0 +1,66 @@
+/*
+ * 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.utils.tapi;
+
+import org.onosproject.yang.gen.v1.tapiconnectivity.rev20180307.tapiconnectivity.connection.ConnectionEndPoint;
+import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.node.OwnedNodeEdgePoint;
+import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.topology.Node;
+import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.topologycontext.Topology;
+
+/**
+ * Util class to create TapiXXXRef class instances using classes auto-generated by onos-yang-tool compiler.
+ */
+public final class DcsBasedTapiObjectRefFactory {
+
+    private DcsBasedTapiObjectRefFactory() {
+    }
+
+    /**
+     * Factory method to create {@link TapiNodeRef}.
+     *
+     * @param topology ModelObject context/topology
+     * @param node     ModelObject context/topology/node
+     * @return node reference instance
+     */
+    public static TapiNodeRef create(Topology topology, Node node) {
+        return TapiNodeRef.create(topology.uuid().toString(), node.uuid().toString());
+    }
+
+    /**
+     * Factory method to create {@link TapiNepRef}.
+     *
+     * @param topology ModelObject context/topology
+     * @param node     ModelObject context/topology/node
+     * @param nep      ModelObject context/topology/node/ownedNodeEdgePoint
+     * @return nep reference instance
+     */
+    public static TapiNepRef create(Topology topology, Node node, OwnedNodeEdgePoint nep) {
+        return TapiNepRef.create(topology.uuid().toString(), node.uuid().toString(), nep.uuid().toString());
+    }
+
+    /**
+     * Factory method to create {@link TapiCepRef}.
+     *
+     * @param cep ModelObject context/connection/connectionEndPoint
+     * @return cep reference instance
+     */
+    public static TapiCepRef create(ConnectionEndPoint cep) {
+        return TapiCepRef.create(cep.topologyId().toString(), cep.nodeId().toString(),
+                cep.ownedNodeEdgePointId().toString(), cep.connectionEndPointId().toString());
+    }
+
+}
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiConnectionHandler.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiConnectionHandler.java
index b3d6f3e..6e3b729 100644
--- a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiConnectionHandler.java
+++ b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiConnectionHandler.java
@@ -100,6 +100,10 @@
         return this;
     }
 
+    public List<ConnectionEndPoint> getCeps() {
+        return obj.connectionEndPoint();
+    }
+
     public List<TapiConnectionHandler> getLowerConnections() {
 
         try {
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiNepRef.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiNepRef.java
index af4d299..95b3e05 100644
--- a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiNepRef.java
+++ b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiNepRef.java
@@ -42,7 +42,7 @@
  * TAPI reference class should be used in ODTN ServiceApplication
  * in order to make independent ServiceApplication implementation from DCS.
  */
-public class TapiNepRef {
+public final class TapiNepRef {
 
     protected final Logger log = getLogger(getClass());
 
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiNodeRef.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiNodeRef.java
index 8db2005..71cd256 100644
--- a/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiNodeRef.java
+++ b/apps/odtn/api/src/main/java/org/onosproject/odtn/utils/tapi/TapiNodeRef.java
@@ -34,7 +34,7 @@
  * TAPI reference class should be used in ODTN ServiceApplication
  * in order to make independent ServiceApplication implementation from DCS.
  */
-public class TapiNodeRef {
+public final class TapiNodeRef {
 
     protected final Logger log = getLogger(getClass());
 
@@ -42,7 +42,7 @@
     private final UUID nodeId;
     private DeviceId deviceId;
 
-    TapiNodeRef(String topologyId, String nodeId) {
+    private TapiNodeRef(String topologyId, String nodeId) {
         this.topologyId = UUID.fromString(topologyId);
         this.nodeId = UUID.fromString(nodeId);
     }
diff --git a/apps/odtn/api/src/test/resources/odtn-terminal-device-config.json b/apps/odtn/api/src/test/resources/odtn-terminal-device-config.json
new file mode 100644
index 0000000..3a44751
--- /dev/null
+++ b/apps/odtn/api/src/test/resources/odtn-terminal-device-config.json
@@ -0,0 +1,17 @@
+{
+  "ports" : {
+    "netconf:127.0.0.1:11001/42" : {
+      "odtn-terminal-device" : {
+        "client" : "netconf:127.0.0.1:11001/41",
+        "enable" : true
+      }
+    },
+    "netconf:127.0.0.1:11002/42" : {
+      "odtn-terminal-device" : {
+        "client" : "netconf:127.0.0.1:11002/41",
+        "enable" : true
+      }
+    }
+  }
+}
+