Lighty support Silicon migration
[transportpce.git] / lighty / src / main / java / io / lighty / controllers / tpce / Main.java
index 19316784bd168584053537c4ae31a5be562023bc..a1b74f68f05838ae71c1eae650c63d872003cf9d 100644 (file)
@@ -7,9 +7,11 @@
  */
 package io.lighty.controllers.tpce;
 
+import io.lighty.controllers.tpce.exception.TechnicalException;
 import io.lighty.controllers.tpce.module.TransportPCE;
 import io.lighty.controllers.tpce.module.TransportPCEImpl;
 import io.lighty.controllers.tpce.utils.TPCEUtils;
+import io.lighty.controllers.tpce.utils.TpceBanner;
 import io.lighty.core.controller.api.LightyController;
 import io.lighty.core.controller.api.LightyModule;
 import io.lighty.core.controller.impl.LightyControllerBuilder;
@@ -26,94 +28,138 @@ import io.lighty.modules.southbound.netconf.impl.NetconfTopologyPluginBuilder;
 import io.lighty.modules.southbound.netconf.impl.config.NetconfConfiguration;
 import io.lighty.modules.southbound.netconf.impl.util.NetconfConfigUtils;
 import io.lighty.server.LightyServerBuilder;
+import java.io.IOException;
 import java.net.InetSocketAddress;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.concurrent.ExecutionException;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class Main {
 
+    private static final String RESTCONF_OPTION_NAME = "restconf";
+
+    private static final String NBINOTIFICATION_OPTION_NAME = "nbinotification";
+
     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
 
     private ShutdownHook shutdownHook;
 
     public void start() {
-        start(new String[] {}, false);
+        start(null, false, false);
     }
 
-    public void start(String[] args, boolean registerShutdownHook) {
+    @SuppressWarnings("checkstyle:Illegalcatch")
+    public void start(String restConfConfigurationFile, boolean activateNbiNotification, boolean registerShutdownHook) {
         long startTime = System.nanoTime();
-        LOG.info(" ___________     ___________________ ___________");
-        LOG.info(" \\__    ___/     \\______   \\_   ___ \\\\_   _____/");
-        LOG.info("   |    |  ______ |     ___/    \\  \\/ |    __)_");
-        LOG.info("   |    | /_____/ |    |   \\     \\____|        \\");
-        LOG.info("   |____|         |____|    \\______  /_______  /");
-        LOG.info("                                   \\/        \\/");
-        LOG.info(".__  .__       .__     __              .__          ");
-        LOG.info("|  | |__| ____ |  |___/  |_ ___.__.    |__| ____    ");
-        LOG.info("|  | |  |/ ___\\|  |  \\   __<   |  |    |  |/  _ \\");
-        LOG.info("|  |_|  / /_/  >   Y  \\  |  \\___  |    |  (  <_> )");
-        LOG.info("|____/__\\___  /|___|  /__|  / ____| /\\ |__|\\____/");
-        LOG.info("        /_____/     \\/      \\/      \\/           ");
-        LOG.info("Starting lighty.io TransportPCE application ...");
-        LOG.info("https://lighty.io/");
-        LOG.info("https://github.com/PantheonTechnologies/lighty-core");
+        TpceBanner.print();
+        RestConfConfiguration restConfConfig = null;
         try {
-            LOG.info("using default configuration ...");
-            //1. get controller configuration
-            ControllerConfiguration defaultSingleNodeConfiguration =
-                    ControllerConfigUtils.getDefaultSingleNodeConfiguration(TPCEUtils.yangModels);
-            //2. get RESTCONF NBP configuration
-            RestConfConfiguration restConfConfig =
-                    RestConfConfigUtils.getDefaultRestConfConfiguration();
+            // 1. get controller configuration
+            ControllerConfiguration singleNodeConfiguration = ControllerConfigUtils
+                    .getDefaultSingleNodeConfiguration(TPCEUtils.getYangModels());
+            // 2. get RESTCONF NBP configuration
+            if (restConfConfigurationFile != null) {
+                Path configPath = Paths.get(restConfConfigurationFile);
+                LOG.info("Using restconf configuration from file {} ...", configPath);
+                restConfConfig = RestConfConfigUtils.getRestConfConfiguration(Files.newInputStream(configPath));
+
+            } else {
+                LOG.info("Using default restconf configuration with http port 8181 ...");
+
+                restConfConfig = RestConfConfigUtils.getDefaultRestConfConfiguration();
+                restConfConfig.setHttpPort(8181);
+
+            }
             restConfConfig.setJsonRestconfServiceType(JsonRestConfServiceType.DRAFT_02);
-            restConfConfig.setHttpPort(8181);
-            //3. NETCONF SBP configuration
+            // 3. NETCONF SBP configuration
             NetconfConfiguration netconfSBPConfig = NetconfConfigUtils.createDefaultNetconfConfiguration();
-            startLighty(defaultSingleNodeConfiguration, restConfConfig, netconfSBPConfig, registerShutdownHook);
+            startLighty(singleNodeConfiguration, restConfConfig, netconfSBPConfig, registerShutdownHook,
+                    activateNbiNotification);
             float duration = (System.nanoTime() - startTime) / 1_000_000f;
             LOG.info("lighty.io and RESTCONF-NETCONF started in {}ms", duration);
+        } catch (ConfigurationException | ExecutionException | IOException e) {
+            LOG.error("An error occured while starting application: ", e);
+            throw new TechnicalException("An error occured while starting application", e);
+        } catch (InterruptedException e) {
+            LOG.error("Application start interrupted : ", e);
+            Thread.currentThread().interrupt();
+            throw new TechnicalException("Application start interrupted", e);
+          //CHECKSTYLE:OFF
         } catch (Exception e) {
-            LOG.error("Main RESTCONF-NETCONF application exception: ", e);
+          //CHECKSTYLE:ON
+            LOG.error("Application start unmanaged exception : ", e);
+            throw new TechnicalException("Application start unmanaged exception", e);
+
         }
     }
 
+    /**
+     * Build options for command line arguments.
+     * @return
+     */
+    private static Options buildOptions() {
+        Option restconfFileOption = Option.builder(RESTCONF_OPTION_NAME)
+                .desc("Restconf configuration file")
+                .argName(RESTCONF_OPTION_NAME)
+                .hasArg(true)
+                .required(false)
+                .build();
+        Option useNbiNotificationsOption = Option.builder(NBINOTIFICATION_OPTION_NAME)
+                .desc("Activate NBI notifications feature")
+                .argName(NBINOTIFICATION_OPTION_NAME)
+                .hasArg(false)
+                .required(false)
+                .build();
+        Options options = new Options();
+        options.addOption(restconfFileOption);
+        options.addOption(useNbiNotificationsOption);
+        return options;
+    }
+
     private void startLighty(ControllerConfiguration controllerConfiguration,
-                             RestConfConfiguration restConfConfiguration,
-                             NetconfConfiguration netconfSBPConfiguration, boolean registerShutdownHook)
-            throws ConfigurationException, ExecutionException, InterruptedException {
+            RestConfConfiguration restConfConfiguration, NetconfConfiguration netconfSBPConfiguration,
+            boolean registerShutdownHook, boolean activateNbiNotification)
+                    throws ConfigurationException, ExecutionException, InterruptedException {
 
-        //1. initialize and start Lighty controller (MD-SAL, Controller, YangTools, Akka)
+        // 1. initialize and start Lighty controller (MD-SAL, Controller, YangTools,
+        // Akka)
         LightyControllerBuilder lightyControllerBuilder = new LightyControllerBuilder();
         LightyController lightyController = lightyControllerBuilder.from(controllerConfiguration).build();
         lightyController.start().get();
 
-        //2. start RestConf server
-        CommunityRestConfBuilder communityRestConfBuilder = new CommunityRestConfBuilder();
-        LightyServerBuilder jettyServerBuilder = new LightyServerBuilder(new InetSocketAddress(
-                restConfConfiguration.getInetAddress(), restConfConfiguration.getHttpPort()));
-        CommunityRestConf communityRestConf = communityRestConfBuilder.from(RestConfConfigUtils
-                .getRestConfConfiguration(restConfConfiguration, lightyController.getServices()))
-                .withLightyServer(jettyServerBuilder)
-                .build();
+        // 2. start RestConf server
+        LightyServerBuilder jettyServerBuilder = new LightyServerBuilder(
+                new InetSocketAddress(restConfConfiguration.getInetAddress(), restConfConfiguration.getHttpPort()));
+        CommunityRestConfBuilder communityRestConfBuilder = CommunityRestConfBuilder.from(
+                RestConfConfigUtils.getRestConfConfiguration(restConfConfiguration, lightyController.getServices()));
+        CommunityRestConf communityRestConf = communityRestConfBuilder.withLightyServer(jettyServerBuilder).build();
         communityRestConf.start().get();
         communityRestConf.startServer();
 
-        //3. start NetConf SBP
+        // 3. start NetConf SBP
         NetconfSBPlugin netconfSouthboundPlugin;
-        netconfSBPConfiguration = NetconfConfigUtils.injectServicesToTopologyConfig(
-                netconfSBPConfiguration, lightyController.getServices());
-        NetconfTopologyPluginBuilder netconfSBPBuilder = new NetconfTopologyPluginBuilder();
-        netconfSouthboundPlugin = netconfSBPBuilder
-                .from(netconfSBPConfiguration, lightyController.getServices())
+        netconfSBPConfiguration = NetconfConfigUtils.injectServicesToTopologyConfig(netconfSBPConfiguration,
+                lightyController.getServices());
+        NetconfTopologyPluginBuilder netconfSBPBuilder = new NetconfTopologyPluginBuilder(
+                lightyController.getServices(), netconfSBPConfiguration);
+        netconfSouthboundPlugin = netconfSBPBuilder.from(netconfSBPConfiguration, lightyController.getServices())
                 .build();
         netconfSouthboundPlugin.start().get();
 
-        //4. start TransportPCE beans
-        TransportPCE transportPCE = new TransportPCEImpl(lightyController.getServices());
+        // 4. start TransportPCE beans
+        TransportPCE transportPCE = new TransportPCEImpl(lightyController.getServices(), activateNbiNotification);
         transportPCE.start().get();
 
-        //5. Register shutdown hook for graceful shutdown.
+        // 5. Register shutdown hook for graceful shutdown.
         shutdownHook = new ShutdownHook(lightyController, communityRestConf, netconfSouthboundPlugin, transportPCE);
         if (registerShutdownHook) {
             Runtime.getRuntime().addShutdownHook(shutdownHook);
@@ -125,8 +171,23 @@ public class Main {
     }
 
     public static void main(String[] args) {
-        Main app = new Main();
-        app.start(args, true);
+        Options options = buildOptions();
+        try {
+            CommandLine commandLine = new DefaultParser().parse(options, args);
+            String restConfConfigurationFile = commandLine.getOptionValue(RESTCONF_OPTION_NAME, null);
+            boolean useNbiNotifications = commandLine.hasOption(NBINOTIFICATION_OPTION_NAME);
+            Main app = new Main();
+            app.start(restConfConfigurationFile, useNbiNotifications, true);
+        } catch (ParseException e) {
+            HelpFormatter formatter = new HelpFormatter();
+            formatter.printHelp(
+                    "java -ms<size> -mx<size> -XX:MaxMetaspaceSize=<size> -jar tpce.jar "
+                    + "[-restconf <restconfConfigurationFile>] [-nbinotification]"
+                    + " e.g. java -ms128m -mx512m -XX:MaxMetaspaceSize=128m -jar tpce.jar"
+                    + "-restconf ../src/test/resources/config.json -nbinotification",
+                    options);
+            System.exit(1);
+        }
     }
 
     private static class ShutdownHook extends Thread {
@@ -138,7 +199,7 @@ public class Main {
         private final TransportPCE transportPCE;
 
         ShutdownHook(LightyController lightyController, CommunityRestConf communityRestConf,
-                     LightyModule netconfSouthboundPlugin, TransportPCE transportPCE) {
+                LightyModule netconfSouthboundPlugin, TransportPCE transportPCE) {
             this.lightyController = lightyController;
             this.communityRestConf = communityRestConf;
             this.netconfSouthboundPlugin = netconfSouthboundPlugin;
@@ -146,6 +207,7 @@ public class Main {
         }
 
         @Override
+        @SuppressWarnings({"checkstyle:Illegalcatch", "checkstyle:VariableDeclarationUsageDistance"})
         public void run() {
             LOG.info("lighty.io and RESTCONF-NETCONF shutting down ...");
             long startTime = System.nanoTime();
@@ -169,7 +231,7 @@ public class Main {
             } catch (Exception e) {
                 LOG.error("Exception while shutting down lighty.io controller:", e);
             }
-            float duration = (System.nanoTime() - startTime)/1_000_000f;
+            float duration = (System.nanoTime() - startTime) / 1_000_000f;
             LOG.info("lighty.io and RESTCONF-NETCONF stopped in {}ms", duration);
         }