Initial source code
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / acs / impl / ACSNotificationHandlerImpl.java
diff --git a/mapper/src/main/java/org/commscope/tr069adapter/mapper/acs/impl/ACSNotificationHandlerImpl.java b/mapper/src/main/java/org/commscope/tr069adapter/mapper/acs/impl/ACSNotificationHandlerImpl.java
new file mode 100644 (file)
index 0000000..693f788
--- /dev/null
@@ -0,0 +1,266 @@
+/*\r
+ * ============LICENSE_START========================================================================\r
+ * ONAP : tr-069-adapter\r
+ * =================================================================================================\r
+ * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
+ * =================================================================================================\r
+ * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
+ * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
+ * may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
+ * either express or implied. See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ===============LICENSE_END=======================================================================\r
+ */\r
+\r
+package org.commscope.tr069adapter.mapper.acs.impl;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.commscope.tr069adapter.acs.common.DeviceInform;\r
+import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;\r
+import org.commscope.tr069adapter.acs.common.InformType;\r
+import org.commscope.tr069adapter.acs.common.ParameterDTO;\r
+import org.commscope.tr069adapter.acs.common.dto.TR069InformType;\r
+import org.commscope.tr069adapter.acs.common.inform.BootInform;\r
+import org.commscope.tr069adapter.acs.common.inform.BootstrapInform;\r
+import org.commscope.tr069adapter.acs.common.inform.PeriodicInform;\r
+import org.commscope.tr069adapter.acs.common.inform.ValueChangeInform;\r
+import org.commscope.tr069adapter.mapper.MOMetaData;\r
+import org.commscope.tr069adapter.mapper.MapperConfigProperties;\r
+import org.commscope.tr069adapter.mapper.acs.ACSNotificationHandler;\r
+import org.commscope.tr069adapter.mapper.model.NetConfServerDetails;\r
+import org.commscope.tr069adapter.mapper.model.NetconfServerManagementError;\r
+import org.commscope.tr069adapter.mapper.netconf.NetConfNotificationSender;\r
+import org.commscope.tr069adapter.mapper.netconf.NetConfServerManager;\r
+import org.commscope.tr069adapter.mapper.sync.SynchronizedRequestHandler;\r
+import org.commscope.tr069adapter.mapper.util.MOMetaDataUtil;\r
+import org.commscope.tr069adapter.mapper.ves.VESNotificationSender;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class ACSNotificationHandlerImpl implements ACSNotificationHandler {\r
+\r
+  private static final Logger logger = LoggerFactory.getLogger(ACSNotificationHandlerImpl.class);\r
+\r
+  @Autowired\r
+  SynchronizedRequestHandler syncHandler;\r
+\r
+  @Autowired\r
+  MOMetaDataUtil metaDataUtil;\r
+\r
+  @Autowired\r
+  PnPPreProvisioningHandler pnpPreProvisioningHandler;\r
+\r
+  @Autowired\r
+  VESNotificationSender vesnotiSender;\r
+\r
+  @Autowired\r
+  NetConfNotificationSender notiSender;\r
+\r
+  @Autowired\r
+  MapperConfigProperties config;\r
+\r
+  @Autowired\r
+  NetConfServerManager netconfManager;\r
+\r
+  @Override\r
+  public void handleOperationResponse(DeviceRPCResponse opResult) {\r
+    opResult.getOperationResponse().setParameterDTOs(\r
+        filterUnsupportedParameters(opResult.getOperationResponse().getParameterDTOs()));\r
+    syncHandler.notifyResult(opResult);\r
+  }\r
+\r
+  @Override\r
+  public void handleNotification(DeviceInform notification) {\r
+    boolean isAlarmVC = isAlarmVC(notification);\r
+\r
+    if (notification instanceof BootstrapInform) {\r
+      logger.info("BootStrap notification received");\r
+      BootstrapInform bootstrapNotification = (BootstrapInform) notification;\r
+      // send request to create the netconf server instance for the bootstrap device\r
+      // id\r
+      NetConfServerDetails serverInfo = createNtConfServer(bootstrapNotification);\r
+      if (serverInfo == null)\r
+        return;\r
+\r
+      vesnotiSender.sendNotification(bootstrapNotification, serverInfo);\r
+      BootstrapInform bsInform =\r
+          getDeviceBootStrapNotification(bootstrapNotification, TR069InformType.BOOTSTRAP);\r
+      if (bootstrapNotification.getValueChangeNotification() != null) {\r
+        logger.info("Bootstrap notification received along with VC");\r
+        ValueChangeInform vcInform =\r
+            getDeviceValueChangeNotification(bootstrapNotification, TR069InformType.VALUECHANGE);\r
+        processVCNotification(vcInform, isAlarmVC);\r
+      }\r
+      notiSender.sendNotification(bsInform);\r
+    } else if (notification instanceof BootInform) {\r
+      logger.info("Boot notification received");\r
+      BootInform bootNotification = (BootInform) notification;\r
+      BootInform bInform = getDeviceBootNotification(bootNotification, TR069InformType.BOOT);\r
+      if (bootNotification.getValueChangeNotification() != null) {\r
+        logger.info("Boot notification received along with VC");\r
+        ValueChangeInform vcInform =\r
+            getDeviceValueChangeNotification(bootNotification, TR069InformType.VALUECHANGE);\r
+        processVCNotification(vcInform, isAlarmVC);\r
+      }\r
+      notiSender.sendNotification(bInform);\r
+    } else if (notification instanceof PeriodicInform) {\r
+      PeriodicInform pINotificaiton = (PeriodicInform) notification;\r
+      vesnotiSender.sendNotification(pINotificaiton, null);\r
+      notiSender.sendNotification(pINotificaiton);\r
+      logger.info("VC notification received");\r
+    } else if (notification instanceof ValueChangeInform) {\r
+      ValueChangeInform valueChgNotificaiton = (ValueChangeInform) notification;\r
+      processVCNotification(valueChgNotificaiton, isAlarmVC);\r
+    }\r
+\r
+    pnpPreProvisioningHandler.onDeviceNotification(notification);\r
+  }\r
+\r
+  private NetConfServerDetails createNtConfServer(BootstrapInform bootstrapNotification) {\r
+    String eNodeBName = pnpPreProvisioningHandler\r
+        .getEnodeBName(bootstrapNotification.getDeviceDetails().getDeviceId());\r
+    if (eNodeBName == null)\r
+      eNodeBName = bootstrapNotification.getDeviceDetails().getDeviceId();\r
+    NetConfServerDetails serverInfo = netconfManager\r
+        .createNetconfServer(bootstrapNotification.getDeviceDetails().getDeviceId(), eNodeBName);\r
+    if (serverInfo != null && !NetconfServerManagementError.SUCCESS.equals(serverInfo.getError())) {\r
+      logger.error("Failed to handle bootstrap notification. Server INFO: {}", serverInfo);\r
+      logger.error("Failed to create the netconf server for device ID: {}  Error: {}",\r
+          bootstrapNotification.getDeviceDetails().getDeviceId(), serverInfo.getError());\r
+      return null;\r
+    } else if (serverInfo == null) {\r
+      logger.error(\r
+          "Failed to handle bootstrap notification. Failed to create netconf server. serverInfo is null");\r
+      return null;\r
+    }\r
+    return serverInfo;\r
+  }\r
+\r
+  private void processVCNotification(ValueChangeInform valueChgNotificaiton, boolean isAlarmVC) {\r
+    if (isAlarmVC) {\r
+      logger.debug("Alarm VC received forwarding to VES Collector");\r
+      vesnotiSender.sendNotification(valueChgNotificaiton, null);\r
+    } else {\r
+      logger.info("VC notification received");\r
+      notiSender.sendNotification(valueChgNotificaiton);\r
+    }\r
+  }\r
+\r
+  private boolean isAlarmVC(DeviceInform notification) {\r
+    if (null != notification && null != notification.getParameters()) {\r
+      for (ParameterDTO param : notification.getParameters()) {\r
+        if (param.getParamName().matches(config.getAlarmMORegex())) {\r
+          logger.debug("This VC contains alarm MOs");\r
+          return true;\r
+        }\r
+      }\r
+    }\r
+    return false;\r
+  }\r
+\r
+  public List<ParameterDTO> filterUnsupportedParameters(List<ParameterDTO> parameters) {\r
+    List<ParameterDTO> result = new ArrayList<>();\r
+    if (null != parameters) {\r
+      for (ParameterDTO param : parameters) {\r
+        MOMetaData metaData = metaDataUtil.getMetaDataByTR69Name(param.getParamName());\r
+        if (null != metaData) {\r
+          result.add(param);\r
+        }\r
+      }\r
+    }\r
+    return result;\r
+  }\r
+\r
+  public static BootstrapInform getDeviceBootStrapNotification(DeviceInform devNotification,\r
+      TR069InformType notificationType) {\r
+    BootstrapInform bsInform = new BootstrapInform();\r
+    List<InformType> informTypeList = new ArrayList<>();\r
+    informTypeList.add(notificationType);\r
+    bsInform.setDeviceDetails(devNotification.getDeviceDetails());\r
+    bsInform.setInformTypeList(informTypeList);\r
+    List<ParameterDTO> paramList = new ArrayList<>();\r
+    for (ParameterDTO param : devNotification.getParameters()) {\r
+      paramList.add(new ParameterDTO(param.getParamName(), param.getParamValue()));\r
+    }\r
+    bsInform.setParameters(paramList);\r
+    return bsInform;\r
+  }\r
+\r
+  public static BootInform getDeviceBootNotification(DeviceInform devNotification,\r
+      TR069InformType notificationType) {\r
+    BootInform bInform = new BootInform();\r
+    List<InformType> informTypeList = new ArrayList<>();\r
+    informTypeList.add(notificationType);\r
+    bInform.setDeviceDetails(devNotification.getDeviceDetails());\r
+    bInform.setInformTypeList(informTypeList);\r
+    List<ParameterDTO> paramList = new ArrayList<>();\r
+    for (ParameterDTO param : devNotification.getParameters()) {\r
+      paramList.add(new ParameterDTO(param.getParamName(), param.getParamValue()));\r
+    }\r
+    bInform.setParameters(paramList);\r
+    return bInform;\r
+  }\r
+\r
+  public static ValueChangeInform getDeviceValueChangeNotification(DeviceInform devNotification,\r
+      TR069InformType notificationType) {\r
+    ValueChangeInform devValChangeNotif = new ValueChangeInform();\r
+    List<InformType> informTypeList = new ArrayList<>();\r
+    informTypeList.add(notificationType);\r
+    devValChangeNotif.setDeviceDetails(devNotification.getDeviceDetails());\r
+    devValChangeNotif.setInformTypeList(informTypeList);\r
+    List<ParameterDTO> paramList = new ArrayList<>();\r
+    for (ParameterDTO param : devNotification.getParameters()) {\r
+      paramList.add(new ParameterDTO(param.getParamName(), param.getParamValue()));\r
+    }\r
+    devValChangeNotif.setParameters(paramList);\r
+    devValChangeNotif.setExternalIPAddress(getExternalIPAddress(devNotification.getParameters()));\r
+    return devValChangeNotif;\r
+  }\r
+\r
+  private static String getExternalIPAddress(List<ParameterDTO> params) {\r
+    ParameterDTO[] nbiParam = params.toArray(new ParameterDTO[params.size()]);\r
+    String externalIpAddress = "";\r
+    boolean isIPv6 = isIPv6Enabled(nbiParam);\r
+\r
+    for (int index1 = 0; index1 < nbiParam.length; index1++) {\r
+\r
+      if (isIPv6) {\r
+        if (nbiParam[index1].getParamName().contains("IPv6Address")\r
+            || nbiParam[index1].getParamName().contains(".1.IPInterfaceIPAddress")) {\r
+          externalIpAddress = nbiParam[index1].getParamValue();\r
+          logger.debug("device communicating is with IPV6 address");\r
+        }\r
+      } else {\r
+        if (nbiParam[index1].getParamName().contains("IPv4Address")\r
+            || nbiParam[index1].getParamName().contains("ExternalIPAddress")\r
+            || nbiParam[index1].getParamName().contains(".1.IPInterfaceIPAddress")) {\r
+          externalIpAddress = nbiParam[index1].getParamValue();\r
+        }\r
+      }\r
+      if (externalIpAddress.trim().length() > 0)\r
+        break;\r
+    }\r
+    return externalIpAddress;\r
+  }\r
+\r
+  private static boolean isIPv6Enabled(ParameterDTO[] nbiParam) {\r
+    boolean isIPv6 = false;\r
+    for (int index1 = 0; index1 < nbiParam.length; index1++) {\r
+      if (nbiParam[index1].getParamName().contains("IPv6Enable")\r
+          && nbiParam[index1].getParamValue().equalsIgnoreCase("1")) {\r
+        isIPv6 = true;\r
+        break;\r
+      }\r
+    }\r
+    return isIPv6;\r
+  }\r
+}\r