-package org.commscope.tr069adapter.netconf.notification;\r
-\r
-import java.io.IOException;\r
-import java.io.StringWriter;\r
-import java.text.SimpleDateFormat;\r
-import java.util.Date;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.StringTokenizer;\r
-\r
-import javax.xml.parsers.DocumentBuilder;\r
-import javax.xml.parsers.DocumentBuilderFactory;\r
-import javax.xml.parsers.ParserConfigurationException;\r
-import javax.xml.transform.Transformer;\r
-import javax.xml.transform.TransformerFactory;\r
-import javax.xml.transform.dom.DOMSource;\r
-import javax.xml.transform.stream.StreamResult;\r
-\r
-import org.commscope.tr069adapter.acs.common.ParameterDTO;\r
-import org.commscope.tr069adapter.mapper.model.NetConfNotificationDTO;\r
-import org.commscope.tr069adapter.netconf.rpc.CreateSubscription;\r
-import org.opendaylight.netconf.api.NetconfMessage;\r
-import org.opendaylight.netconf.api.xml.XmlNetconfConstants;\r
-import org.opendaylight.netconf.api.xml.XmlUtil;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-import org.springframework.stereotype.Component;\r
-import org.w3c.dom.Document;\r
-import org.w3c.dom.Element;\r
-import org.xml.sax.SAXException;\r
-\r
-@Component\r
-public class NetConfSessionUtil {\r
-\r
- private static final Logger LOG = LoggerFactory.getLogger(NetConfSessionUtil.class);\r
-\r
- private static final String INDEX_STR = "index";\r
- private static final String INDEX_REGEX = "[0-9]{1,}";\r
- private static final String NS_URI = "urn:onf:otcc:wireless:yang:tr069-notification";\r
-\r
- public void sendNetConfNotification(NetConfNotificationDTO netConNotifDTO) {\r
- NetconfMessage netconfMessage = convertToNetConfMessage(netConNotifDTO);\r
- LOG.debug("Notification converted to NetConf format" + netconfMessage);\r
- CreateSubscription.sendNotification(netconfMessage, netConNotifDTO.getDeviceID());\r
- }\r
-\r
- private NetconfMessage convertToNetConfMessage(NetConfNotificationDTO netConNotifDTO) {\r
- try {\r
- String nameSpace = "";\r
- if (netConNotifDTO.getUri() != null) {\r
- nameSpace = netConNotifDTO.getUri();\r
- } else {\r
- nameSpace = NS_URI;\r
- }\r
- return new NetconfMessage(\r
- XmlUtil.readXmlToDocument(getNetconfResponseXML(netConNotifDTO, nameSpace)));\r
- } catch (SAXException | IOException e) {\r
- throw new IllegalArgumentException("Cannot parse notifications", e);\r
- }\r
- }\r
-\r
- private static String getNetconfResponseXML(NetConfNotificationDTO netConNotifDTO,\r
- String nameSpace) {\r
- if (netConNotifDTO == null || netConNotifDTO.getParameters().isEmpty()) {\r
- LOG.debug("There are no parameters found in the response.");\r
- return null;\r
- }\r
-\r
- List<ParameterDTO> parameters = netConNotifDTO.getParameters();\r
-\r
- String result = null;\r
- try {\r
- DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();\r
- DocumentBuilder docBuilder = docFactory.newDocumentBuilder();\r
- Document doc = docBuilder.newDocument();\r
-\r
- Map<String, Element> parentNodeMap = new HashMap<>();\r
- Element dataNode = null; // root of all nodes\r
-\r
- for (ParameterDTO paramDto : parameters) {\r
- String paramName = paramDto.getParamName();\r
- String paramValue = paramDto.getParamValue();\r
- StringTokenizer tokenizer = new StringTokenizer(paramName, ".");\r
- String parentNodeName = null;\r
- String parentNodeKey = null;\r
- Element parentNode = null;\r
- while (tokenizer.hasMoreElements()) {\r
- String nodeName = (String) tokenizer.nextElement();\r
- if (null == parentNodeName) { // construct first node or\r
- // Device node\r
- parentNodeName = nodeName;\r
- parentNodeKey = nodeName;\r
- // check if the node already exists in parentNodeMap\r
- parentNode = parentNodeMap.get(parentNodeKey);\r
- if (null == dataNode) {\r
- dataNode = parentNode;\r
- }\r
- continue;\r
- } else if (nodeName.matches(INDEX_REGEX)) { // construct\r
- // tabular and\r
- // index nodes\r
-\r
- // get parent tabular node from parent MAP\r
- parentNodeKey = parentNodeKey + "." + nodeName;\r
- Element node = parentNodeMap.get(parentNodeKey);\r
-\r
- // create a tabular parent node if doesn't exit in MAP\r
- if (null == node) {\r
- node = doc.createElement(parentNodeName);\r
- parentNodeMap.put(parentNodeKey, node);\r
-\r
- // update current tabular parent node.\r
- if (null != parentNode)\r
- parentNode.appendChild(node);\r
- else\r
- parentNode = node;\r
-\r
- // prepare and add index node to tabular parent node\r
- Element indexNode = doc.createElement(INDEX_STR);\r
- indexNode.setTextContent(nodeName);\r
- node.appendChild(indexNode);\r
- }\r
- parentNode = node; // move parent to child\r
- parentNodeName = nodeName;\r
- } else if (parentNodeName.matches(INDEX_REGEX)) { // move to\r
- // next\r
- // node\r
- // if\r
- // tabular\r
- // attribute\r
- // is\r
- // found\r
- parentNodeKey = parentNodeKey + "." + nodeName;\r
- parentNodeName = nodeName;\r
- } else {\r
- // construct intermediate nodes\r
- Element node = parentNodeMap.get(parentNodeKey);\r
- if (null == node) {\r
- if (null == dataNode) {\r
- node = doc.createElementNS(nameSpace, parentNodeName);\r
- dataNode = node;\r
- } else {\r
- node = doc.createElement(parentNodeName);\r
- }\r
- parentNodeMap.put(parentNodeKey, node);\r
- if (null != parentNode)\r
- parentNode.appendChild(node);\r
- }\r
- parentNodeKey = parentNodeKey + "." + nodeName;\r
- parentNodeName = nodeName;\r
- parentNode = node;\r
- }\r
- }\r
- // construct leaf node\r
- Element leafNode = doc.createElement(parentNodeName);\r
- leafNode.setTextContent(paramValue);\r
- parentNode.appendChild(leafNode);\r
- }\r
-\r
- if (null != dataNode) {\r
- final Element element = doc.createElement(XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME);\r
- final Element eventTime = doc.createElement(XmlNetconfConstants.EVENT_TIME);\r
- eventTime\r
- .setTextContent(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(new Date()));\r
- element.appendChild(element.getOwnerDocument().importNode(eventTime, true));\r
-\r
- if (netConNotifDTO.getNotificationType() != null) {\r
- final Element evtTypeElement =\r
- doc.createElementNS(nameSpace, netConNotifDTO.getNotificationType());\r
- evtTypeElement.appendChild(dataNode);\r
- element.appendChild(element.getOwnerDocument().importNode(evtTypeElement, true));\r
- } else {\r
- element.appendChild(element.getOwnerDocument().importNode(dataNode, true));\r
- }\r
-\r
- result = convertDocumentToString(element);\r
- }\r
- } catch (ParserConfigurationException pce) {\r
- pce.printStackTrace();\r
- }\r
-\r
- return result;\r
- }\r
-\r
- public static String convertDocumentToString(Element element) {\r
- String strxml = null;\r
- try {\r
- TransformerFactory transformerFactory = TransformerFactory.newInstance();\r
- Transformer transformer = transformerFactory.newTransformer();\r
- DOMSource source = new DOMSource(element);\r
- StreamResult result = new StreamResult(new StringWriter());\r
- transformer.transform(source, result);\r
- strxml = result.getWriter().toString();\r
- } catch (Exception e) {\r
- LOG.error("Error while converting Element to String" + e);\r
- }\r
- LOG.debug("Converted XML is : " + strxml);\r
- return strxml;\r
- }\r
-\r
-}\r
+/*
+ * ============LICENSE_START========================================================================
+ * ONAP : tr-069-adapter
+ * =================================================================================================
+ * Copyright (C) 2020 CommScope Inc Intellectual Property.
+ * =================================================================================================
+ * This tr-069-adapter software file is distributed by CommScope Inc 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
+ *
+ * This file 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.
+ * ===============LICENSE_END=======================================================================
+ */
+
+package org.commscope.tr069adapter.netconf.notification;
+
+import org.commscope.tr069adapter.mapper.model.NetConfNotificationDTO;
+import org.commscope.tr069adapter.netconf.error.NetconfNotificationException;
+import org.commscope.tr069adapter.netconf.rpc.CreateSubscription;
+import org.opendaylight.netconf.api.NetconfMessage;
+import org.opendaylight.netconf.api.xml.XmlUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+@Component
+public class NetConfSessionUtil {
+
+ private static final Logger LOG = LoggerFactory.getLogger(NetConfSessionUtil.class);
+
+ public void sendNetConfNotification(NetConfNotificationDTO netConNotifDTO)
+ throws NetconfNotificationException {
+ NetconfMessage netconfMessage = convertToNetConfMessage(netConNotifDTO);
+ CreateSubscription.sendNotification(netconfMessage, netConNotifDTO.getDeviceID());
+ }
+
+ private NetconfMessage convertToNetConfMessage(NetConfNotificationDTO netConNotifDTO)
+ throws NetconfNotificationException {
+ try {
+ return new NetconfMessage(XmlUtil.readXmlToDocument(netConNotifDTO.getNotificaiton()));
+ } catch (Exception e) {
+ LOG.error("Error while converting to netcon notification ");
+ throw new NetconfNotificationException("Cannot parse notifications", e);
+ }
+ }
+}