Device Software version management
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / acs / impl / ACSNotificationHandlerImpl.java
index 693f788..b6adad7 100644 (file)
@@ -20,7 +20,6 @@ package org.commscope.tr069adapter.mapper.acs.impl;
 \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
@@ -28,16 +27,23 @@ import org.commscope.tr069adapter.acs.common.ParameterDTO;
 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.ConnectionRequestInform;\r
 import org.commscope.tr069adapter.acs.common.inform.PeriodicInform;\r
+import org.commscope.tr069adapter.acs.common.inform.TransferCompleteInform;\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.dao.DeviceOperationsDAO;\r
+import org.commscope.tr069adapter.mapper.entity.DeviceOperationDetails;\r
+import org.commscope.tr069adapter.mapper.model.NetConfNotificationDTO;\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.FirwareUpgradeErrorCode;\r
+import org.commscope.tr069adapter.mapper.util.FirwareUpgradeStatus;\r
 import org.commscope.tr069adapter.mapper.util.MOMetaDataUtil;\r
 import org.commscope.tr069adapter.mapper.ves.VESNotificationSender;\r
 import org.slf4j.Logger;\r
@@ -49,6 +55,7 @@ import org.springframework.stereotype.Component;
 public class ACSNotificationHandlerImpl implements ACSNotificationHandler {\r
 \r
   private static final Logger logger = LoggerFactory.getLogger(ACSNotificationHandlerImpl.class);\r
+  private static final String SOFT_MGMT_NS_URI = "urn:o-ran:software-management:1.0";\r
 \r
   @Autowired\r
   SynchronizedRequestHandler syncHandler;\r
@@ -71,10 +78,15 @@ public class ACSNotificationHandlerImpl implements ACSNotificationHandler {
   @Autowired\r
   NetConfServerManager netconfManager;\r
 \r
+  @Autowired\r
+  DeviceOperationsDAO deviceOperDAO;\r
+\r
   @Override\r
   public void handleOperationResponse(DeviceRPCResponse opResult) {\r
     opResult.getOperationResponse().setParameterDTOs(\r
-        filterUnsupportedParameters(opResult.getOperationResponse().getParameterDTOs()));\r
+        filterUnsupportedParameters(opResult.getOperationResponse().getParameterDTOs(),\r
+            opResult.getDeviceDetails().getSoftwareVersion(),\r
+            opResult.getDeviceDetails().getHardwareVersion()));\r
     syncHandler.notifyResult(opResult);\r
   }\r
 \r
@@ -85,6 +97,18 @@ public class ACSNotificationHandlerImpl implements ACSNotificationHandler {
     if (notification instanceof BootstrapInform) {\r
       logger.info("BootStrap notification received");\r
       BootstrapInform bootstrapNotification = (BootstrapInform) notification;\r
+\r
+      DeviceOperationDetails deviceDetails =\r
+          deviceOperDAO.findByDeviceId(notification.getDeviceDetails().getDeviceId());\r
+      if (deviceDetails == null) {\r
+        deviceDetails = new DeviceOperationDetails();\r
+        deviceDetails.setDeviceId(notification.getDeviceDetails().getDeviceId());\r
+        deviceDetails.setSwVersion(notification.getDeviceDetails().getSoftwareVersion());\r
+        deviceOperDAO.save(deviceDetails);\r
+      }\r
+\r
+      checkForActivateNotification(notification);\r
+\r
       // send request to create the netconf server instance for the bootstrap device\r
       // id\r
       NetConfServerDetails serverInfo = createNtConfServer(bootstrapNotification);\r
@@ -103,6 +127,7 @@ public class ACSNotificationHandlerImpl implements ACSNotificationHandler {
       notiSender.sendNotification(bsInform);\r
     } else if (notification instanceof BootInform) {\r
       logger.info("Boot notification received");\r
+      checkForActivateNotification(notification);\r
       BootInform bootNotification = (BootInform) notification;\r
       BootInform bInform = getDeviceBootNotification(bootNotification, TR069InformType.BOOT);\r
       if (bootNotification.getValueChangeNotification() != null) {\r
@@ -117,21 +142,37 @@ public class ACSNotificationHandlerImpl implements ACSNotificationHandler {
       vesnotiSender.sendNotification(pINotificaiton, null);\r
       notiSender.sendNotification(pINotificaiton);\r
       logger.info("VC notification received");\r
+    } else if (notification instanceof ConnectionRequestInform) {\r
+      ConnectionRequestInform crNotificaiton = (ConnectionRequestInform) notification;\r
+      vesnotiSender.sendNotification(crNotificaiton, null);\r
+      logger.info("ConnectionRequestInform notification received");\r
     } else if (notification instanceof ValueChangeInform) {\r
       ValueChangeInform valueChgNotificaiton = (ValueChangeInform) notification;\r
       processVCNotification(valueChgNotificaiton, isAlarmVC);\r
+    } else if (notification instanceof TransferCompleteInform) {\r
+      TransferCompleteInform tfNotificaiton = (TransferCompleteInform) notification;\r
+      if (tfNotificaiton.getCommandKey() != null && tfNotificaiton.getCommandKey()\r
+          .equalsIgnoreCase(tfNotificaiton.getDeviceDetails().getDeviceId())) {\r
+        logger.debug("TransferCompleteInform is recevied at mapper");\r
+        processTransferCompleteInform(tfNotificaiton);\r
+        logger.debug("TransferCompleteInform processing completed at mapper");\r
+      }\r
     }\r
 \r
     pnpPreProvisioningHandler.onDeviceNotification(notification);\r
   }\r
 \r
   private NetConfServerDetails createNtConfServer(BootstrapInform bootstrapNotification) {\r
-    String eNodeBName = pnpPreProvisioningHandler\r
-        .getEnodeBName(bootstrapNotification.getDeviceDetails().getDeviceId());\r
+    String eNodeBName = pnpPreProvisioningHandler.getEnodeBName(\r
+        bootstrapNotification.getDeviceDetails().getDeviceId(),\r
+        bootstrapNotification.getDeviceDetails().getSoftwareVersion(),\r
+        bootstrapNotification.getDeviceDetails().getHardwareVersion());\r
     if (eNodeBName == null)\r
       eNodeBName = bootstrapNotification.getDeviceDetails().getDeviceId();\r
-    NetConfServerDetails serverInfo = netconfManager\r
-        .createNetconfServer(bootstrapNotification.getDeviceDetails().getDeviceId(), eNodeBName);\r
+    NetConfServerDetails serverInfo =\r
+        netconfManager.createNetconfServer(bootstrapNotification.getDeviceDetails().getDeviceId(),\r
+            eNodeBName, bootstrapNotification.getDeviceDetails().getSoftwareVersion(),\r
+            bootstrapNotification.getDeviceDetails().getHardwareVersion());\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
@@ -167,11 +208,13 @@ public class ACSNotificationHandlerImpl implements ACSNotificationHandler {
     return false;\r
   }\r
 \r
-  public List<ParameterDTO> filterUnsupportedParameters(List<ParameterDTO> parameters) {\r
+  public List<ParameterDTO> filterUnsupportedParameters(List<ParameterDTO> parameters,\r
+      String swVersion, String hwVersion) {\r
     List<ParameterDTO> result = new ArrayList<>();\r
     if (null != parameters) {\r
       for (ParameterDTO param : parameters) {\r
-        MOMetaData metaData = metaDataUtil.getMetaDataByTR69Name(param.getParamName());\r
+        MOMetaData metaData =\r
+            metaDataUtil.getMetaDataByTR69Name(param.getParamName(), swVersion, hwVersion);\r
         if (null != metaData) {\r
           result.add(param);\r
         }\r
@@ -263,4 +306,102 @@ public class ACSNotificationHandlerImpl implements ACSNotificationHandler {
     }\r
     return isIPv6;\r
   }\r
+\r
+  private void processTransferCompleteInform(TransferCompleteInform notification) {\r
+\r
+    try {\r
+      ArrayList<ParameterDTO> paramList = new ArrayList<ParameterDTO>();\r
+      DeviceOperationDetails fwDetails =\r
+          deviceOperDAO.findByDeviceId(notification.getDeviceDetails().getDeviceId());\r
+      if (fwDetails == null || fwDetails.getFileName() == null) {\r
+        logger.debug(\r
+            "TransferCompleteInform recevied for invaild device, there is no entry exist in the database");\r
+        return;\r
+      }\r
+      if (fwDetails.getDownLoadStatus() == FirwareUpgradeStatus.DOWNLOAD_INTIATED.getStatus()) {\r
+        paramList.add(new ParameterDTO("download-event.file-name", fwDetails.getFileName()));\r
+\r
+        String status = FirwareUpgradeErrorCode.getErrorCodeMapping(notification.getFaultCode());\r
+        paramList.add(new ParameterDTO("download-event.status", status));\r
+        if (notification.getFaultCode() != 0) {\r
+          fwDetails.setDownLoadStatus(FirwareUpgradeStatus.DOWNLOAD_FAILED.getStatus());\r
+          paramList\r
+              .add(new ParameterDTO("download-event.error-message", notification.getFaultString()));\r
+        } else {\r
+          fwDetails.setDownLoadStatus(FirwareUpgradeStatus.DOWNLOAD_COMPLETED.getStatus());\r
+          logger.debug("downloading file completed on the device successfully.");\r
+        }\r
+        deviceOperDAO.save(fwDetails);\r
+\r
+        logger.debug("sending download-event notification to netconfserver");\r
+\r
+        if (notiSender.sendCustomNotification(notification.getDeviceDetails().getDeviceId(),\r
+            paramList, SOFT_MGMT_NS_URI).getStatusCode().is2xxSuccessful()) {\r
+          logger.debug("sending download-event notification to netconfserver sucess");\r
+        } else {\r
+          logger.error("sending download-event notification to netconfserver failed");\r
+        }\r
+      } else {\r
+        logger.debug(\r
+            "TransferCompleteInform recevied after boot is received; already software is activated");\r
+      }\r
+    } catch (Exception e) {\r
+      logger.debug("Exception occured while processing TransferCompleteInform " + e.toString());\r
+    }\r
+  }\r
+\r
+  private void checkForActivateNotification(DeviceInform notification) {\r
+\r
+    try {\r
+      ArrayList<ParameterDTO> paramList = new ArrayList<ParameterDTO>();\r
+      DeviceOperationDetails devDetails =\r
+          deviceOperDAO.findByDeviceId(notification.getDeviceDetails().getDeviceId());\r
+\r
+      if (devDetails == null\r
+          || devDetails.getDownLoadStatus() == FirwareUpgradeStatus.NOT_STARTED.getStatus()) {\r
+        logger.debug("firmware upgrade is not in progress");\r
+        return;\r
+      }\r
+\r
+      if (!notification.getDeviceDetails().getSoftwareVersion()\r
+          .equalsIgnoreCase(devDetails.getSwVersion())\r
+          && devDetails.getDownLoadStatus() == FirwareUpgradeStatus.DOWNLOAD_INTIATED.getStatus()) {\r
+        logger.debug("received the boot/bootstrap before the transfer complete recevied");\r
+        TransferCompleteInform inform = new TransferCompleteInform();\r
+        inform.setDeviceDetails(notification.getDeviceDetails());\r
+        inform.setFaultCode(0);\r
+        processTransferCompleteInform(inform);\r
+      }\r
+\r
+      devDetails = deviceOperDAO.findByDeviceId(notification.getDeviceDetails().getDeviceId());\r
+      if (devDetails.getDownLoadStatus() == FirwareUpgradeStatus.DOWNLOAD_COMPLETED.getStatus()) {\r
+        paramList.add(new ParameterDTO("activation-event.slot-name", "Active-Partion"));\r
+        // check for software change\r
+        if (notification.getDeviceDetails().getSoftwareVersion()\r
+            .equalsIgnoreCase(devDetails.getSwVersion())) {\r
+          paramList.add(new ParameterDTO("activation-event.status", "APPLICATION_ERROR"));\r
+          paramList.add(new ParameterDTO("activation-event.error-message",\r
+              "Same Software Version is reported after upgrade"));\r
+          devDetails.setDownLoadStatus(FirwareUpgradeStatus.ACTIVATION_ERROR.getStatus());\r
+        } else {\r
+          devDetails.setSwVersion(notification.getDeviceDetails().getSoftwareVersion());\r
+          devDetails.setDownLoadStatus(FirwareUpgradeStatus.ACTIVATION_COMPLETED.getStatus());\r
+          paramList.add(new ParameterDTO("activation-event.status", "COMPLETED"));\r
+        }\r
+        deviceOperDAO.save(devDetails);\r
+\r
+        logger.debug("sending activation-event notification to netconfserver");\r
+\r
+        if (notiSender.sendCustomNotification(notification.getDeviceDetails().getDeviceId(),\r
+            paramList, SOFT_MGMT_NS_URI).getStatusCode().is2xxSuccessful()) {\r
+          logger.debug("sending activation-event notification to netconfserver sucess");\r
+        } else {\r
+          logger.error("sending activation-event notification to netconfserver failed");\r
+        }\r
+      }\r
+    } catch (Exception e) {\r
+      logger.debug(\r
+          "Exception occured while processing ProcessFirmWareActivateNotification " + e.toString());\r
+    }\r
+  }\r
 }\r