4d65d36bb9ccf7887dc39244f12b578236717ddb
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2019 AT&T, Bell Canada
3  * Modifications Copyright (c) 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.utils
18
19 import org.apache.commons.lang3.StringUtils
20 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.NetconfException
21 import org.slf4j.LoggerFactory
22 import org.xml.sax.InputSource
23 import java.io.StringReader
24 import java.nio.charset.StandardCharsets
25 import java.util.regex.MatchResult
26 import java.util.regex.Pattern
27 import javax.xml.XMLConstants
28 import javax.xml.parsers.DocumentBuilderFactory
29 import kotlin.text.Charsets.UTF_8
30
31
32 class NetconfMessageUtils {
33
34     companion object {
35         val log = LoggerFactory.getLogger(NetconfMessageUtils::class.java)
36
37         const val NEW_LINE = "\n"
38         const val CHUNKED_END_REGEX_PATTERN = "\n##\n"
39
40         val CAPABILITY_REGEX_PATTERN: Pattern = Pattern.compile(RpcMessageUtils.CAPABILITY_REGEX)
41         val SESSION_ID_REGEX_PATTERN: Pattern = Pattern.compile(RpcMessageUtils.SESSION_ID_REGEX)
42
43         private val CHUNKED_FRAMING_PATTERN: Pattern =
44             Pattern.compile("(\\n#([1-9][0-9]*)\\n(.+))+\\n##\\n", Pattern.DOTALL)
45         private val CHUNKED_SIZE_PATTERN: Pattern = Pattern.compile("\\n#([1-9][0-9]*)\\n")
46         private val MSG_ID_STRING_PATTERN = Pattern.compile("${RpcMessageUtils.MESSAGE_ID_STRING}=\"(.*?)\"")
47
48         fun get(messageId: String, filterContent: String): String {
49             val request = StringBuilder()
50
51             request.append("<get>").append(NEW_LINE)
52             if (!filterContent.isNullOrEmpty()) {
53                 request.append(RpcMessageUtils.SUBTREE_FILTER_OPEN).append(NEW_LINE)
54                 request.append(filterContent).append(NEW_LINE)
55                 request.append(RpcMessageUtils.SUBTREE_FILTER_CLOSE).append(NEW_LINE)
56             }
57             request.append("</get>")
58
59             return doWrappedRpc(messageId, request.toString())
60         }
61
62         fun getConfig(messageId: String, configType: String, filterContent: String?): String {
63             val request = StringBuilder()
64
65             request.append("<get-config>").append(NEW_LINE)
66             request.append(RpcMessageUtils.SOURCE_OPEN).append(NEW_LINE)
67             request.append(RpcMessageUtils.OPEN).append(configType).append(RpcMessageUtils.TAG_CLOSE)
68                 .append(NEW_LINE)
69             request.append(RpcMessageUtils.SOURCE_CLOSE).append(NEW_LINE)
70
71             if (!filterContent.isNullOrEmpty()) {
72                 request.append(RpcMessageUtils.SUBTREE_FILTER_OPEN).append(NEW_LINE)
73                 request.append(filterContent).append(NEW_LINE)
74                 request.append(RpcMessageUtils.SUBTREE_FILTER_CLOSE).append(NEW_LINE)
75             }
76             request.append("</get-config>")
77
78             return doWrappedRpc(messageId, request.toString())
79         }
80
81         fun doWrappedRpc(messageId: String, request: String): String {
82             val rpc = StringBuilder(RpcMessageUtils.XML_HEADER).append(NEW_LINE)
83             rpc.append(RpcMessageUtils.RPC_OPEN)
84             rpc.append(RpcMessageUtils.MESSAGE_ID_STRING).append(RpcMessageUtils.EQUAL)
85             rpc.append(RpcMessageUtils.QUOTE).append(messageId).append(RpcMessageUtils.QUOTE_SPACE)
86             rpc.append(RpcMessageUtils.NETCONF_BASE_NAMESPACE).append(RpcMessageUtils.CLOSE)
87             rpc.append(NEW_LINE).append(request).append(NEW_LINE)
88             rpc.append(RpcMessageUtils.RPC_CLOSE)
89             // rpc.append(NEW_LINE).append(END_PATTERN);
90
91             return rpc.toString()
92         }
93
94         fun editConfig(messageId: String, configType: String, defaultOperation: String?,
95                        newConfiguration: String): String {
96             val request = StringBuilder()
97             request.append("<edit-config>").append(NEW_LINE)
98             request.append(RpcMessageUtils.TARGET_OPEN).append(NEW_LINE)
99             request.append(RpcMessageUtils.OPEN).append(configType).append(RpcMessageUtils.TAG_CLOSE)
100                 .append(NEW_LINE)
101             request.append(RpcMessageUtils.TARGET_CLOSE).append(NEW_LINE)
102
103             if (defaultOperation != null) {
104                 request.append(RpcMessageUtils.DEFAULT_OPERATION_OPEN).append(defaultOperation)
105                     .append(RpcMessageUtils.DEFAULT_OPERATION_CLOSE)
106                 request.append(NEW_LINE)
107             }
108
109             request.append(RpcMessageUtils.CONFIG_OPEN).append(NEW_LINE)
110             request.append(newConfiguration.trim { it <= ' ' }).append(NEW_LINE)
111             request.append(RpcMessageUtils.CONFIG_CLOSE).append(NEW_LINE)
112             request.append("</edit-config>")
113
114             return doWrappedRpc(messageId, request.toString())
115         }
116
117         fun validate(messageId: String, configType: String): String {
118             val request = StringBuilder()
119
120             request.append("<validate>").append(NEW_LINE)
121             request.append(RpcMessageUtils.SOURCE_OPEN).append(NEW_LINE)
122             request.append(RpcMessageUtils.OPEN).append(configType).append(RpcMessageUtils.TAG_CLOSE)
123                 .append(NEW_LINE)
124             request.append(RpcMessageUtils.SOURCE_CLOSE).append(NEW_LINE)
125             request.append("</validate>")
126
127             return doWrappedRpc(messageId, request.toString())
128         }
129
130         fun commit(messageId: String, confirmed: Boolean, confirmTimeout: Int, persist: String,
131                    persistId: String): String {
132
133             if (!persist.isEmpty() && !persistId.isEmpty()) {
134                 throw NetconfException("Can't proceed <commit> with both persist($persist) and " +
135                         "persistId($persistId) specified. Only one should be specified.")
136             }
137             if (confirmed && !persistId.isEmpty()) {
138                 throw NetconfException("Can't proceed <commit> with both confirmed flag and " +
139                         "persistId($persistId) specified. Only one should be specified.")
140             }
141
142             val request = StringBuilder()
143             request.append("<commit>").append(NEW_LINE)
144             if (confirmed) {
145                 request.append("<confirmed/>").append(NEW_LINE)
146                 request.append("<confirm-timeout>$confirmTimeout</confirm-timeout>").append(NEW_LINE)
147                 if (!persist.isEmpty()) {
148                     request.append("<persist>$persist</persist>").append(NEW_LINE)
149                 }
150             }
151             if (!persistId.isEmpty()) {
152                 request.append("<persist-id>$persistId</persist-id>").append(NEW_LINE)
153             }
154             request.append("</commit>")
155
156             return doWrappedRpc(messageId, request.toString())
157         }
158
159         fun cancelCommit(messageId: String, persistId: String): String {
160             val request = StringBuilder()
161             request.append("<cancel-commit>").append(NEW_LINE)
162             if (!persistId.isEmpty()) {
163                 request.append("<persist-id>$persistId</persist-id>").append(NEW_LINE)
164             }
165             request.append("</cancel-commit>")
166
167             return doWrappedRpc(messageId, request.toString())
168         }
169
170         fun unlock(messageId: String, configType: String): String {
171             val request = StringBuilder()
172
173             request.append("<unlock>").append(NEW_LINE)
174             request.append(RpcMessageUtils.TARGET_OPEN).append(NEW_LINE)
175             request.append(RpcMessageUtils.OPEN).append(configType).append(RpcMessageUtils.TAG_CLOSE)
176                 .append(NEW_LINE)
177             request.append(RpcMessageUtils.TARGET_CLOSE).append(NEW_LINE)
178             request.append("</unlock>")
179
180             return doWrappedRpc(messageId, request.toString())
181         }
182
183         @Throws(NetconfException::class)
184         fun deleteConfig(messageId: String, configType: String): String {
185             if (configType == NetconfDatastore.RUNNING.datastore) {
186                 log.warn("Target configuration for delete operation can't be \"running\" {}", configType)
187                 throw NetconfException("Target configuration for delete operation can't be running")
188             }
189
190             val request = StringBuilder()
191
192             request.append("<delete-config>").append(NEW_LINE)
193             request.append(RpcMessageUtils.TARGET_OPEN).append(NEW_LINE)
194             request.append(RpcMessageUtils.OPEN).append(configType)
195                 .append(RpcMessageUtils.TAG_CLOSE)
196                 .append(NEW_LINE)
197             request.append(RpcMessageUtils.TARGET_CLOSE).append(NEW_LINE)
198             request.append("</delete-config>")
199
200             return doWrappedRpc(messageId, request.toString())
201         }
202
203         fun discardChanges(messageId: String): String {
204             val request = StringBuilder()
205             request.append("<discard-changes/>")
206             return doWrappedRpc(messageId, request.toString())
207         }
208
209         fun lock(messageId: String, configType: String): String {
210             val request = StringBuilder()
211
212             request.append("<lock>").append(NEW_LINE)
213             request.append(RpcMessageUtils.TARGET_OPEN).append(NEW_LINE)
214             request.append(RpcMessageUtils.OPEN).append(configType).append(RpcMessageUtils.TAG_CLOSE)
215                 .append(NEW_LINE)
216             request.append(RpcMessageUtils.TARGET_CLOSE).append(NEW_LINE)
217             request.append("</lock>")
218
219             return doWrappedRpc(messageId, request.toString())
220         }
221
222         fun closeSession(messageId: String, force: Boolean): String {
223             val request = StringBuilder()
224
225             if (force) {
226                 request.append("<kill-session/>")
227             } else {
228                 request.append("<close-session/>")
229             }
230
231             return doWrappedRpc(messageId, request.toString())
232         }
233
234         fun validateRPCXML(rpcRequest: String): Boolean {
235             try {
236                 if (StringUtils.isBlank(rpcRequest)) {
237                     return false
238                 }
239                 val dbf = DocumentBuilderFactory.newInstance()
240                 dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true)
241                 dbf.setFeature("http://xml.org/sax/features/external-general-entities", false)
242                 dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false)
243                 dbf.newDocumentBuilder()
244                     .parse(InputSource(StringReader(rpcRequest.replace(RpcMessageUtils.END_PATTERN, ""))))
245                 return true
246             } catch (e: Exception) {
247                 return false
248             }
249
250         }
251
252         fun getMsgId(message: String): String {
253             val matcher = MSG_ID_STRING_PATTERN.matcher(message)
254             if (matcher.find()) {
255                 return matcher.group(1)
256             }
257             return if (message.contains(RpcMessageUtils.HELLO)) {
258                 (-1).toString()
259             } else ""
260         }
261
262         fun validateChunkedFraming(reply: String): Boolean {
263             val matcher = CHUNKED_FRAMING_PATTERN.matcher(reply)
264             if (!matcher.matches()) {
265                 log.debug("Error Reply: {}", reply)
266                 return false
267             }
268             val chunkM = CHUNKED_SIZE_PATTERN.matcher(reply)
269             val chunks = ArrayList<MatchResult>()
270             var chunkdataStr = ""
271             while (chunkM.find()) {
272                 chunks.add(chunkM.toMatchResult())
273                 // extract chunk-data (and later) in bytes
274                 val bytes = Integer.parseInt(chunkM.group(1))
275                 val chunkdata = reply.substring(chunkM.end()).toByteArray(StandardCharsets.UTF_8)
276                 if (bytes > chunkdata.size) {
277                     log.debug("Error Reply - wrong chunk size {}", reply)
278                     return false
279                 }
280                 // convert (only) chunk-data part into String
281                 chunkdataStr = String(chunkdata, 0, bytes, StandardCharsets.UTF_8)
282                 // skip chunk-data part from next match
283                 chunkM.region(chunkM.end() + chunkdataStr.length, reply.length)
284             }
285             if (!CHUNKED_END_REGEX_PATTERN.equals(reply.substring(chunks[chunks.size - 1].end() + chunkdataStr.length))) {
286                 log.debug("Error Reply: {}", reply)
287                 return false
288             }
289             return true
290         }
291
292         fun createHelloString(capabilities: List<String>): String {
293             val helloMessage = StringBuilder()
294             helloMessage.append(RpcMessageUtils.XML_HEADER).append(NEW_LINE)
295             helloMessage.append("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">").append(NEW_LINE)
296             helloMessage.append("  <capabilities>").append(NEW_LINE)
297             if (capabilities.isNotEmpty()) {
298                 capabilities.forEach { cap ->
299                     helloMessage.append("    <capability>").append(cap).append("</capability>").append(NEW_LINE)
300                 }
301             }
302             helloMessage.append("  </capabilities>").append(NEW_LINE)
303             helloMessage.append("</hello>").append(NEW_LINE)
304             helloMessage.append(RpcMessageUtils.END_PATTERN)
305             return helloMessage.toString()
306         }
307
308         fun formatRPCRequest(request: String, messageId: String, deviceCapabilities: Set<String>): String {
309             var request = request
310             request = NetconfMessageUtils.formatNetconfMessage(deviceCapabilities, request)
311             request = NetconfMessageUtils.formatXmlHeader(request)
312             request = NetconfMessageUtils.formatRequestMessageId(request, messageId)
313
314             return request
315         }
316
317         /**
318          * Validate and format netconf message. - NC1.0 if no EOM sequence present on `message`,
319          * append. - NC1.1 chunk-encode given message unless it already is chunk encoded
320          *
321          * @param deviceCapabilities Set containing Device Capabilities
322          * @param message to format
323          * @return formated message
324          */
325         fun formatNetconfMessage(deviceCapabilities: Set<String>, message: String): String {
326             var message = message
327             if (deviceCapabilities.contains(RpcMessageUtils.NETCONF_11_CAPABILITY)) {
328                 message = formatChunkedMessage(message)
329             } else if (!message.endsWith(RpcMessageUtils.END_PATTERN)) {
330                 message = message + NEW_LINE + RpcMessageUtils.END_PATTERN
331             }
332             return message
333         }
334
335         /**
336          * Validate and format message according to chunked framing mechanism.
337          *
338          * @param message to format
339          * @return formated message
340          */
341         fun formatChunkedMessage(message: String): String {
342             var message = message
343             if (message.endsWith(RpcMessageUtils.END_PATTERN)) {
344                 // message given had Netconf 1.0 EOM pattern -> remove
345                 message = message.substring(0, message.length - RpcMessageUtils.END_PATTERN.length)
346             }
347             if (!message.startsWith(RpcMessageUtils.NEW_LINE + RpcMessageUtils.HASH)) {
348                 // chunk encode message
349                 message =
350                     (RpcMessageUtils.NEW_LINE + RpcMessageUtils.HASH + message.toByteArray(UTF_8).size + RpcMessageUtils.NEW_LINE + message + RpcMessageUtils.NEW_LINE + RpcMessageUtils.HASH + RpcMessageUtils.HASH
351                             + RpcMessageUtils.NEW_LINE)
352             }
353             return message
354         }
355
356         /**
357          * Ensures xml start directive/declaration appears in the `request`.
358          *
359          * @param request RPC request message
360          * @return XML RPC message
361          */
362         fun formatXmlHeader(request: String): String {
363             var request = request
364             if (!request.contains(RpcMessageUtils.XML_HEADER)) {
365                 if (request.startsWith(RpcMessageUtils.NEW_LINE + RpcMessageUtils.HASH)) {
366                     request =
367                         request.split("<".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0] + RpcMessageUtils.XML_HEADER + request.substring(
368                             request.split("<".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0].length)
369                 } else {
370                     request = RpcMessageUtils.XML_HEADER + "\n" + request
371                 }
372             }
373             return request
374         }
375
376         fun formatRequestMessageId(request: String, messageId: String): String {
377             var request = request
378             if (request.contains(RpcMessageUtils.MESSAGE_ID_STRING)) {
379                 request =
380                     request.replaceFirst((RpcMessageUtils.MESSAGE_ID_STRING + RpcMessageUtils.EQUAL + RpcMessageUtils.NUMBER_BETWEEN_QUOTES_MATCHER).toRegex(),
381                         RpcMessageUtils.MESSAGE_ID_STRING + RpcMessageUtils.EQUAL + RpcMessageUtils.QUOTE + messageId + RpcMessageUtils.QUOTE)
382             } else if (!request.contains(RpcMessageUtils.MESSAGE_ID_STRING) && !request.contains(
383                     RpcMessageUtils.HELLO)) {
384                 request = request.replaceFirst(RpcMessageUtils.END_OF_RPC_OPEN_TAG.toRegex(),
385                     RpcMessageUtils.QUOTE_SPACE + RpcMessageUtils.MESSAGE_ID_STRING + RpcMessageUtils.EQUAL + RpcMessageUtils.QUOTE + messageId + RpcMessageUtils.QUOTE + ">")
386             }
387             return updateRequestLength(request)
388         }
389
390         fun updateRequestLength(request: String): String {
391             if (request.contains(NEW_LINE + RpcMessageUtils.HASH + RpcMessageUtils.HASH + NEW_LINE)) {
392                 val oldLen =
393                     Integer.parseInt(request.split(RpcMessageUtils.HASH.toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[1].split(
394                         NEW_LINE.toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[0])
395                 val rpcWithEnding = request.substring(request.indexOf('<'))
396                 val firstBlock =
397                     request.split(RpcMessageUtils.MSGLEN_REGEX_PATTERN.toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[1].split(
398                         (NEW_LINE + RpcMessageUtils.HASH + RpcMessageUtils.HASH + NEW_LINE).toRegex()).dropLastWhile(
399                         { it.isEmpty() }).toTypedArray()[0]
400                 var newLen = 0
401                 newLen = firstBlock.toByteArray(UTF_8).size
402                 if (oldLen != newLen) {
403                     return NEW_LINE + RpcMessageUtils.HASH + newLen + NEW_LINE + rpcWithEnding
404                 }
405             }
406             return request
407         }
408
409         fun checkReply(reply: String?): Boolean {
410             return if (reply != null) {
411                 !reply.contains("rpc-error>") || reply.contains("ok/>")
412             } else false
413         }
414     }
415
416 }