2 * ============LICENSE_START========================================================================
3 * ONAP : tr-069-adapter
4 * =================================================================================================
5 * Copyright (C) 2020 CommScope Inc Intellectual Property.
6 * =================================================================================================
7 * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9 * may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14 * either express or implied. See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ===============LICENSE_END=======================================================================
19 package org.commscope.tr069adapter.netconf.notification;
21 import java.io.IOException;
22 import java.io.StringWriter;
23 import javax.xml.transform.Transformer;
24 import javax.xml.transform.TransformerFactory;
25 import javax.xml.transform.dom.DOMSource;
26 import javax.xml.transform.stream.StreamResult;
27 import org.commscope.tr069adapter.mapper.model.NetConfNotificationDTO;
28 import org.commscope.tr069adapter.netconf.rpc.CreateSubscription;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.api.xml.XmlUtil;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.stereotype.Component;
34 import org.w3c.dom.Element;
35 import org.xml.sax.SAXException;
38 public class NetConfSessionUtil {
40 private static final Logger LOG = LoggerFactory.getLogger(NetConfSessionUtil.class);
42 public void sendNetConfNotification(NetConfNotificationDTO netConNotifDTO) {
43 NetconfMessage netconfMessage = convertToNetConfMessage(netConNotifDTO);
44 LOG.debug("Notification converted to NetConf format" + netconfMessage);
45 CreateSubscription.sendNotification(netconfMessage, netConNotifDTO.getDeviceID());
48 private NetconfMessage convertToNetConfMessage(NetConfNotificationDTO netConNotifDTO) {
50 return new NetconfMessage(XmlUtil.readXmlToDocument(netConNotifDTO.getNotificaiton()));
51 } catch (SAXException | IOException e) {
52 throw new IllegalArgumentException("Cannot parse notifications", e);
56 public static String convertDocumentToString(Element element) {
59 TransformerFactory transformerFactory = TransformerFactory.newInstance();
60 Transformer transformer = transformerFactory.newTransformer();
61 DOMSource source = new DOMSource(element);
62 StreamResult result = new StreamResult(new StringWriter());
63 transformer.transform(source, result);
64 strxml = result.getWriter().toString();
65 } catch (Exception e) {
66 LOG.error("Error while converting Element to String" + e);
68 LOG.debug("Converted XML is : " + strxml);