70965a077cb4b40639df2ca37308f8782b11a549
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / notification / NetConfSessionUtil.java
1 /*
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=======================================================================
17  */
18
19 package org.commscope.tr069adapter.netconf.notification;
20
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;
36
37 @Component
38 public class NetConfSessionUtil {
39
40   private static final Logger LOG = LoggerFactory.getLogger(NetConfSessionUtil.class);
41
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());
46   }
47
48   private NetconfMessage convertToNetConfMessage(NetConfNotificationDTO netConNotifDTO) {
49     try {
50       return new NetconfMessage(XmlUtil.readXmlToDocument(netConNotifDTO.getNotificaiton()));
51     } catch (SAXException | IOException e) {
52       throw new IllegalArgumentException("Cannot parse notifications", e);
53     }
54   }
55
56   public static String convertDocumentToString(Element element) {
57     String strxml = null;
58     try {
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);
67     }
68     LOG.debug("Converted XML is : " + strxml);
69     return strxml;
70   }
71
72 }