VES Heartbeat and Software Management Feature
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / netconf / NetConfNotificationSender.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 package org.commscope.tr069adapter.mapper.netconf;\r
20 \r
21 import java.util.ArrayList;\r
22 import java.util.List;\r
23 \r
24 import org.commscope.tr069adapter.acs.common.DeviceInform;\r
25 import org.commscope.tr069adapter.acs.common.ParameterDTO;\r
26 import org.commscope.tr069adapter.mapper.MOMetaData;\r
27 import org.commscope.tr069adapter.mapper.MapperConfigProperties;\r
28 import org.commscope.tr069adapter.mapper.model.NetConfNotificationDTO;\r
29 import org.commscope.tr069adapter.mapper.util.MOMetaDataUtil;\r
30 import org.slf4j.Logger;\r
31 import org.slf4j.LoggerFactory;\r
32 import org.springframework.beans.factory.annotation.Autowired;\r
33 import org.springframework.http.ResponseEntity;\r
34 import org.springframework.stereotype.Component;\r
35 import org.springframework.web.client.RestTemplate;\r
36 \r
37 @Component\r
38 public class NetConfNotificationSender {\r
39 \r
40   private static final Logger LOG = LoggerFactory.getLogger(NetConfNotificationSender.class);\r
41   private static final String BOOLEAN_TRUE_VALUE = "1";\r
42   private static final String BOOLEAN_FALSE_VALUE = "0";\r
43   private static final String BOOLEAN_DATA_TYPE = "boolean";\r
44 \r
45   @Autowired\r
46   MapperConfigProperties config;\r
47 \r
48   @Autowired\r
49   MOMetaDataUtil metaDataUtil;\r
50 \r
51   @Autowired\r
52   RestTemplate restTemplate;\r
53 \r
54   public ResponseEntity sendNotification(DeviceInform deviceInform) {\r
55     ResponseEntity response = null;\r
56     final String uri = getUri();\r
57     LOG.debug("Posting notification to netconf server {}", uri);\r
58 \r
59     try {\r
60       LOG.debug("deviceInform : {} {}", deviceInform.getInformTypeList(),\r
61           deviceInform.getParameters());\r
62       NetConfNotificationDTO netConfDTO =\r
63           new NetConfNotificationDTO(deviceInform.getDeviceDetails().getDeviceId(),\r
64               deviceInform.getInformType().toString(), false);\r
65       netConfDTO.setParameters(deviceInform.getParameters());\r
66       convertTR069ToNetConfParams(netConfDTO);\r
67       LOG.debug("Posting notification to netconf server");\r
68       response = restTemplate.postForObject(uri, netConfDTO, ResponseEntity.class);\r
69       LOG.debug("Posting notification to netconf server completed ");\r
70     } catch (Exception e) {\r
71       LOG.error("Exception while sending the notification.", e);\r
72     }\r
73     return response;\r
74   }\r
75 \r
76   public ResponseEntity sendCustomNotification(NetConfNotificationDTO netConfDTO) {\r
77     ResponseEntity response = null;\r
78     final String uri = getUri();\r
79     LOG.debug("Posting custom notification to netconf server " + uri);\r
80     try {\r
81       response = restTemplate.postForObject(uri, netConfDTO, ResponseEntity.class);\r
82       LOG.debug("Posting custom notification to netconf server sucessfull");\r
83     } catch (Exception e) {\r
84       LOG.error("Exception while sending the custom notification.", e.toString());\r
85     }\r
86     return response;\r
87   }\r
88 \r
89   private void convertTR069ToNetConfParams(NetConfNotificationDTO netConfDTO) {\r
90     List<ParameterDTO> removeList = new ArrayList<>();\r
91     if (null != netConfDTO) {\r
92       for (ParameterDTO param : netConfDTO.getParameters()) {\r
93         if (param.getParamValue() == null || param.getParamValue().trim().length() <= 0) {\r
94           continue;\r
95         }\r
96         handleBooleanParameters(param);\r
97         if (null != param.getParamName()) {\r
98           String netConfMOName =\r
99               metaDataUtil.getNetconfNameByTR69NameWithIndexes(param.getParamName());\r
100           if (null != netConfMOName)\r
101             param.setParamName(netConfMOName);\r
102           else\r
103             removeList.add(param); // unknown parameter found.\r
104         }\r
105       }\r
106       netConfDTO.getParameters().removeAll(removeList);\r
107     }\r
108   }\r
109 \r
110   private void handleBooleanParameters(ParameterDTO param) {\r
111     MOMetaData metaData = metaDataUtil.getMetaDataByTR69Name(param.getParamName());\r
112     if (null != metaData && BOOLEAN_DATA_TYPE.equalsIgnoreCase(metaData.getDataType())) {\r
113       if (BOOLEAN_TRUE_VALUE.equalsIgnoreCase(param.getParamValue().trim())) {\r
114         param.setParamValue(Boolean.TRUE.toString());\r
115       } else if (BOOLEAN_FALSE_VALUE.equalsIgnoreCase(param.getParamValue().trim())) {\r
116         param.setParamValue(Boolean.FALSE.toString());\r
117       }\r
118     }\r
119   }\r
120 \r
121   private String getUri() {\r
122     return config.getNbiNotificationUri();\r
123   }\r
124 \r
125 }\r