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.server.LightyServerBuilder;
30 import java.io.IOException;
31 import java.net.InetSocketAddress;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.concurrent.ExecutionException;
36 import org.apache.commons.cli.CommandLine;
37 import org.apache.commons.cli.DefaultParser;
38 import org.apache.commons.cli.HelpFormatter;
39 import org.apache.commons.cli.Option;
40 import org.apache.commons.cli.Options;
41 import org.apache.commons.cli.ParseException;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
47 private static final String RESTCONF_OPTION_NAME = "restconf";
48 private static final String NBINOTIFICATION_OPTION_NAME = "nbinotification";
49 private static final String TAPI_OPTION_NAME = "tapi";
50 private static final String OLMTIMER1_OPTION_NAME = "olmtimer1";
51 private static final String OLMTIMER2_OPTION_NAME = "olmtimer2";
53 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
55 private ShutdownHook shutdownHook;
58 start(null, false, false, null, null, false);
61 @SuppressWarnings("checkstyle:Illegalcatch")
62 public void start(String restConfConfigurationFile, boolean activateNbiNotification, boolean activateTapi,
63 String olmtimer1, String olmtimer2, boolean registerShutdownHook) {
64 long startTime = System.nanoTime();
66 RestConfConfiguration restConfConfig = null;
68 // 1. get controller configuration
69 ControllerConfiguration singleNodeConfiguration = ControllerConfigUtils
70 .getDefaultSingleNodeConfiguration(TPCEUtils.getYangModels());
71 // 2. get RESTCONF NBP configuration
72 if (restConfConfigurationFile != null) {
73 Path configPath = Paths.get(restConfConfigurationFile);
74 LOG.info("Using restconf configuration from file {} ...", configPath);
75 restConfConfig = RestConfConfigUtils.getRestConfConfiguration(Files.newInputStream(configPath));
78 LOG.info("Using default restconf configuration with http port 8181 ...");
80 restConfConfig = RestConfConfigUtils.getDefaultRestConfConfiguration();
81 restConfConfig.setHttpPort(8181);
84 // 3. NETCONF SBP configuration
85 NetconfConfiguration netconfSBPConfig = NetconfConfigUtils.createDefaultNetconfConfiguration();
86 startLighty(singleNodeConfiguration, restConfConfig, netconfSBPConfig, registerShutdownHook,
87 activateNbiNotification, activateTapi, olmtimer1, olmtimer2);
88 float duration = (System.nanoTime() - startTime) / 1_000_000f;
89 LOG.info("lighty.io and RESTCONF-NETCONF started in {}ms", duration);
90 } catch (ConfigurationException | ExecutionException | IOException e) {
91 LOG.error("An error occured while starting application: ", e);
92 throw new TechnicalException("An error occured while starting application", e);
93 } catch (InterruptedException e) {
94 LOG.error("Application start interrupted : ", e);
95 Thread.currentThread().interrupt();
96 throw new TechnicalException("Application start interrupted", e);
98 } catch (Exception e) {
100 LOG.error("Application start unmanaged exception : ", e);
101 throw new TechnicalException("Application start unmanaged exception", e);
107 * Build options for command line arguments.
109 private static Options buildOptions() {
110 Option restconfFileOption = Option.builder(RESTCONF_OPTION_NAME)
111 .desc("Restconf configuration file")
112 .argName(RESTCONF_OPTION_NAME)
116 Option useNbiNotificationsOption = Option.builder(NBINOTIFICATION_OPTION_NAME)
117 .desc("Activate NBI notifications feature")
118 .argName(NBINOTIFICATION_OPTION_NAME)
122 Option useTapiOption = Option.builder(TAPI_OPTION_NAME)
123 .desc("Activate TAPI feature")
124 .argName(TAPI_OPTION_NAME)
128 Option olmTimer1Option = Option.builder(OLMTIMER1_OPTION_NAME)
129 .desc("OLM timer 1 value")
130 .argName(OLMTIMER1_OPTION_NAME)
134 Option olmTimer2Option = Option.builder(OLMTIMER2_OPTION_NAME)
135 .desc("OLM timer 2 value")
136 .argName(OLMTIMER1_OPTION_NAME)
140 Options options = new Options();
141 options.addOption(restconfFileOption);
142 options.addOption(useNbiNotificationsOption);
143 options.addOption(useTapiOption);
144 options.addOption(olmTimer1Option);
145 options.addOption(olmTimer2Option);
149 private void startLighty(ControllerConfiguration controllerConfiguration,
150 RestConfConfiguration restConfConfiguration, NetconfConfiguration netconfSBPConfiguration,
151 boolean registerShutdownHook, boolean activateNbiNotification, boolean activateTapi, String olmtimer1,
152 String olmtimer2) throws ConfigurationException, ExecutionException, InterruptedException {
154 // 1. initialize and start Lighty controller (MD-SAL, Controller, YangTools,
156 LightyControllerBuilder lightyControllerBuilder = new LightyControllerBuilder();
157 LightyController lightyController = lightyControllerBuilder.from(controllerConfiguration).build();
158 lightyController.start().get();
160 // 2. start RestConf server
161 LightyServerBuilder jettyServerBuilder = new LightyServerBuilder(
162 new InetSocketAddress(restConfConfiguration.getInetAddress(), restConfConfiguration.getHttpPort()));
163 CommunityRestConfBuilder communityRestConfBuilder = CommunityRestConfBuilder.from(
164 RestConfConfigUtils.getRestConfConfiguration(restConfConfiguration, lightyController.getServices()));
165 CommunityRestConf communityRestConf = communityRestConfBuilder.withLightyServer(jettyServerBuilder).build();
166 communityRestConf.start().get();
167 communityRestConf.startServer();
169 // 3. start NetConf SBP
170 NetconfSBPlugin netconfSouthboundPlugin;
171 netconfSBPConfiguration = NetconfConfigUtils.injectServicesToTopologyConfig(netconfSBPConfiguration,
172 lightyController.getServices());
173 NetconfTopologyPluginBuilder netconfSBPBuilder = new NetconfTopologyPluginBuilder(
174 lightyController.getServices(), netconfSBPConfiguration);
175 netconfSouthboundPlugin = netconfSBPBuilder.from(netconfSBPConfiguration, lightyController.getServices())
177 netconfSouthboundPlugin.start().get();
179 // 4. start TransportPCE beans
180 TransportPCE transportPCE = new TransportPCEImpl(lightyController.getServices(), activateNbiNotification,
181 activateTapi, olmtimer1, olmtimer2);
182 transportPCE.start().get();
184 // 5. Register shutdown hook for graceful shutdown.
185 shutdownHook = new ShutdownHook(lightyController, communityRestConf, netconfSouthboundPlugin, transportPCE);
186 if (registerShutdownHook) {
187 Runtime.getRuntime().addShutdownHook(shutdownHook);
191 public void shutdown() {
195 public static void main(String[] args) {
196 Options options = buildOptions();
198 CommandLine commandLine = new DefaultParser().parse(options, args);
199 String restConfConfigurationFile = commandLine.getOptionValue(RESTCONF_OPTION_NAME, null);
200 boolean useNbiNotifications = commandLine.hasOption(NBINOTIFICATION_OPTION_NAME);
201 boolean useTapi = commandLine.hasOption(TAPI_OPTION_NAME);
202 String olmtimer1 = commandLine.getOptionValue(OLMTIMER1_OPTION_NAME, null);
203 String olmtimer2 = commandLine.getOptionValue(OLMTIMER2_OPTION_NAME, null);
204 Main app = new Main();
205 app.start(restConfConfigurationFile, useNbiNotifications, useTapi, olmtimer1, olmtimer2, true);
206 } catch (ParseException e) {
207 HelpFormatter formatter = new HelpFormatter();
209 "java -ms<size> -mx<size> -XX:MaxMetaspaceSize=<size> -jar tpce.jar "
210 + "[-restconf <restconfConfigurationFile>] [-nbinotification]"
211 + " e.g. java -ms128m -mx512m -XX:MaxMetaspaceSize=128m -jar tpce.jar"
212 + "-restconf ../src/test/resources/config.json -nbinotification"
213 + "-olmtimer1 120000 -olmtimer2 20000",
219 private static class ShutdownHook extends Thread {
221 private static final Logger LOG = LoggerFactory.getLogger(ShutdownHook.class);
222 private final LightyController lightyController;
223 private final CommunityRestConf communityRestConf;
224 private final LightyModule netconfSouthboundPlugin;
225 private final TransportPCE transportPCE;
227 ShutdownHook(LightyController lightyController, CommunityRestConf communityRestConf,
228 LightyModule netconfSouthboundPlugin, TransportPCE transportPCE) {
229 this.lightyController = lightyController;
230 this.communityRestConf = communityRestConf;
231 this.netconfSouthboundPlugin = netconfSouthboundPlugin;
232 this.transportPCE = transportPCE;
236 @SuppressWarnings({"checkstyle:Illegalcatch", "checkstyle:VariableDeclarationUsageDistance"})
238 LOG.info("lighty.io and RESTCONF-NETCONF shutting down ...");
239 long startTime = System.nanoTime();
241 transportPCE.shutdown().get();
242 } catch (Exception e) {
243 LOG.error("Exception while shutting down TransportPCE: ", e);
246 communityRestConf.shutdown().get();
247 } catch (Exception e) {
248 LOG.error("Exception while shutting down RESTCONF: ", e);
251 netconfSouthboundPlugin.shutdown().get();
252 } catch (Exception e) {
253 LOG.error("Exception while shutting down NETCONF: ", e);
256 lightyController.shutdown().get();
257 } catch (Exception e) {
258 LOG.error("Exception while shutting down lighty.io controller:", e);
260 float duration = (System.nanoTime() - startTime) / 1_000_000f;
261 LOG.info("lighty.io and RESTCONF-NETCONF stopped in {}ms", duration);