exchanging new version yang models on firmware upgrade
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / server / NetconfServerStarter.java
index c438ee5..392b693 100644 (file)
@@ -18,8 +18,6 @@
 \r
 package org.commscope.tr069adapter.netconf.server;\r
 \r
-import com.google.common.base.Preconditions;\r
-\r
 import java.io.BufferedReader;\r
 import java.io.File;\r
 import java.io.FileReader;\r
@@ -32,7 +30,7 @@ import java.util.Map;
 import java.util.concurrent.TimeUnit;\r
 import java.util.regex.Matcher;\r
 import java.util.regex.Pattern;\r
-\r
+import org.commscope.tr069adapter.common.deviceversion.DeviceVersionManager;\r
 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;\r
 import org.commscope.tr069adapter.netconf.operations.CustomOperationsCreator;\r
 import org.opendaylight.netconf.test.tool.NetconfDeviceSimulator;\r
@@ -44,6 +42,8 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;\r
 import org.springframework.context.annotation.Scope;\r
 import org.springframework.stereotype.Component;\r
+import com.google.common.base.Preconditions;\r
+\r
 \r
 @Component\r
 @Scope("singleton")\r
@@ -56,15 +56,21 @@ public class NetconfServerStarter {
   @Autowired\r
   NetConfServerProperties config;\r
 \r
-  public boolean startServer(String netConfPort, String macID) {\r
+  @Autowired\r
+  DeviceVersionManager versionManager;\r
+\r
+  public boolean startServer(String netConfPort, String macID, String swVersion, String hwVersion) {\r
 \r
     if (netConfPort == null) {\r
       LOG.error("Invalid NETCONF port for deviceID: {}, port is null.", macID);\r
       return false;\r
     }\r
 \r
-    LOG.debug("Starting Netconf server for MACID :{}, on port :{}", macID, netConfPort);\r
-    boolean result = startServer(netConfPort, config.getSchemaDirPath(), macID);\r
+    LOG.debug(\r
+        "Starting Netconf server for MACID :{}, on port :{}, softwareVersion {}, hardwareVersion {}",\r
+        macID, netConfPort, swVersion, hwVersion);\r
+    boolean result =\r
+        startServer(netConfPort, config.getSchemaDirPath(), macID, swVersion, hwVersion);\r
     LOG.debug("Completed starting Netconf server for MACID :{} , on port :{}, server status={}",\r
         macID, netConfPort, result);\r
 \r
@@ -72,11 +78,12 @@ public class NetconfServerStarter {
   }\r
 \r
   @SuppressWarnings({"resource", "deprecation"})\r
-  private boolean startServer(String portStr, String schemaDirPath, String macID) {\r
+  private boolean startServer(String portStr, String schemaDirPath, String macID, String swVersion,\r
+      String hwVersion) {\r
 \r
     // creating configuration for the netconf instance\r
     final Configuration configuration = new ConfigurationBuilder().build();\r
-    OperationsCreator operationsCreator = new CustomOperationsCreator(macID);\r
+    OperationsCreator operationsCreator = new CustomOperationsCreator(macID, swVersion, hwVersion);\r
     configuration.setOperationsCreator(operationsCreator);\r
     configuration.setGenerateConfigsTimeout((int) TimeUnit.MINUTES.toMillis(30));\r
     if (portStr != null) {\r
@@ -90,16 +97,21 @@ public class NetconfServerStarter {
     }\r
     configuration.setDeviceCount(1);\r
     configuration.setSsh(Boolean.TRUE);\r
-    File schemaDir = new File(schemaDirPath);\r
-    configuration.setSchemasDir(schemaDir);\r
     configuration.setCapabilities(Configuration.DEFAULT_BASE_CAPABILITIES_EXI);\r
     configuration.setIp("0.0.0.0");\r
 \r
-    boolean isSchemaLoaded = loadSchemas(schemaDir);\r
-    if (!isSchemaLoaded) {\r
-      LOG.debug("Failed to load schema for netconf server instance {}", macID);\r
+    String versionPath = versionManager.getNetconfYangSchemaPath(swVersion, hwVersion);\r
+    if (versionPath == null && swVersion != null) {\r
+      LOG.error("Failed to get version path for software version {}, calling base version",\r
+          swVersion);\r
+      versionPath = versionManager.getBaseNetconfYangSchemaPath();\r
+    } else if (swVersion == null) {\r
+      LOG.error("Software version is null {}", swVersion);\r
       return false;\r
     }\r
+    String schemaVerPath = schemaDirPath + File.separator + versionPath;\r
+    File schemaVerDir = new File(schemaVerPath);\r
+    configuration.setSchemasDir(schemaVerDir);\r
 \r
     try (final NetconfDevice netconfDevice = new NetconfDevice(configuration)) {\r
       final List<Integer> openDevices = netconfDevice.start();\r
@@ -123,6 +135,7 @@ public class NetconfServerStarter {
       NetconfDevice netconf = serversMap.get(macID);\r
       netconf.setAutoClose(true);\r
       netconf.close();\r
+      serversMap.remove(macID);\r
       LOG.debug("Completed stopping Netconf server for MACID {}", macID);\r
       return true;\r
     } catch (Exception e) {\r
@@ -133,7 +146,7 @@ public class NetconfServerStarter {
     return false;\r
   }\r
 \r
-  private boolean loadSchemas(File schemasDir) {\r
+  protected boolean loadSchemas(File schemasDir) {\r
     if (schemasDir != null) {\r
       if (!schemasDir.exists() || !schemasDir.isDirectory() || !schemasDir.canRead()) {\r
         LOG.error("Failed to load schema. schema location is not valid.");\r
@@ -165,6 +178,14 @@ public class NetconfServerStarter {
     return true;\r
   }\r
 \r
+  public boolean isNetConfServerRunning(String deviceId) {\r
+    NetconfDevice nc = serversMap.get(deviceId);\r
+    if (null != nc)\r
+      return true;\r
+    else\r
+      return false;\r
+  }\r
+\r
   private void loadSchemaPattren(String line, File file, Pattern revisionregex) {\r
     if (line != null) {\r
       final Matcher m = revisionregex.matcher(line);\r