2 * Copyright (c) 2018 Pantheon Technologies s.r.o. All Rights Reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at https://www.eclipse.org/legal/epl-v10.html
8 package io.lighty.controllers.tpce;
10 import io.lighty.controllers.tpce.exception.TechnicalException;
11 import io.lighty.controllers.tpce.module.TransportPCE;
12 import io.lighty.controllers.tpce.module.TransportPCEImpl;
13 import io.lighty.controllers.tpce.utils.TPCEUtils;
14 import io.lighty.controllers.tpce.utils.TpceBanner;
15 import io.lighty.core.controller.api.LightyController;
16 import io.lighty.core.controller.api.LightyModule;
17 import io.lighty.core.controller.impl.LightyControllerBuilder;
18 import io.lighty.core.controller.impl.config.ConfigurationException;
19 import io.lighty.core.controller.impl.config.ControllerConfiguration;
20 import io.lighty.core.controller.impl.util.ControllerConfigUtils;
21 import io.lighty.modules.northbound.restconf.community.impl.CommunityRestConf;
22 import io.lighty.modules.northbound.restconf.community.impl.CommunityRestConfBuilder;
23 import io.lighty.modules.northbound.restconf.community.impl.config.RestConfConfiguration;
24 import io.lighty.modules.northbound.restconf.community.impl.util.RestConfConfigUtils;
25 import io.lighty.modules.southbound.netconf.impl.NetconfSBPlugin;
26 import io.lighty.modules.southbound.netconf.impl.NetconfTopologyPluginBuilder;
27 import io.lighty.modules.southbound.netconf.impl.config.NetconfConfiguration;
28 import io.lighty.modules.southbound.netconf.impl.util.NetconfConfigUtils;
29 import io.lighty.openapi.OpenApiLighty;
30 import io.lighty.server.LightyServerBuilder;
31 import java.io.IOException;
32 import java.net.InetSocketAddress;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.concurrent.ExecutionException;
37 import org.apache.commons.cli.CommandLine;
38 import org.apache.commons.cli.DefaultParser;
39 import org.apache.commons.cli.HelpFormatter;
40 import org.apache.commons.cli.Option;
41 import org.apache.commons.cli.Options;
42 import org.apache.commons.cli.ParseException;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
48 private static final String RESTCONF_OPTION_NAME = "restconf";
49 private static final String NBINOTIFICATION_OPTION_NAME = "nbinotification";
50 private static final String TAPI_OPTION_NAME = "tapi";
51 private static final String OLMTIMER1_OPTION_NAME = "olmtimer1";
52 private static final String OLMTIMER2_OPTION_NAME = "olmtimer2";
54 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
56 private ShutdownHook shutdownHook;
59 start(null, false, false, null, null, false);
62 @SuppressWarnings("checkstyle:Illegalcatch")
63 public void start(String restConfConfigurationFile, boolean activateNbiNotification, boolean activateTapi,
64 String olmtimer1, String olmtimer2, boolean registerShutdownHook) {
65 long startTime = System.nanoTime();
67 RestConfConfiguration restConfConfig = null;
69 // 1. get controller configuration
70 ControllerConfiguration singleNodeConfiguration = ControllerConfigUtils
71 .getDefaultSingleNodeConfiguration(TPCEUtils.getYangModels());
72 // 2. get RESTCONF NBP configuration
73 if (restConfConfigurationFile != null) {
74 Path configPath = Paths.get(restConfConfigurationFile);
75 LOG.info("Using restconf configuration from file {} ...", configPath);
76 restConfConfig = RestConfConfigUtils.getRestConfConfiguration(Files.newInputStream(configPath));
79 LOG.info("Using default restconf configuration with http port 8181 ...");
81 restConfConfig = RestConfConfigUtils.getDefaultRestConfConfiguration();
82 restConfConfig.setHttpPort(8181);
85 // 3. NETCONF SBP configuration
86 NetconfConfiguration netconfSBPConfig = NetconfConfigUtils.createDefaultNetconfConfiguration();
87 startLighty(singleNodeConfiguration, restConfConfig, netconfSBPConfig, registerShutdownHook,
88 activateNbiNotification, activateTapi, olmtimer1, olmtimer2);
89 float duration = (System.nanoTime() - startTime) / 1_000_000f;
90 LOG.info("lighty.io and RESTCONF-NETCONF started in {}ms", duration);
91 } catch (ConfigurationException | ExecutionException | IOException e) {
92 LOG.error("An error occured while starting application: ", e);
93 throw new TechnicalException("An error occured while starting application", e);
94 } catch (InterruptedException e) {
95 LOG.error("Application start interrupted : ", e);
96 Thread.currentThread().interrupt();
97 throw new TechnicalException("Application start interrupted", e);
99 } catch (Exception e) {
101 LOG.error("Application start unmanaged exception : ", e);
102 throw new TechnicalException("Application start unmanaged exception", e);
108 * Build options for command line arguments.
110 private static Options buildOptions() {
111 Option restconfFileOption = Option.builder(RESTCONF_OPTION_NAME)
112 .desc("Restconf configuration file")
113 .argName(RESTCONF_OPTION_NAME)
117 Option useNbiNotificationsOption = Option.builder(NBINOTIFICATION_OPTION_NAME)
118 .desc("Activate NBI notifications feature")
119 .argName(NBINOTIFICATION_OPTION_NAME)
123 Option useTapiOption = Option.builder(TAPI_OPTION_NAME)
124 .desc("Activate TAPI feature")
125 .argName(TAPI_OPTION_NAME)
129 Option olmTimer1Option = Option.builder(OLMTIMER1_OPTION_NAME)
130 .desc("OLM timer 1 value")
131 .argName(OLMTIMER1_OPTION_NAME)
135 Option olmTimer2Option = Option.builder(OLMTIMER2_OPTION_NAME)
136 .desc("OLM timer 2 value")
137 .argName(OLMTIMER1_OPTION_NAME)
141 Options options = new Options();
142 options.addOption(restconfFileOption);
143 options.addOption(useNbiNotificationsOption);
144 options.addOption(useTapiOption);
145 options.addOption(olmTimer1Option);
146 options.addOption(olmTimer2Option);
150 private void startLighty(ControllerConfiguration controllerConfiguration,
151 RestConfConfiguration restConfConfiguration, NetconfConfiguration netconfSBPConfiguration,
152 boolean registerShutdownHook, boolean activateNbiNotification, boolean activateTapi, String olmtimer1,
153 String olmtimer2) throws ConfigurationException, ExecutionException, InterruptedException {
155 // 1. initialize and start Lighty controller (MD-SAL, Controller, YangTools,
157 LightyControllerBuilder lightyControllerBuilder = new LightyControllerBuilder();
158 LightyController lightyController = lightyControllerBuilder.from(controllerConfiguration).build();
159 lightyController.start().get();
161 // 2. Start swagger server
162 LightyServerBuilder jettyServerBuilder = new LightyServerBuilder(
163 new InetSocketAddress(restConfConfiguration.getInetAddress(), restConfConfiguration.getHttpPort()));
164 CommunityRestConfBuilder communityRestConfBuilder = CommunityRestConfBuilder.from(
165 RestConfConfigUtils.getRestConfConfiguration(restConfConfiguration, lightyController.getServices()));
166 OpenApiLighty swagger = new OpenApiLighty(restConfConfiguration, jettyServerBuilder,
167 lightyController.getServices());
168 swagger.start().get();
170 // 3. start RestConf server
171 CommunityRestConf communityRestConf = communityRestConfBuilder.withLightyServer(jettyServerBuilder).build();
172 communityRestConf.start().get();
173 communityRestConf.startServer();
175 // 4. start NetConf SBP
176 NetconfSBPlugin netconfSouthboundPlugin;
177 netconfSBPConfiguration = NetconfConfigUtils.injectServicesToTopologyConfig(netconfSBPConfiguration,
178 lightyController.getServices());
179 NetconfTopologyPluginBuilder netconfSBPBuilder = new NetconfTopologyPluginBuilder(
180 lightyController.getServices(), netconfSBPConfiguration);
181 netconfSouthboundPlugin = netconfSBPBuilder.from(netconfSBPConfiguration, lightyController.getServices())
183 netconfSouthboundPlugin.start().get();
185 // 5. start TransportPCE beans
186 TransportPCE transportPCE = new TransportPCEImpl(lightyController.getServices(), activateNbiNotification,
187 activateTapi, olmtimer1, olmtimer2);
188 transportPCE.start().get();
190 // 6. Register shutdown hook for graceful shutdown.
191 shutdownHook = new ShutdownHook(lightyController, communityRestConf, netconfSouthboundPlugin, transportPCE);
192 if (registerShutdownHook) {
193 Runtime.getRuntime().addShutdownHook(shutdownHook);
197 public void shutdown() {
201 public static void main(String[] args) {
202 Options options = buildOptions();
204 CommandLine commandLine = new DefaultParser().parse(options, args);
205 String restConfConfigurationFile = commandLine.getOptionValue(RESTCONF_OPTION_NAME, null);
206 boolean useNbiNotifications = commandLine.hasOption(NBINOTIFICATION_OPTION_NAME);
207 boolean useTapi = commandLine.hasOption(TAPI_OPTION_NAME);
208 String olmtimer1 = commandLine.getOptionValue(OLMTIMER1_OPTION_NAME, null);
209 String olmtimer2 = commandLine.getOptionValue(OLMTIMER2_OPTION_NAME, null);
210 Main app = new Main();
211 app.start(restConfConfigurationFile, useNbiNotifications, useTapi, olmtimer1, olmtimer2, true);
212 } catch (ParseException e) {
213 HelpFormatter formatter = new HelpFormatter();
215 "java -ms<size> -mx<size> -XX:MaxMetaspaceSize=<size> -jar tpce.jar "
216 + "[-restconf <restconfConfigurationFile>] [-nbinotification]"
217 + " e.g. java -ms128m -mx512m -XX:MaxMetaspaceSize=128m -jar tpce.jar"
218 + "-restconf ../src/test/resources/config.json -nbinotification"
219 + "-olmtimer1 120000 -olmtimer2 20000",
225 private static class ShutdownHook extends Thread {
227 private static final Logger LOG = LoggerFactory.getLogger(ShutdownHook.class);
228 private final LightyController lightyController;
229 private final CommunityRestConf communityRestConf;
230 private final LightyModule netconfSouthboundPlugin;
231 private final TransportPCE transportPCE;
233 ShutdownHook(LightyController lightyController, CommunityRestConf communityRestConf,
234 LightyModule netconfSouthboundPlugin, TransportPCE transportPCE) {
235 this.lightyController = lightyController;
236 this.communityRestConf = communityRestConf;
237 this.netconfSouthboundPlugin = netconfSouthboundPlugin;
238 this.transportPCE = transportPCE;
242 @SuppressWarnings({"checkstyle:Illegalcatch", "checkstyle:VariableDeclarationUsageDistance"})
244 LOG.info("lighty.io and RESTCONF-NETCONF shutting down ...");
245 long startTime = System.nanoTime();
247 transportPCE.shutdown().get();
248 } catch (Exception e) {
249 LOG.error("Exception while shutting down TransportPCE: ", e);
252 communityRestConf.shutdown().get();
253 } catch (Exception e) {
254 LOG.error("Exception while shutting down RESTCONF: ", e);
257 netconfSouthboundPlugin.shutdown().get();
258 } catch (Exception e) {
259 LOG.error("Exception while shutting down NETCONF: ", e);
262 lightyController.shutdown().get();
263 } catch (Exception e) {
264 LOG.error("Exception while shutting down lighty.io controller:", e);
266 float duration = (System.nanoTime() - startTime) / 1_000_000f;
267 LOG.info("lighty.io and RESTCONF-NETCONF stopped in {}ms", duration);