-/*\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.netconf.server;\r
-\r
-import java.io.BufferedReader;\r
-import java.io.File;\r
-import java.io.FileReader;\r
-import java.io.IOException;\r
-import java.util.Arrays;\r
-import java.util.Collections;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.concurrent.TimeUnit;\r
-import java.util.regex.Matcher;\r
-import java.util.regex.Pattern;\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
-import org.opendaylight.netconf.test.tool.config.Configuration;\r
-import org.opendaylight.netconf.test.tool.config.ConfigurationBuilder;\r
-import org.opendaylight.netconf.test.tool.operations.OperationsCreator;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-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
-public class NetconfServerStarter {\r
-\r
- private static final Logger LOG = LoggerFactory.getLogger(NetconfServerStarter.class);\r
-\r
- private static Map<String, NetconfDevice> serversMap = new HashMap<>();\r
-\r
- @Autowired\r
- NetConfServerProperties config;\r
-\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(\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
- return result;\r
- }\r
-\r
- @SuppressWarnings({"resource", "deprecation"})\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, swVersion, hwVersion);\r
- configuration.setOperationsCreator(operationsCreator);\r
- configuration.setGenerateConfigsTimeout((int) TimeUnit.MINUTES.toMillis(30));\r
- if (portStr != null) {\r
- try {\r
- int port = Integer.parseInt(portStr);\r
- configuration.setStartingPort(port);\r
- } catch (Exception e) {\r
- LOG.error("Failed to get netconf service instance port for parameters {}", e.toString());\r
- return false;\r
- }\r
- }\r
- configuration.setDeviceCount(1);\r
- configuration.setSsh(Boolean.TRUE);\r
- configuration.setCapabilities(Configuration.DEFAULT_BASE_CAPABILITIES_EXI);\r
- configuration.setIp("0.0.0.0");\r
-\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
- if (openDevices.isEmpty()) {\r
- LOG.debug("Failed to start netconf server instance {}", macID);\r
- return false;\r
- }\r
- netconfDevice.setAutoClose(false);\r
- serversMap.put(macID, netconfDevice);\r
- } catch (RuntimeException e) {\r
- LOG.error("Unhandled exception. Failed to start the server", e);\r
- return false;\r
- }\r
-\r
- return true;\r
- }\r
-\r
- public boolean stopServer(String macID) {\r
- try {\r
- LOG.debug("Stopping Netconf server for MACID {}", macID);\r
- 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
- LOG.debug("Error while stopping Netconf server for MACID {}; error message {}", macID,\r
- e.getMessage());\r
- }\r
-\r
- return false;\r
- }\r
-\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
- return false;\r
- }\r
-\r
- Pattern yangregex = Pattern.compile("(?<name>.*)@(?<revision>\\d{4}-\\d{2}-\\d{2})\\.yang");\r
- Pattern revisionregex = Pattern.compile("revision\\s+\"?(\\d{4}-\\d{2}-\\d{2})\"?");\r
-\r
- final File[] filesArray = schemasDir.listFiles();\r
- final List<File> files =\r
- filesArray != null ? Arrays.asList(filesArray) : Collections.emptyList();\r
- for (final File file : files) {\r
- final Matcher yangMatcher = yangregex.matcher(file.getName());\r
- if (!yangMatcher.matches()) {\r
- try (BufferedReader reader = new BufferedReader(new FileReader(file))) {\r
- String line = reader.readLine();\r
- while (line != null && !revisionregex.matcher(line).find()) {\r
- line = reader.readLine();\r
- }\r
- loadSchemaPattren(line, file, revisionregex);\r
- } catch (final IOException e) {\r
- LOG.error("Unhandled exception. Failed to load the schema.{}", e.toString());\r
- return false;\r
- }\r
- }\r
- }\r
- }\r
- 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
- Preconditions.checkState(m.find());\r
- String moduleName = file.getAbsolutePath();\r
- if (file.getName().endsWith(".yang")) {\r
- moduleName = moduleName.substring(0, moduleName.length() - 5);\r
- }\r
- final String revision = m.group(1);\r
- final String correctName = moduleName + "@" + revision + ".yang";\r
- final File correctNameFile = new File(correctName);\r
- if (!file.renameTo(correctNameFile)) {\r
- throw new IllegalStateException("Failed to rename '%s'." + file);\r
- }\r
- }\r
- }\r
-\r
-}\r
-\r
-\r
-class NetconfDevice extends NetconfDeviceSimulator {\r
- boolean autoClose = true;\r
-\r
- public NetconfDevice(Configuration configuration) {\r
- super(configuration);\r
- }\r
-\r
- @Override\r
- public void close() {\r
- if (autoClose)\r
- super.close();\r
- }\r
-\r
- public void setAutoClose(boolean autoClose) {\r
- this.autoClose = autoClose;\r
- }\r
-\r
-}\r
+/*
+ * ============LICENSE_START========================================================================
+ * ONAP : tr-069-adapter
+ * =================================================================================================
+ * Copyright (C) 2020 CommScope Inc Intellectual Property.
+ * =================================================================================================
+ * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
+ * may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ===============LICENSE_END=======================================================================
+ */
+
+package org.commscope.tr069adapter.netconf.server;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.commscope.tr069adapter.common.deviceversion.DeviceVersionManager;
+import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;
+import org.commscope.tr069adapter.netconf.operations.CustomOperationsCreator;
+import org.opendaylight.netconf.test.tool.NetconfDeviceSimulator;
+import org.opendaylight.netconf.test.tool.config.Configuration;
+import org.opendaylight.netconf.test.tool.config.ConfigurationBuilder;
+import org.opendaylight.netconf.test.tool.operations.OperationsCreator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
+
+import com.google.common.base.Preconditions;
+
+
+@Component
+@Scope("singleton")
+public class NetconfServerStarter {
+
+ private static final Logger LOG = LoggerFactory.getLogger(NetconfServerStarter.class);
+ public static final String PATTERN = "[\n|\r|\t]";
+
+ private static Map<String, NetconfDevice> serversMap = new HashMap<>();
+
+ @Autowired
+ NetConfServerProperties config;
+
+ @Autowired
+ DeviceVersionManager versionManager;
+
+ public boolean startServer(String netConfPort, String macID, String swVersion, String hwVersion) {
+ macID = macID.replaceAll(PATTERN, "_");
+ if (netConfPort == null) {
+ LOG.error("Invalid NETCONF port for deviceID: {}, port is null.", macID);
+ return false;
+ }
+
+ LOG.debug(
+ "Starting Netconf server for MACID :{}, on port :{}, softwareVersion {}, hardwareVersion {}",
+ macID, netConfPort, swVersion, hwVersion);
+ boolean result =
+ startServer(netConfPort, config.getSchemaDirPath(), macID, swVersion, hwVersion);
+ LOG.debug("Completed starting Netconf server for MACID :{} , on port :{}, server status={}",
+ macID, netConfPort, result);
+
+ return result;
+ }
+
+ @SuppressWarnings({"resource", "deprecation"})
+ private boolean startServer(String portStr, String schemaDirPath, String macID, String swVersion,
+ String hwVersion) {
+
+ // creating configuration for the netconf instance
+ final Configuration configuration = new ConfigurationBuilder().build();
+ OperationsCreator operationsCreator = new CustomOperationsCreator(macID, swVersion, hwVersion);
+ configuration.setOperationsCreator(operationsCreator);
+ configuration.setGenerateConfigsTimeout((int) TimeUnit.MINUTES.toMillis(30));
+ if (portStr != null) {
+ try {
+ int port = Integer.parseInt(portStr);
+ configuration.setStartingPort(port);
+ } catch (Exception e) {
+ LOG.error("Failed to get netconf service instance port for parameters {}", e.toString());
+ return false;
+ }
+ }
+ configuration.setDeviceCount(1);
+ configuration.setSsh(Boolean.TRUE);
+ configuration.setCapabilities(Configuration.DEFAULT_BASE_CAPABILITIES_EXI);
+ configuration.setIp("0.0.0.0");
+
+ String versionPath = versionManager.getNetconfYangSchemaPath(swVersion, hwVersion);
+ if (versionPath == null && swVersion != null) {
+ swVersion = swVersion.replaceAll(PATTERN, "_");
+ LOG.error("Failed to get version path for software version {}, calling base version",
+ swVersion);
+ versionPath = versionManager.getBaseNetconfYangSchemaPath();
+ } else if (swVersion == null) {
+ LOG.error("Software version is null ");
+ return false;
+ }
+ String schemaVerPath = schemaDirPath + File.separator + versionPath;
+ File schemaVerDir = new File(schemaVerPath);
+ configuration.setSchemasDir(schemaVerDir);
+
+ try (final NetconfDevice netconfDevice = new NetconfDevice(configuration)) {
+ final List<Integer> openDevices = netconfDevice.start();
+ if (openDevices.isEmpty()) {
+ LOG.debug("Failed to start netconf server instance {}", macID);
+ return false;
+ }
+ netconfDevice.setAutoClose(false);
+ serversMap.put(macID, netconfDevice);
+ } catch (RuntimeException e) {
+ LOG.error("Unhandled exception. Failed to start the server", e);
+ return false;
+ }
+
+ return true;
+ }
+
+ public boolean stopServer(String macID) {
+ try {
+ LOG.debug("Stopping Netconf server for MACID {}", macID);
+ NetconfDevice netconf = serversMap.get(macID);
+ netconf.setAutoClose(true);
+ netconf.close();
+ serversMap.remove(macID);
+ LOG.debug("Completed stopping Netconf server for MACID {}", macID);
+ return true;
+ } catch (Exception e) {
+ LOG.debug("Error while stopping Netconf server for MACID {}; error message {}", macID,
+ e.getMessage());
+ }
+
+ return false;
+ }
+
+ protected boolean loadSchemas(File schemasDir) {
+ if (schemasDir != null) {
+ if (!schemasDir.exists() || !schemasDir.isDirectory() || !schemasDir.canRead()) {
+ LOG.error("Failed to load schema. schema location is not valid.");
+ return false;
+ }
+
+ Pattern yangregex = Pattern.compile("(?<name>.*)@(?<revision>\\d{4}-\\d{2}-\\d{2})\\.yang");
+ Pattern revisionregex = Pattern.compile("revision\\s+\"?(\\d{4}-\\d{2}-\\d{2})\"?");
+
+ final File[] filesArray = schemasDir.listFiles();
+ final List<File> files =
+ filesArray != null ? Arrays.asList(filesArray) : Collections.emptyList();
+ for (final File file : files) {
+ final Matcher yangMatcher = yangregex.matcher(file.getName());
+ if (!yangMatcher.matches()) {
+ try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
+ String line = reader.readLine();
+ while (line != null && !revisionregex.matcher(line).find()) {
+ line = reader.readLine();
+ }
+ loadSchemaPattren(line, file, revisionregex);
+ } catch (final IOException e) {
+ LOG.error("Unhandled exception. Failed to load the schema.{}", e.toString());
+ return false;
+ }
+ }
+ }
+ }
+ return true;
+ }
+
+ public boolean isNetConfServerRunning(String deviceId) {
+ NetconfDevice nc = serversMap.get(deviceId);
+ if (null != nc)
+ return true;
+ else
+ return false;
+ }
+
+ private void loadSchemaPattren(String line, File file, Pattern revisionregex) {
+ if (line != null) {
+ final Matcher m = revisionregex.matcher(line);
+ Preconditions.checkState(m.find());
+ String moduleName = file.getAbsolutePath();
+ if (file.getName().endsWith(".yang")) {
+ moduleName = moduleName.substring(0, moduleName.length() - 5);
+ }
+ final String revision = m.group(1);
+ final String correctName = moduleName + "@" + revision + ".yang";
+ final File correctNameFile = new File(correctName);
+ if (!file.renameTo(correctNameFile)) {
+ throw new IllegalStateException("Failed to rename '%s'." + file);
+ }
+ }
+ }
+
+}
+
+
+class NetconfDevice extends NetconfDeviceSimulator {
+ boolean autoClose = true;
+
+ public NetconfDevice(Configuration configuration) {
+ super(configuration);
+ }
+
+ @Override
+ public void close() {
+ if (autoClose)
+ super.close();
+ }
+
+ public void setAutoClose(boolean autoClose) {
+ this.autoClose = autoClose;
+ }
+
+}