exchanging new version yang models on firmware upgrade
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / server / NetConfServerManagerImpl.java
index ed1164e..852d205 100644 (file)
@@ -18,6 +18,8 @@
 \r
 package org.commscope.tr069adapter.netconf.server;\r
 \r
+import java.io.File;\r
+import java.io.IOException;\r
 import java.net.URI;\r
 import java.net.URISyntaxException;\r
 import java.util.ArrayList;\r
@@ -26,10 +28,12 @@ import java.util.Map;
 import java.util.Map.Entry;\r
 import java.util.concurrent.ExecutorService;\r
 import java.util.concurrent.Executors;\r
-\r
+import org.apache.commons.io.FileUtils;\r
 import org.commscope.tr069adapter.acs.common.OperationDetails;\r
 import org.commscope.tr069adapter.acs.common.ParameterDTO;\r
 import org.commscope.tr069adapter.acs.common.dto.TR069OperationCode;\r
+import org.commscope.tr069adapter.common.deviceversion.DeviceVersionManager;\r
+import org.commscope.tr069adapter.common.deviceversion.ProfileDefinition;\r
 import org.commscope.tr069adapter.mapper.model.NetConfServerDetails;\r
 import org.commscope.tr069adapter.mapper.model.NetconfServerManagementError;\r
 import org.commscope.tr069adapter.mapper.model.VESNotification;\r
@@ -73,8 +77,48 @@ public class NetConfServerManagerImpl {
   @Autowired\r
   VESNotificationSender vesNotificationSender;\r
 \r
+  @Autowired\r
+  DeviceVersionManager versionManager;\r
+\r
   ExecutorService executorService = Executors.newFixedThreadPool(10);\r
 \r
+  public boolean loadSchemas() {\r
+    LOG.debug("Loading yang schema started");\r
+    List<ProfileDefinition> profiles = versionManager.getSupportedProfileDefinitions();\r
+    try {\r
+      String commonSchemaPath = config.getSchemaDirPath() + "/common";\r
+\r
+      for (ProfileDefinition profile : profiles) {\r
+        String verSpecificSchemaPath =\r
+            config.getSchemaDirPath() + File.separator + profile.getNetConfSchemaPath();\r
+        File schemaDir = new File(commonSchemaPath);\r
+        File schemaVerDir = new File(verSpecificSchemaPath);\r
+\r
+        if (!schemaVerDir.isDirectory()) {\r
+          LOG.error("No folder path found for given version path {}",\r
+              schemaVerDir.getAbsolutePath());\r
+          return false;\r
+        }\r
+\r
+        try {\r
+          FileUtils.copyDirectory(schemaDir, schemaVerDir);\r
+        } catch (IOException e) {\r
+          LOG.error("Failed to copy directory " + e.getMessage());\r
+        }\r
+        boolean isSchemaLoaded = ncServerStarter.loadSchemas(schemaVerDir);\r
+        if (!isSchemaLoaded) {\r
+          LOG.debug("Failed to load schema for profile {}", profile.getProfileId());\r
+          return false;\r
+        }\r
+      }\r
+    } catch (Exception e) {\r
+      LOG.error("Load schema's failed in netconf server {}", e.getMessage());\r
+      return false;\r
+    }\r
+    LOG.debug("Loading yang schema completed");\r
+    return true;\r
+  }\r
+\r
   public void restartServers() {\r
     LOG.debug("Restarting all netconf servers during startup...");\r
     Iterable<NetConfServerDetailsEntity> entities = netconfDAO.findAll();\r
@@ -88,14 +132,15 @@ public class NetConfServerManagerImpl {
         try {\r
           restartServersHandler.restart(entity);\r
         } catch (RetryFailedException e) {\r
-          e.printStackTrace();\r
+          LOG.error("submit task for restarting is failed {}", e.toString());\r
         }\r
       }\r
     }\r
     LOG.debug("Restarting netconf servers during startup is completed.");\r
   }\r
 \r
