ONOS-3605 Create thread Session input stream mechanism, adding listener for events from the device

Change-Id: Ib323487f61d9e595f7ccdc1957a92e58b7002d2a
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfController.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfController.java
index ea07728..37311b1 100644
--- a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfController.java
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfController.java
@@ -19,7 +19,6 @@
 import org.onlab.packet.IpAddress;
 import org.onosproject.net.DeviceId;
 
-import java.io.IOException;
 import java.util.Map;
 
 /**
@@ -48,8 +47,9 @@
      *
      * @param deviceInfo info about the device to add
      * @return NetconfDevice Netconf device
+     * @throws NetconfException when device is not available
      */
-    NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws IOException;
+    NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws NetconfException;
 
     /**
      * Removes a Netconf device.
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfDeviceOutputEvent.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfDeviceOutputEvent.java
new file mode 100644
index 0000000..0a140f1
--- /dev/null
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfDeviceOutputEvent.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2015 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;
+
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * Describes network configuration event.
+ */
+public final class NetconfDeviceOutputEvent extends
+        AbstractEvent<NetconfDeviceOutputEvent.Type, Object> {
+
+    private final String messagePayload;
+    private final int messageID;
+    private final NetconfDeviceInfo deviceInfo;
+
+    /**
+     * Type of network configuration events.
+     */
+    public enum Type {
+        /**
+         * Signifies that sent a reply to a request.
+         */
+        DEVICE_REPLY,
+
+        /**
+         * Signifies that the device sent a notification.
+         */
+        DEVICE_NOTIFICATION,
+
+        /**
+         * Signifies that the device is not reachable.
+         */
+        DEVICE_UNREGISTERED,
+
+        /**
+         * Signifies that the device has encountered an error.
+         */
+        DEVICE_ERROR,
+
+    }
+
+    /**
+     * Creates an event of a given type and for the specified subject and the
+     * current time.
+     *
+     * @param type              event type
+     * @param subject           event subject
+     * @param payload           message from the device
+     * @param msgID             id of the message related to the event
+     * @param netconfDeviceInfo device of event
+     */
+    public NetconfDeviceOutputEvent(Type type, Object subject, String payload, int msgID,
+                                    NetconfDeviceInfo netconfDeviceInfo) {
+        super(type, subject);
+        messagePayload = payload;
+        this.messageID = msgID;
+        deviceInfo = netconfDeviceInfo;
+    }
+
+    /**
+     * Creates an event of a given type and for the specified subject and time.
+     *
+     * @param type              event type
+     * @param subject           event subject
+     * @param payload           message from the device
+     * @param msgID             id of the message related to the event
+     * @param netconfDeviceInfo device of event
+     * @param msgID             id of the message related to the event
+     * @param time              occurrence time
+     */
+    public NetconfDeviceOutputEvent(Type type, Object subject, String payload, int msgID,
+                                    NetconfDeviceInfo netconfDeviceInfo, long time) {
+        super(type, subject, time);
+        messagePayload = payload;
+        deviceInfo = netconfDeviceInfo;
+        this.messageID = msgID;
+    }
+
+    public String getMessagePayload() {
+        return messagePayload;
+    }
+
+    public NetconfDeviceInfo getDeviceInfo() {
+        return deviceInfo;
+    }
+
+    public Integer getMessageID() {
+        return messageID;
+    }
+}
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfDeviceOutputEventListener.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfDeviceOutputEventListener.java
new file mode 100644
index 0000000..e33cd9d
--- /dev/null
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfDeviceOutputEventListener.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2016 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;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Interface for Netconf device output Listeners.
+ */
+public interface NetconfDeviceOutputEventListener extends EventListener<NetconfDeviceOutputEvent> {
+}
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfException.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfException.java
new file mode 100644
index 0000000..4b46a4b
--- /dev/null
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfException.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2016 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;
+
+import java.io.IOException;
+
+/**
+ * Represents class of errors related to NETCONF SB protocol.
+ */
+public class NetconfException extends IOException {
+    /**
+     * Constructs an exception with the specified message.
+     *
+     * @param message the message describing the specific nature of the error
+     */
+    public NetconfException(String message) {
+        super(message);
+    }
+
+    /**
+     * Constructs an exception with the specified message and the underlying cause.
+     *
+     * @param message the message describing the specific nature of the error
+     * @param cause   the underlying cause of this error
+     */
+    public NetconfException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java
index 2f02125..bb5e996 100644
--- a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java
@@ -16,8 +16,8 @@
 
 package org.onosproject.netconf;
 
-import java.io.IOException;
 import java.util.List;
+import java.util.concurrent.CompletableFuture;
 
 /**
  * NETCONF session object that allows NETCONF operations on top with the physical
@@ -27,28 +27,45 @@
 public interface NetconfSession {
 
     /**
+     * Executes an asynchronous RPC to the server and obtains a future to be completed.
+     *
+     * @param request the XML containing the RPC for the server.
+     * @return Server response or ERROR
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     */
+    CompletableFuture<String> request(String request) throws NetconfException;
+
+
+    /**
      * Retrives the requested configuration, different from get-config.
      *
      * @param request the XML containing the request to the server.
      * @return device running configuration
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
      */
-    String get(String request) throws IOException;
+    String get(String request) throws NetconfException;
 
     /**
-     * Executes an RPC to the server.
+     * Executes an synchronous RPC to the server.
      *
      * @param request the XML containing the RPC for the server.
      * @return Server response or ERROR
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
      */
-    String doRPC(String request) throws IOException;
+    String requestSync(String request) throws NetconfException;
 
     /**
      * Retrives the specified configuration.
      *
      * @param targetConfiguration the type of configuration to retrieve.
      * @return specified configuration.
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
      */
-    String getConfig(String targetConfiguration) throws IOException;
+    String getConfig(String targetConfiguration) throws NetconfException;
 
     /**
      * Retrives part of the specivied configuration based on the filterSchema.
@@ -57,28 +74,35 @@
      * @param configurationFilterSchema XML schema to filter the configuration
      *                                  elements we are interested in
      * @return device running configuration.
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
      */
     String getConfig(String targetConfiguration, String configurationFilterSchema)
-            throws IOException;
+            throws NetconfException;
 
     /**
      * Retrives part of the specified configuration based on the filterSchema.
      *
      * @param newConfiguration configuration to set
      * @return true if the configuration was edited correctly
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
      */
 
-    boolean editConfig(String newConfiguration) throws IOException;
+    boolean editConfig(String newConfiguration) throws NetconfException;
 
     /**
      * Retrives part of the specified configuration based on the filterSchema.
+     *
      * @param targetConfiguration the targetConfiguration to change
-     * @param mode selected mode to change the configuration
-     * @param newConfiguration configuration to set
+     * @param mode                selected mode to change the configuration
+     * @param newConfiguration    configuration to set
      * @return true if the configuration was edited correctly
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
      */
     boolean editConfig(String targetConfiguration, String mode, String newConfiguration)
-            throws IOException;
+            throws NetconfException;
 
     /**
      * Copies the new configuration, an Url or a complete configuration xml tree
@@ -88,39 +112,49 @@
      * @param targetConfiguration the type of configuration to retrieve.
      * @param newConfiguration    configuration to set
      * @return true if the configuration was copied correctly
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
      */
     boolean copyConfig(String targetConfiguration, String newConfiguration)
-            throws IOException;
+            throws NetconfException;
 
     /**
      * Deletes part of the specified configuration based on the filterSchema.
      *
      * @param targetConfiguration the name of the configuration to delete
      * @return true if the configuration was copied correctly
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
      */
-    boolean deleteConfig(String targetConfiguration) throws IOException;
+    boolean deleteConfig(String targetConfiguration) throws NetconfException;
 
     /**
      * Locks the candidate configuration.
      *
      * @return true if successful.
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
      */
-    boolean lock() throws IOException;
+    boolean lock() throws NetconfException;
 
     /**
      * Unlocks the candidate configuration.
      *
      * @return true if successful.
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
      */
-    boolean unlock() throws IOException;
+    boolean unlock() throws NetconfException;
 
     /**
      * Closes the Netconf session with the device.
      * the first time it tries gracefully, then kills it forcefully
      *
      * @return true if closed
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
      */
-    boolean close() throws IOException;
+    boolean close() throws NetconfException;
 
     /**
      * Gets the session ID of the Netconf session.
@@ -137,10 +171,24 @@
     String getServerCapabilities();
 
     /**
-     * Sets the device capabilities.
+     * Sets the ONOS side capabilities.
      *
      * @param capabilities list of capabilities the device has.
      */
     void setDeviceCapabilities(List<String> capabilities);
 
+    /**
+     * Remove a listener from the underlying stream handler implementation.
+     *
+     * @param listener event listener.
+     */
+    void addDeviceOutputListener(NetconfDeviceOutputEventListener listener);
+
+    /**
+     * Remove a listener from the underlying stream handler implementation.
+     *
+     * @param listener event listener.
+     */
+    void removeDeviceOutputListener(NetconfDeviceOutputEventListener listener);
+
 }