aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/core/NetconfDeviceCommunicatorTest.kt
blob: be8b2a2da0aa7d3b84a37ad15c37d952018298d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * 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.mockito.Mockito
import org.mockito.kotlin.any
import org.junit.Before
import org.junit.Test
import org.mockito.kotlin.never
import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.DeviceInfo
import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.NetconfReceivedEvent
import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.NetconfSession
import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.NetconfSessionListener
import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.utils.RpcMessageUtils
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.nio.charset.StandardCharsets
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ConcurrentHashMap
import java.util.regex.Pattern
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class NetconfDeviceCommunicatorTest {

    private lateinit var netconfSession: NetconfSession
    private lateinit var netconfSessionListener: NetconfSessionListener
    private lateinit var mockInputStream: InputStream
    private lateinit var mockOutputStream: OutputStream
    private lateinit var stubInputStream: InputStream
    private lateinit var replies: MutableMap<String, CompletableFuture<String>>
    private val endPatternCharArray: List<Int> = stringToCharArray(RpcMessageUtils.END_PATTERN)

    companion object {

        private val chunkedEnding = "\n##\n"

        // using example from section 4.2 of RFC6242 (https://tools.ietf.org/html/rfc6242#section-4.2)
        private val validChunkedEncodedMsg = """
            |
            |#4
            |<rpc
            |#18
            | message-id="102"
            |
            |#79
            |     xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
            |  <close-session/>
            |</rpc>
            |##
            |""".trimMargin()
    }

    private fun stringToCharArray(str: String): List<Int> {
        return str.toCharArray().map(Char::toInt)
    }

    @Before
    fun setup() {
        netconfSession = Mockito.mock(NetconfSession::class.java)
        netconfSessionListener = Mockito.mock(NetconfSessionListener::class.java)
        mockInputStream = Mockito.mock(InputStream::class.java)
        mockOutputStream = Mockito.mock(OutputStream::class.java)
        replies = ConcurrentHashMap()
    }

    @Test
    fun `NetconfDeviceCommunicator should read from supplied reader`() {
        Mockito.`when`(mockInputStream.read()).thenReturn(-1)
        Mockito.`when`(mockInputStream.read(any(), any(), any())).thenReturn(-1)
        val communicator: NetconfDeviceCommunicator =
            NetconfDeviceCommunicator(mockInputStream, mockOutputStream, genDeviceInfo(), netconfSessionListener, replies)
        communicator.join()
        // verify
        Mockito.verify(mockInputStream).read(any(), any(), any())
    }

    @Test
    fun `NetconfDeviceCommunicator unregisters device on END_PATTERN`() {
        // The reader will generate RpcMessageUtils.END_PATTERN "]]>]]>" which tells Netconf
        // to unregister the device.
        // we want to capture the slot to return the value as inputStreamReader will pass a char array
        // create a slot where NetconfReceivedEvent will be placed to further verify Type.DEVICE_UNREGISTERED
        val captured = mutableListOf<NetconfReceivedEvent>()
        Mockito.doAnswer {
            captured.add(it.getArgument(0))
        }.`when`(netconfSessionListener).accept(any())
        stubInputStream = RpcMessageUtils.END_PATTERN.byteInputStream(StandardCharsets.UTF_8)
        // RUN the test
        val communicator = NetconfDeviceCommunicator(
            stubInputStream, mockOutputStream,
            genDeviceInfo(), netconfSessionListener, replies
        )
        communicator.join()
        // Verify
        assertTrue(captured.size == 1)
        assertEquals(NetconfReceivedEvent.Type.DEVICE_UNREGISTERED, captured[0].type)
        assertEquals(genDeviceInfo(), captured[0].deviceInfo)
    }

    @Test
    fun `NetconfDeviceCommunicator on IOException generated DEVICE_ERROR event`() {
        val captured = mutableListOf<NetconfReceivedEvent>()
        Mockito.doAnswer {
            captured.add(it.getArgument(0))
        }.`when`(netconfSessionListener).accept(any())
        stubInputStream = "".byteInputStream(StandardCharsets.UTF_8)
        Mockito.`when`(mockInputStream.read(any(), any(), any()))
            .thenReturn(1).thenThrow(IOException("Fake IO"))
        // RUN THE TEST
        val communicator = NetconfDeviceCommunicator(
            mockInputStream, mockOutputStream,
            genDeviceInfo(), netconfSessionListener, replies
        )
        communicator.join()
        // Verify
        assertTrue(captured.size == 1)
        assertEquals(genDeviceInfo(), captured[0].deviceInfo)
        assertEquals(NetconfReceivedEvent.Type.DEVICE_ERROR, captured[0].type)
    }

    @Test
    fun `NetconfDeviceCommunicator in END_PATTERN state but fails RpcMessageUtils end pattern validation`() {
        val captured = mutableListOf<NetconfReceivedEvent>()
        Mockito.doAnswer {
            captured.add(it.getArgument(0))
        }.`when`(netconfSessionListener).accept(any())
        val payload = "<rpc-reply>blah</rpc-reply>"
        stubInputStream = "$payload${RpcMessageUtils.END_PATTERN}".byteInputStream(StandardCharsets.UTF_8)
        Mockito.doAnswer {
            val bytes = stubInputStream.readAllBytes()
            bytes.forEachIndexed { index, byte ->
                (it.getArgument(0) as ByteArray)[index] = byte
            }
            bytes.size
        }.doReturn(-1).`when`(mockInputStream).read(any(), any(), any())
        // RUN the test
        val communicator = NetconfDeviceCommunicator(
            mockInputStream, mockOutputStream,
            genDeviceInfo(), netconfSessionListener, replies
        )
        communicator.join()
        // Verify
        Mockito.verify(mockInputStream, never()).close() // make sure the reader is not closed as this could cause problems
        assertTrue(captured.size == 1)
        // eventually, sessionListener is called with message type DEVICE_REPLY
        assertEquals(NetconfReceivedEvent.Type.DEVICE_REPLY, captured[0].type)
        assertEquals(payload, captured[0].messagePayload)
    }

    @Test
    fun `NetconfDeviceCommunicator in END_CHUNKED_PATTERN but validation failing produces DEVICE_ERROR`() {
        val captured = mutableListOf<NetconfReceivedEvent>()
        Mockito.doAnswer {
            captured.add(it.getArgument(0))
        }.`when`(netconfSessionListener).accept(any())
        val payload = "<rpc-reply>blah</rpc-reply>"
        val payloadWithChunkedEnding = "$payload$chunkedEnding"

        stubInputStream = payloadWithChunkedEnding.byteInputStream(StandardCharsets.UTF_8)
        Mockito.doAnswer {
            val bytes = stubInputStream.readAllBytes()
            bytes.forEachIndexed { index, byte ->
                (it.getArgument(0) as ByteArray)[index] = byte
            }
            bytes.size
        }.doReturn(-1).`when`(mockInputStream).read(any(), any(), any())
        // RUN the test
        val communicator = NetconfDeviceCommunicator(
            mockInputStream, mockOutputStream, genDeviceInfo(),
            netconfSessionListener, replies
        )
        communicator.join()
        // Verify
        Mockito.verify(mockInputStream, never()).close() // make sure the reader is not closed as this could cause problems
        assertTrue(captured.size == 1)
        // eventually, sessionListener is called with message type DEVICE_REPLY
        assertEquals(NetconfReceivedEvent.Type.DEVICE_ERROR, captured[0].type)
        assertEquals("", captured[0].messagePayload)
    }

    @Test
    fun `NetconfDeviceCommunicator in END_CHUNKED_PATTERN passing validation generates DEVICE_REPLY`() {
        val captured = mutableListOf<NetconfReceivedEvent>()
        Mockito.doAnswer {
            captured.add(it.getArgument(0))
        }.`when`(netconfSessionListener).accept(any())
        stubInputStream = validChunkedEncodedMsg.byteInputStream(StandardCharsets.UTF_8)
        Mockito.doAnswer {
            val bytes = stubInputStream.readAllBytes()
            bytes.forEachIndexed { index, byte ->
                (it.getArgument(0) as ByteArray)[index] = byte
            }
            bytes.size
        }.doReturn(-1).`when`(mockInputStream).read(any(), any(), any())
        // RUN the test
        NetconfDeviceCommunicator(mockInputStream, mockOutputStream, genDeviceInfo(), netconfSessionListener, replies).join()
        // Verify
        Mockito.verify(mockOutputStream, never()).close() // make sure the reader is not closed as this could cause problems
        assertTrue(captured.size == 1)
        // eventually, sessionListener is called with message type DEVICE_REPLY
        assertEquals(NetconfReceivedEvent.Type.DEVICE_REPLY, captured[0].type)
        assertEquals(
            """
        <rpc message-id="102"
             xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
          <close-session/>
        </rpc>
            """.trimIndent(),
            captured[0].messagePayload
        )
    }

    @Test
    // test to ensure that we have a valid test message to be then used in the case of chunked message
    // validation code path
    fun `chunked sample is validated by the chunked response regex`() {
        val test1 = "\n#10\nblah\n##\n"
        val chunkedFramingPattern = Pattern.compile("(\\n#([1-9][0-9]*)\\n(.+))+\\n##\\n", Pattern.DOTALL)
        val matcher = chunkedFramingPattern.matcher(test1)
        assertTrue { matcher.matches() }
    }

    @Test
    // Verify that our test sample passes the second pattern for chunked size
    fun `chunkSizeMatcher pattern finds matches in chunkedMessageSample`() {
        val sizePattern = Pattern.compile("\\n#([1-9][0-9]*)\\n")
        val matcher = sizePattern.matcher(validChunkedEncodedMsg)
        assertTrue { matcher.find() }
    }

    @Test
    fun `sendMessage writes the request to NetconfDeviceCommunicator Writer`() {
        val msgPayload = "some text"
        val msgId = "100"
        stubInputStream = "".byteInputStream(StandardCharsets.UTF_8) // no data available in the stream...
        // Run the command
        val communicator = NetconfDeviceCommunicator(
            stubInputStream, mockOutputStream,
            genDeviceInfo(), netconfSessionListener, replies
        )
        val completableFuture = communicator.sendMessage(msgPayload, msgId)
        communicator.join()
        // verify
        Mockito.verify(mockOutputStream).write(any(), any(), any())
        Mockito.verify(mockOutputStream).flush()
        assertFalse { completableFuture.isCompletedExceptionally }
    }

    @Test
    fun `sendMessage on IOError returns completed exceptionally future`() {
        val msgPayload = "some text"
        val msgId = "100"
        Mockito.`when`(mockOutputStream.write(any(), any(), any()))
            .thenThrow(IOException("Some IO error occurred!"))
        stubInputStream = "".byteInputStream(StandardCharsets.UTF_8) // no data available in the stream...
        // Run the command
        val communicator = NetconfDeviceCommunicator(
            stubInputStream, mockOutputStream,
            genDeviceInfo(), netconfSessionListener, replies
        )
        val completableFuture = communicator.sendMessage(msgPayload, msgId)
        // verify
        Mockito.verify(mockOutputStream).write(any(), any(), any())
        Mockito.verify(mockOutputStream, never()).flush()
        assertTrue { completableFuture.isCompletedExceptionally }
    }

    private fun genDeviceInfo(): DeviceInfo {
        return DeviceInfo().apply {
            username = "user"
            password = "pass"
            ipAddress = "localhost"
            port = 4567
        }
    }
}