-  public NetConfServerDetails createServer(String deviceId, String enodeBName) {\r
+  public NetConfServerDetails createServer(String deviceId, String enodeBName, String swVersion,\r
+      String hwVersion) {\r
     NetConfServerDetails result = new NetConfServerDetails();\r
     NetConfServerDetailsEntity entity = null;\r
     if (deviceId != null) {\r
@@ -109,7 +154,11 @@ public class NetConfServerManagerImpl {
       return null;\r
     }\r
 \r
-    if (null != entity) {\r
+    if (null != entity && ncServerStarter.isNetConfServerRunning(deviceId)) {\r
+      if (isVersionChanged(entity, swVersion, hwVersion)) {\r
+        return restartOnVersionChange(deviceId, enodeBName, swVersion, hwVersion);\r
+      }\r
+\r
       // found the entity. server is already running. double check and run\r
       // if\r
       // required. else return the details.\r
@@ -133,7 +182,7 @@ public class NetConfServerManagerImpl {
         LOG.debug("Successfully reserved a port for deviceID={} ,port={}", deviceId, port);\r
 \r
         // start the server\r
-        boolean isServerStarted = ncServerStarter.startServer(port, deviceId);\r
+        boolean isServerStarted = ncServerStarter.startServer(port, deviceId, swVersion, hwVersion);\r
         boolean isPortInUse = serverPortAllocator.isServerPortInUse(port);\r
 \r
         if (!isServerStarted || !isPortInUse) {\r
@@ -150,6 +199,8 @@ public class NetConfServerManagerImpl {
       entity.setDeviceId(deviceId);\r
       entity.setListenPort(port);\r
       entity.setEnodeBName(enodeBName);\r
+      entity.setSwVersion(swVersion);\r
+      entity.setHwVersion(hwVersion);\r
       netconfDAO.save(entity);\r
 \r
       result = getNetConfServerDetails(deviceId, entity);\r
@@ -172,13 +223,50 @@ public class NetConfServerManagerImpl {
     return result;\r
   }\r
 \r
-  public boolean restartServersOnStartup(NetConfServerDetailsEntity entity) {\r
+  public NetConfServerDetails restartOnVersionChange(String deviceId, String enodeBName,\r
+      String swVersion, String hwVersion) {\r
+\r
+    NetConfServerDetailsEntity entity = null;\r
+    if (deviceId != null) {\r
+      entity = netconfDAO.findByDeviceId(deviceId);\r
+    }\r
+    if (entity == null) {\r
+      return null;\r
+    }\r
+\r
+    boolean isVersionChanged = isVersionChanged(entity, swVersion, hwVersion);\r
+    if (isVersionChanged) {\r
+      LOG.debug("software version changed, stopping the the existing netconf instance");\r
+      boolean result = this.ncServerStarter.stopServer(deviceId);\r
+      if (result) {\r
+        LOG.debug(\r
+            "successfully stopped the netconf instance; trying to start with new version yang models");\r
+        entity.setSwVersion(swVersion);\r
+        entity.setHwVersion(hwVersion);\r
+        netconfDAO.save(entity);\r
+\r
+        boolean isSuccess = startNetConfServerInstance(entity);\r
+\r
+        if (!isSuccess) {\r
+          try {\r
+            restartServersHandler.restart(entity);\r
+          } catch (RetryFailedException e) {\r
+            LOG.debug("");\r
+          }\r
+        }\r
+      }\r
+    }\r
+    return getNetConfServerDetails(deviceId, entity);\r
+  }\r
+\r
+\r
+  public boolean startNetConfServerInstance(NetConfServerDetailsEntity entity) {\r
     boolean isSuccess = false;\r
 \r
-    boolean isServerStarted =\r
-        ncServerStarter.startServer(entity.getListenPort(), entity.getDeviceId());\r
+    boolean isServerStarted = ncServerStarter.startServer(entity.getListenPort(),\r
+        entity.getDeviceId(), entity.getSwVersion(), entity.getHwVersion());\r
     if (isServerStarted) {\r
-      LOG.info("Successfully restarted NETCONF server {}  on port {}  upon application startup.",\r
+      LOG.info("Successfully restarted NETCONF server {}  on port {} .",\r
           entity.getDeviceId(), entity.getListenPort());\r
       // we need to push the pnfEntry for IP updated\r
       NetConfServerDetails details = getNetConfServerDetails(entity.getDeviceId(), entity);\r
@@ -215,6 +303,8 @@ public class NetConfServerManagerImpl {
     result.setDeviceId(deviceId);\r
     result.setListenPort(entity.getListenPort());\r
     result.setEnodeBName(entity.getEnodeBName());\r
+    result.setSwVersion(entity.getSwVersion());\r
+    result.setHwVersion(entity.getHwVersion());\r
     String netconfListenAddress = getServiceHost();\r
     if (netconfListenAddress == null) {\r
       netconfListenAddress = config.getNetconfServerIP();\r
@@ -241,6 +331,7 @@ public class NetConfServerManagerImpl {
       resultMsg = "Failed to unregister the device " + deviceId + ", enodeBName=" + enodeBName\r
           + ". Invalid deviceId/enodeBName specified.";\r
       LOG.info(resultMsg);\r
+      return resultMsg;\r
     }\r
     boolean result = this.ncServerStarter.stopServer(deviceId);\r
     if (result) {\r
@@ -302,6 +393,8 @@ public class NetConfServerManagerImpl {
       server.setError(NetconfServerManagementError.SUCCESS);\r
       server.setListenAddress(netconfListenAddress);\r
       server.setListenPort(entity.getListenPort());\r
+      server.setSwVersion(entity.getSwVersion());\r
+      server.setHwVersion(entity.getHwVersion());\r
       result.add(server);\r
     }\r
     return result;\r
@@ -317,6 +410,14 @@ public class NetConfServerManagerImpl {
     return null;\r
   }\r
 \r
+  private boolean isVersionChanged(NetConfServerDetailsEntity entity, String swVersion,\r
+      String hwVersion) {\r
+    String existingProfileId =\r
+        versionManager.getAssociatedProfileId(entity.getSwVersion(), entity.getHwVersion());\r
+    String newProfiled = versionManager.getAssociatedProfileId(swVersion, hwVersion);\r
+    return !existingProfileId.equalsIgnoreCase(newProfiled) ? true : false;\r
+  }\r
+\r
   class ServerStartTask implements Runnable {\r
 \r
     NetConfServerDetailsEntity entity;\r
@@ -330,12 +431,12 @@ public class NetConfServerManagerImpl {
 \r
     @Override\r
     public void run() {\r
-      boolean isSuccess = netconfServerManager.restartServersOnStartup(entity);\r
-      if (isSuccess) {\r
+      boolean isSuccess = netconfServerManager.startNetConfServerInstance(entity);\r
+      if (!isSuccess) {\r
         try {\r
           netconfServerManager.restartServersHandler.restart(entity);\r
         } catch (RetryFailedException e) {\r
-          e.printStackTrace();// TODO: logg\r
+          LOG.debug("");\r
         }\r
       }\r
     }\r