Initial source code
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / acs / impl / ACSNotificationHandlerImpl.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.acs.impl;\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.DeviceRPCResponse;\r
26 import org.commscope.tr069adapter.acs.common.InformType;\r
27 import org.commscope.tr069adapter.acs.common.ParameterDTO;\r
28 import org.commscope.tr069adapter.acs.common.dto.TR069InformType;\r
29 import org.commscope.tr069adapter.acs.common.inform.BootInform;\r
30 import org.commscope.tr069adapter.acs.common.inform.BootstrapInform;\r
31 import org.commscope.tr069adapter.acs.common.inform.PeriodicInform;\r
32 import org.commscope.tr069adapter.acs.common.inform.ValueChangeInform;\r
33 import org.commscope.tr069adapter.mapper.MOMetaData;\r
34 import org.commscope.tr069adapter.mapper.MapperConfigProperties;\r
35 import org.commscope.tr069adapter.mapper.acs.ACSNotificationHandler;\r
36 import org.commscope.tr069adapter.mapper.model.NetConfServerDetails;\r
37 import org.commscope.tr069adapter.mapper.model.NetconfServerManagementError;\r
38 import org.commscope.tr069adapter.mapper.netconf.NetConfNotificationSender;\r
39 import org.commscope.tr069adapter.mapper.netconf.NetConfServerManager;\r
40 import org.commscope.tr069adapter.mapper.sync.SynchronizedRequestHandler;\r
41 import org.commscope.tr069adapter.mapper.util.MOMetaDataUtil;\r
42 import org.commscope.tr069adapter.mapper.ves.VESNotificationSender;\r
43 import org.slf4j.Logger;\r
44 import org.slf4j.LoggerFactory;\r
45 import org.springframework.beans.factory.annotation.Autowired;\r
46 import org.springframework.stereotype.Component;\r
47 \r
48 @Component\r
49 public class ACSNotificationHandlerImpl implements ACSNotificationHandler {\r
50 \r
51   private static final Logger logger = LoggerFactory.getLogger(ACSNotificationHandlerImpl.class);\r
52 \r
53   @Autowired\r
54   SynchronizedRequestHandler syncHandler;\r
55 \r
56   @Autowired\r
57   MOMetaDataUtil metaDataUtil;\r
58 \r
59   @Autowired\r
60   PnPPreProvisioningHandler pnpPreProvisioningHandler;\r
61 \r
62   @Autowired\r
63   VESNotificationSender vesnotiSender;\r
64 \r
65   @Autowired\r
66   NetConfNotificationSender notiSender;\r
67 \r
68   @Autowired\r
69   MapperConfigProperties config;\r
70 \r
71   @Autowired\r
72   NetConfServerManager netconfManager;\r
73 \r
74   @Override\r
75   public void handleOperationResponse(DeviceRPCResponse opResult) {\r
76     opResult.getOperationResponse().setParameterDTOs(\r
77         filterUnsupportedParameters(opResult.getOperationResponse().getParameterDTOs()));\r
78     syncHandler.notifyResult(opResult);\r
79   }\r
80 \r
81   @Override\r
82   public void handleNotification(DeviceInform notification) {\r
83     boolean isAlarmVC = isAlarmVC(notification);\r
84 \r
85     if (notification instanceof BootstrapInform) {\r
86       logger.info("BootStrap notification received");\r
87       BootstrapInform bootstrapNotification = (BootstrapInform) notification;\r
88       // send request to create the netconf server instance for the bootstrap device\r
89       // id\r
90       NetConfServerDetails serverInfo = createNtConfServer(bootstrapNotification);\r
91       if (serverInfo == null)\r
92         return;\r
93 \r
94       vesnotiSender.sendNotification(bootstrapNotification, serverInfo);\r
95       BootstrapInform bsInform =\r
96           getDeviceBootStrapNotification(bootstrapNotification, TR069InformType.BOOTSTRAP);\r
97       if (bootstrapNotification.getValueChangeNotification() != null) {\r
98         logger.info("Bootstrap notification received along with VC");\r
99         ValueChangeInform vcInform =\r
100             getDeviceValueChangeNotification(bootstrapNotification, TR069InformType.VALUECHANGE);\r
101         processVCNotification(vcInform, isAlarmVC);\r
102       }\r
103       notiSender.sendNotification(bsInform);\r
104     } else if (notification instanceof BootInform) {\r
105       logger.info("Boot notification received");\r
106       BootInform bootNotification = (BootInform) notification;\r
107       BootInform bInform = getDeviceBootNotification(bootNotification, TR069InformType.BOOT);\r
108       if (bootNotification.getValueChangeNotification() != null) {\r
109         logger.info("Boot notification received along with VC");\r
110         ValueChangeInform vcInform =\r
111             getDeviceValueChangeNotification(bootNotification, TR069InformType.VALUECHANGE);\r
112         processVCNotification(vcInform, isAlarmVC);\r
113       }\r
114       notiSender.sendNotification(bInform);\r
115     } else if (notification instanceof PeriodicInform) {\r
116       PeriodicInform pINotificaiton = (PeriodicInform) notification;\r
117       vesnotiSender.sendNotification(pINotificaiton, null);\r
118       notiSender.sendNotification(pINotificaiton);\r
119       logger.info("VC notification received");\r
120     } else if (notification instanceof ValueChangeInform) {\r
121       ValueChangeInform valueChgNotificaiton = (ValueChangeInform) notification;\r
122       processVCNotification(valueChgNotificaiton, isAlarmVC);\r
123     }\r
124 \r
125     pnpPreProvisioningHandler.onDeviceNotification(notification);\r
126   }\r
127 \r
128   private NetConfServerDetails createNtConfServer(BootstrapInform bootstrapNotification) {\r
129     String eNodeBName = pnpPreProvisioningHandler\r
130         .getEnodeBName(bootstrapNotification.getDeviceDetails().getDeviceId());\r
131     if (eNodeBName == null)\r
132       eNodeBName = bootstrapNotification.getDeviceDetails().getDeviceId();\r
133     NetConfServerDetails serverInfo = netconfManager\r
134         .createNetconfServer(bootstrapNotification.getDeviceDetails().getDeviceId(), eNodeBName);\r
135     if (serverInfo != null && !NetconfServerManagementError.SUCCESS.equals(serverInfo.getError())) {\r
136       logger.error("Failed to handle bootstrap notification. Server INFO: {}", serverInfo);\r
137       logger.error("Failed to create the netconf server for device ID: {}  Error: {}",\r
138           bootstrapNotification.getDeviceDetails().getDeviceId(), serverInfo.getError());\r
139       return null;\r
140     } else if (serverInfo == null) {\r
141       logger.error(\r
142           "Failed to handle bootstrap notification. Failed to create netconf server. serverInfo is null");\r
143       return null;\r
144     }\r
145     return serverInfo;\r
146   }\r
147 \r
148   private void processVCNotification(ValueChangeInform valueChgNotificaiton, boolean isAlarmVC) {\r
149     if (isAlarmVC) {\r
150       logger.debug("Alarm VC received forwarding to VES Collector");\r
151       vesnotiSender.sendNotification(valueChgNotificaiton, null);\r
152     } else {\r
153       logger.info("VC notification received");\r
154       notiSender.sendNotification(valueChgNotificaiton);\r
155     }\r
156   }\r
157 \r
158   private boolean isAlarmVC(DeviceInform notification) {\r
159     if (null != notification && null != notification.getParameters()) {\r
160       for (ParameterDTO param : notification.getParameters()) {\r
161         if (param.getParamName().matches(config.getAlarmMORegex())) {\r
162           logger.debug("This VC contains alarm MOs");\r
163           return true;\r
164         }\r
165       }\r
166     }\r
167     return false;\r
168   }\r
169 \r
170   public List<ParameterDTO> filterUnsupportedParameters(List<ParameterDTO> parameters) {\r
171     List<ParameterDTO> result = new ArrayList<>();\r
172     if (null != parameters) {\r
173       for (ParameterDTO param : parameters) {\r
174         MOMetaData metaData = metaDataUtil.getMetaDataByTR69Name(param.getParamName());\r
175         if (null != metaData) {\r
176           result.add(param);\r
177         }\r
178       }\r
179     }\r
180     return result;\r
181   }\r
182 \r
183   public static BootstrapInform getDeviceBootStrapNotification(DeviceInform devNotification,\r
184       TR069InformType notificationType) {\r
185     BootstrapInform bsInform = new BootstrapInform();\r
186     List<InformType> informTypeList = new ArrayList<>();\r
187     informTypeList.add(notificationType);\r
188     bsInform.setDeviceDetails(devNotification.getDeviceDetails());\r
189     bsInform.setInformTypeList(informTypeList);\r
190     List<ParameterDTO> paramList = new ArrayList<>();\r
191     for (ParameterDTO param : devNotification.getParameters()) {\r
192       paramList.add(new ParameterDTO(param.getParamName(), param.getParamValue()));\r
193     }\r
194     bsInform.setParameters(paramList);\r
195     return bsInform;\r
196   }\r
197 \r
198   public static BootInform getDeviceBootNotification(DeviceInform devNotification,\r
199       TR069InformType notificationType) {\r
200     BootInform bInform = new BootInform();\r
201     List<InformType> informTypeList = new ArrayList<>();\r
202     informTypeList.add(notificationType);\r
203     bInform.setDeviceDetails(devNotification.getDeviceDetails());\r
204     bInform.setInformTypeList(informTypeList);\r
205     List<ParameterDTO> paramList = new ArrayList<>();\r
206     for (ParameterDTO param : devNotification.getParameters()) {\r
207       paramList.add(new ParameterDTO(param.getParamName(), param.getParamValue()));\r
208     }\r
209     bInform.setParameters(paramList);\r
210     return bInform;\r
211   }\r
212 \r
213   public static ValueChangeInform getDeviceValueChangeNotification(DeviceInform devNotification,\r
214       TR069InformType notificationType) {\r
215     ValueChangeInform devValChangeNotif = new ValueChangeInform();\r
216     List<InformType> informTypeList = new ArrayList<>();\r
217     informTypeList.add(notificationType);\r
218     devValChangeNotif.setDeviceDetails(devNotification.getDeviceDetails());\r
219     devValChangeNotif.setInformTypeList(informTypeList);\r
220     List<ParameterDTO> paramList = new ArrayList<>();\r
221     for (ParameterDTO param : devNotification.getParameters()) {\r
222       paramList.add(new ParameterDTO(param.getParamName(), param.getParamValue()));\r
223     }\r
224     devValChangeNotif.setParameters(paramList);\r
225     devValChangeNotif.setExternalIPAddress(getExternalIPAddress(devNotification.getParameters()));\r
226     return devValChangeNotif;\r
227   }\r
228 \r
229   private static String getExternalIPAddress(List<ParameterDTO> params) {\r
230     ParameterDTO[] nbiParam = params.toArray(new ParameterDTO[params.size()]);\r
231     String externalIpAddress = "";\r
232     boolean isIPv6 = isIPv6Enabled(nbiParam);\r
233 \r
234     for (int index1 = 0; index1 < nbiParam.length; index1++) {\r
235 \r
236       if (isIPv6) {\r
237         if (nbiParam[index1].getParamName().contains("IPv6Address")\r
238             || nbiParam[index1].getParamName().contains(".1.IPInterfaceIPAddress")) {\r
239           externalIpAddress = nbiParam[index1].getParamValue();\r
240           logger.debug("device communicating is with IPV6 address");\r
241         }\r
242       } else {\r
243         if (nbiParam[index1].getParamName().contains("IPv4Address")\r
244             || nbiParam[index1].getParamName().contains("ExternalIPAddress")\r
245             || nbiParam[index1].getParamName().contains(".1.IPInterfaceIPAddress")) {\r
246           externalIpAddress = nbiParam[index1].getParamValue();\r
247         }\r
248       }\r
249       if (externalIpAddress.trim().length() > 0)\r
250         break;\r
251     }\r
252     return externalIpAddress;\r
253   }\r
254 \r
255   private static boolean isIPv6Enabled(ParameterDTO[] nbiParam) {\r
256     boolean isIPv6 = false;\r
257     for (int index1 = 0; index1 < nbiParam.length; index1++) {\r
258       if (nbiParam[index1].getParamName().contains("IPv6Enable")\r
259           && nbiParam[index1].getParamValue().equalsIgnoreCase("1")) {\r
260         isIPv6 = true;\r
261         break;\r
262       }\r
263     }\r
264     return isIPv6;\r
265   }\r
266 }\r