netconf-executor: Moving NetconfSessionListenerImpl out of NetconfSessionImpl, and... 86/84886/1
authorOleg Mitsura <oleg.mitsura@amdocs.com>
Wed, 10 Apr 2019 13:58:21 +0000 (09:58 -0400)
committerOleg Mitsura <oleg.mitsura@amdocs.com>
Wed, 10 Apr 2019 13:59:50 +0000 (09:59 -0400)
Issue-ID: CCSDK-1126

Change-Id: I8674c247e64efdf48faf35b8d21eae5eaed14d95
Signed-off-by: Oleg Mitsura <oleg.mitsura@amdocs.com>
ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionImpl.kt
ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionListenerImpl.kt [new file with mode: 0644]
ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionListenerImplTest.kt [new file with mode: 0644]

index d0f4a1d..12eb43f 100644 (file)
@@ -236,7 +236,7 @@ class NetconfSessionImpl(private val deviceInfo: DeviceInfo, private val rpcServ
     }
 
     private fun setupHandler() {
-        val sessionListener: NetconfSessionListener = NetconfSessionListenerImpl()
+        val sessionListener: NetconfSessionListener = NetconfSessionListenerImpl(this)
         streamHandler = NetconfDeviceCommunicator(channel.invertedOut, channel.invertedIn, deviceInfo,
             sessionListener, replies)
 
@@ -262,19 +262,6 @@ class NetconfSessionImpl(private val deviceInfo: DeviceInfo, private val rpcServ
         }
     }
 
-    inner class NetconfSessionListenerImpl : NetconfSessionListener {
-        override fun accept(event: NetconfReceivedEvent) {
-            val messageId = event.messageId
-
-            when (event.type) {
-                NetconfReceivedEvent.Type.DEVICE_UNREGISTERED -> disconnect()
-                NetconfReceivedEvent.Type.DEVICE_ERROR -> errorReplies.add(event.messagePayload)
-                NetconfReceivedEvent.Type.DEVICE_REPLY -> replies[messageId]?.complete(event.messagePayload)
-                NetconfReceivedEvent.Type.SESSION_CLOSED -> disconnect()
-            }
-        }
-    }
-
     fun sessionstatus(state:String): Boolean{
         return when (state){
             "Close" -> channel.isClosed
@@ -282,4 +269,39 @@ class NetconfSessionImpl(private val deviceInfo: DeviceInfo, private val rpcServ
             else -> false
         }
     }
+
+    internal fun setStreamHandler(streamHandler: NetconfDeviceCommunicator) {
+        this.streamHandler = streamHandler
+    }
+
+    /**
+     * Add an error reply
+     * Used by {@link NetconfSessionListenerImpl}
+     */
+    internal fun addDeviceErrorReply(errReply: String) {
+        println("addDeviceErrorReply (errReply: $errReply") //TODO : get rid of this.
+        errorReplies.add(errReply)
+    }
+
+    /**
+     * Add a reply from the device
+     * Used by {@link NetconfSessionListenerImpl}
+     */
+    internal fun addDeviceReply(messageId: String, replyMsg: String) {
+        println("addDeviceReply (messageId: $messageId replyMsg: $replyMsg") //TODO : get rid of this.
+        replies[messageId]?.complete(replyMsg)
+    }
+
+    /**
+     * Internal function for accessing replies for testing.
+     */
+    internal fun getReplies() = replies
+
+    /**
+     * internal function for accessing errorReplies for testing.
+     */
+    internal fun getErrorReplies() = errorReplies
+
+    internal fun clearErrorReplies() = errorReplies.clear()
+    internal fun clearReplies() = replies.clear()
 }
