Initial source code
[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.util.MOMetaDataUtil;\r
29 import org.slf4j.Logger;\r
30 import org.slf4j.LoggerFactory;\r
31 import org.springframework.beans.factory.annotation.Autowired;\r
32 import org.springframework.http.ResponseEntity;\r
33 import org.springframework.stereotype.Component;\r
34 import org.springframework.web.client.RestTemplate;\r
35 \r
36 @Component\r
37 public class NetConfNotificationSender {\r
38 \r
39   private static final Logger LOG = LoggerFactory.getLogger(NetConfNotificationSender.class);\r
40   private static final String BOOLEAN_TRUE_VALUE = "1";\r
41   private static final String BOOLEAN_FALSE_VALUE = "0";\r
42   private static final String BOOLEAN_DATA_TYPE = "boolean";\r
43 \r
44   @Autowired\r
45   MapperConfigProperties config;\r
46 \r
47   @Autowired\r
48   MOMetaDataUtil metaDataUtil;\r
49 \r
50   @Autowired\r
51   RestTemplate restTemplate;\r
52 \r
53   public ResponseEntity sendNotification(DeviceInform deviceInform) {\r
54     ResponseEntity response = null;\r
55     final String uri = getUri();\r
56     LOG.debug("Posting notification to netconf server {}", uri);\r
57 \r
58     try {\r
59       LOG.debug("deviceInform : {} {}", deviceInform.getInformTypeList(),\r
60           deviceInform.getParameters());\r
61       convertTR069ToNetConfParams(deviceInform);\r
62       LOG.debug("Posting notification to netconf server");\r
63       response = restTemplate.postForObject(uri, deviceInform, ResponseEntity.class);\r
64       LOG.debug("Posting notification to netconf server completed ");\r
65     } catch (Exception e) {\r
66       LOG.error("Exception while sending the notification.", e);\r
67     }\r
68     return response;\r
69   }\r
70 \r
71   private void convertTR069ToNetConfParams(DeviceInform deviceInform) {\r
72     List<ParameterDTO> removeList = new ArrayList<>();\r
73     if (null != deviceInform) {\r
74       for (ParameterDTO param : deviceInform.getParameters()) {\r
75         if (param.getParamValue() == null || param.getParamValue().trim().length() <= 0) {\r
76           continue;\r
77         }\r
78         handleBooleanParameters(param);\r
79         if (null != param.getParamName()) {\r
80           String netConfMOName =\r
81               metaDataUtil.getNetconfNameByTR69NameWithIndexes(param.getParamName());\r
82           if (null != netConfMOName)\r
83             param.setParamName(netConfMOName);\r
84           else\r
85             removeList.add(param); // unknown parameter found.\r
86         }\r
87       }\r
88       deviceInform.getParameters().removeAll(removeList); // remove unknown\r
89       // parameters\r
90     }\r
91   }\r
92 \r
93   private void handleBooleanParameters(ParameterDTO param) {\r
94     MOMetaData metaData = metaDataUtil.getMetaDataByTR69Name(param.getParamName());\r
95     if (null != metaData && BOOLEAN_DATA_TYPE.equalsIgnoreCase(metaData.getDataType())) {\r
96       if (BOOLEAN_TRUE_VALUE.equalsIgnoreCase(param.getParamValue().trim())) {\r
97         param.setParamValue(Boolean.TRUE.toString());\r
98       } else if (BOOLEAN_FALSE_VALUE.equalsIgnoreCase(param.getParamValue().trim())) {\r
99         param.setParamValue(Boolean.FALSE.toString());\r
100       }\r
101     }\r
102   }\r
103 \r
104   private String getUri() {\r
105     return config.getNbiNotificationUri();\r
106   }\r
107 \r
108 }\r