\ No newline at end of file
diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionListenerImpl.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionListenerImpl.kt
new file mode 100644 (file)
index 0000000..c8b9c55
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright © 2019 Bell Canada
+ *
+ * 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.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.core
+
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.NetconfReceivedEvent
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.NetconfSessionListener
+
+/**
+ * Implementation of the NetconfSessionListener
+ * Encapsulates logic for type of message received and action that NetconfSessionImpl should take.
+ * TODO: Is there a better way to extract this out of NetconfSession, I'd like to use the NetconfSession as param,
+ * rather than NetconfSessionImpl, but at the same time, addDeviceReply/ErrorReply should not be part of the public
+ * interface....
+ */
+
+internal class NetconfSessionListenerImpl(private val session: NetconfSessionImpl) : NetconfSessionListener {
+    override fun accept(event: NetconfReceivedEvent) {
+        when (event.type) {
+            NetconfReceivedEvent.Type.DEVICE_UNREGISTERED -> session.disconnect()
+            NetconfReceivedEvent.Type.SESSION_CLOSED -> session.disconnect()
+            NetconfReceivedEvent.Type.DEVICE_ERROR -> session.addDeviceErrorReply(event.messagePayload)
+            NetconfReceivedEvent.Type.DEVICE_REPLY -> session.addDeviceReply(event.messageId, event.messagePayload)
+        }
+    }
+}
\ No newline at end of file
diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionListenerImplTest.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionListenerImplTest.kt
new file mode 100644 (file)
index 0000000..f3817b7
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright © 2019 Bell Canada
+ *
+ * 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.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.core
+
+import org.junit.Test
+import io.mockk.every
+import io.mockk.mockk
+import io.mockk.verifyAll
+import org.junit.Before
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.DeviceInfo
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.NetconfReceivedEvent
+
+class NetconfSessionListenerImplTest {
+    //Note: mockk's verifyAll is akin to verify with verifyNoMoreInteractions in Mockito
+    private val netconSession = mockk<NetconfSessionImpl>()
+
+    @Before
+    fun init() {
+        every { netconSession.disconnect() } returns Unit
+        every { netconSession.addDeviceErrorReply(any()) } returns Unit
+        every { netconSession.addDeviceReply(any(), any()) } returns Unit
+    }
+
+    @Test
+    //NetconfReceivedEvent wth DEVICE_UNREGISTERED TYPE should call disconnect() on the NetconfSession
+    fun deviceUnregisteredMessageShouldCallSessionDisconnect() {
+        val netconfSessionListener = NetconfSessionListenerImpl(netconSession)
+        val event: NetconfReceivedEvent = genEventByType(NetconfReceivedEvent.Type.DEVICE_UNREGISTERED)
+        netconfSessionListener.accept(event)
+        verifyAll { netconSession.disconnect() }
+    }
+
+    @Test
+    //NetconfReceivedEvent wth SESSION_CLOSED TYPE should ALSO call disconnect() on the NetconfSession
+    fun sessionClosedMessageShouldCallSesionDisconnect() {
+        val netconfSessionListener = NetconfSessionListenerImpl(netconSession)
+        val event: NetconfReceivedEvent = genEventByType(NetconfReceivedEvent.Type.SESSION_CLOSED)
+        netconfSessionListener.accept(event)
+        verifyAll { netconSession.disconnect() }
+    }
+
+    @Test
+    //NetconfReceivedEvent wth DEVICE_ERROR TYPE should call addDeviceErrorReply() on the NetconfSession
+    //with the event message payload
+    fun deviceErrorMessageShouldCallAddDeviceErrorReply() {
+        val netconfSessionListener = NetconfSessionListenerImpl(netconSession)
+        val event: NetconfReceivedEvent = genEventByType(NetconfReceivedEvent.Type.DEVICE_ERROR)
+        netconfSessionListener.accept(event)
+        verifyAll { netconSession.addDeviceErrorReply(event.messagePayload) }
+    }
+
+    @Test
+    //NetconfReceivedEvent wth DEVICE_REPLY TYPE should call addDeviceReply(messageId, payload) on the NetconfSession
+    fun deviceReplyMessageShouldCallAddDeviceReply() {
+        val netconfSessionListener = NetconfSessionListenerImpl(netconSession)
+        val event: NetconfReceivedEvent = genEventByType(NetconfReceivedEvent.Type.DEVICE_REPLY)
+        netconfSessionListener.accept(event)
+        verifyAll { netconSession.addDeviceReply(event.messageId, event.messagePayload) }
+    }
+
+    /**
+     * Helper to generate {@link NetconfReceivedEvent} object based on the {@link NetconfReceivedEvent.Type}
+     * @param type  {@link NetconfReceivedEvent.Type} of event
+     */
+    private fun genEventByType(type: NetconfReceivedEvent.Type): NetconfReceivedEvent {
+        return NetconfReceivedEvent(
+                type,
+                "messagePayload",
+                "messageId",
+                DeviceInfo()
+        )
+    }
+}
\ No newline at end of file