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.JsonRestConfServiceType;
24 import io.lighty.modules.northbound.restconf.community.impl.config.RestConfConfiguration;
25 import io.lighty.modules.northbound.restconf.community.impl.util.RestConfConfigUtils;
26 import io.lighty.modules.southbound.netconf.impl.NetconfSBPlugin;
27 import io.lighty.modules.southbound.netconf.impl.NetconfTopologyPluginBuilder;
28 import io.lighty.modules.southbound.netconf.impl.config.NetconfConfiguration;
29 import io.lighty.modules.southbound.netconf.impl.util.NetconfConfigUtils;
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 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, null, null, false);
61 @SuppressWarnings("checkstyle:Illegalcatch")
62 public void start(String restConfConfigurationFile, boolean activateNbiNotification,
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 restConfConfig.setJsonRestconfServiceType(JsonRestConfServiceType.DRAFT_02);
85 // 3. NETCONF SBP configuration
86 NetconfConfiguration netconfSBPConfig = NetconfConfigUtils.createDefaultNetconfConfiguration();
87 startLighty(singleNodeConfiguration, restConfConfig, netconfSBPConfig, registerShutdownHook,
88 activateNbiNotification, 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 olmTimer1Option = Option.builder(OLMTIMER1_OPTION_NAME)
124 .desc("OLM timer 1 value")
125 .argName(OLMTIMER1_OPTION_NAME)
129 Option olmTimer2Option = Option.builder(OLMTIMER2_OPTION_NAME)
130 .desc("OLM timer 2 value")
131 .argName(OLMTIMER1_OPTION_NAME)
135 Options options = new Options();
136 options.addOption(restconfFileOption);
137 options.addOption(useNbiNotificationsOption);
138 options.addOption(olmTimer1Option);
139 options.addOption(olmTimer2Option);
143 private void startLighty(ControllerConfiguration controllerConfiguration,
144 RestConfConfiguration restConfConfiguration, NetconfConfiguration netconfSBPConfiguration,
145 boolean registerShutdownHook, boolean activateNbiNotification, String olmtimer1, String olmtimer2)
146 throws ConfigurationException, ExecutionException, InterruptedException {
148 // 1. initialize and start Lighty controller (MD-SAL, Controller, YangTools,
150 LightyControllerBuilder lightyControllerBuilder = new LightyControllerBuilder();
151 LightyController lightyController = lightyControllerBuilder.from(controllerConfiguration).build();
152 lightyController.start().get();
154 // 2. start RestConf server
155 LightyServerBuilder jettyServerBuilder = new LightyServerBuilder(
156 new InetSocketAddress(restConfConfiguration.getInetAddress(), restConfConfiguration.getHttpPort()));
157 CommunityRestConfBuilder communityRestConfBuilder = CommunityRestConfBuilder.from(
158 RestConfConfigUtils.getRestConfConfiguration(restConfConfiguration, lightyController.getServices()));
159 CommunityRestConf communityRestConf = communityRestConfBuilder.withLightyServer(jettyServerBuilder).build();
160 communityRestConf.start().get();
161 communityRestConf.startServer();
163 // 3. start NetConf SBP
164 NetconfSBPlugin netconfSouthboundPlugin;
165 netconfSBPConfiguration = NetconfConfigUtils.injectServicesToTopologyConfig(netconfSBPConfiguration,
166 lightyController.getServices());
167 NetconfTopologyPluginBuilder netconfSBPBuilder = new NetconfTopologyPluginBuilder(
168 lightyController.getServices(), netconfSBPConfiguration);
169 netconfSouthboundPlugin = netconfSBPBuilder.from(netconfSBPConfiguration, lightyController.getServices())
171 netconfSouthboundPlugin.start().get();
173 // 4. start TransportPCE beans
174 TransportPCE transportPCE = new TransportPCEImpl(lightyController.getServices(), activateNbiNotification,
175 olmtimer1, olmtimer2);
176 transportPCE.start().get();
178 // 5. Register shutdown hook for graceful shutdown.
179 shutdownHook = new ShutdownHook(lightyController, communityRestConf, netconfSouthboundPlugin, transportPCE);
180 if (registerShutdownHook) {
181 Runtime.getRuntime().addShutdownHook(shutdownHook);
185 public void shutdown() {
189 public static void main(String[] args) {
190 Options options = buildOptions();
192 CommandLine commandLine = new DefaultParser().parse(options, args);
193 String restConfConfigurationFile = commandLine.getOptionValue(RESTCONF_OPTION_NAME, null);
194 boolean useNbiNotifications = commandLine.hasOption(NBINOTIFICATION_OPTION_NAME);
195 String olmtimer1 = commandLine.getOptionValue(OLMTIMER1_OPTION_NAME, null);
196 String olmtimer2 = commandLine.getOptionValue(OLMTIMER2_OPTION_NAME, null);
197 Main app = new Main();
198 app.start(restConfConfigurationFile, useNbiNotifications, olmtimer1, olmtimer2, true);
199 } catch (ParseException e) {
200 HelpFormatter formatter = new HelpFormatter();
202 "java -ms<size> -mx<size> -XX:MaxMetaspaceSize=<size> -jar tpce.jar "
203 + "[-restconf <restconfConfigurationFile>] [-nbinotification]"
204 + " e.g. java -ms128m -mx512m -XX:MaxMetaspaceSize=128m -jar tpce.jar"
205 + "-restconf ../src/test/resources/config.json -nbinotification"
206 + "-olmtimer1 120000 -olmtimer2 20000",
212 private static class ShutdownHook extends Thread {
214 private static final Logger LOG = LoggerFactory.getLogger(ShutdownHook.class);
215 private final LightyController lightyController;
216 private final CommunityRestConf communityRestConf;
217 private final LightyModule netconfSouthboundPlugin;
218 private final TransportPCE transportPCE;
220 ShutdownHook(LightyController lightyController, CommunityRestConf communityRestConf,
221 LightyModule netconfSouthboundPlugin, TransportPCE transportPCE) {
222 this.lightyController = lightyController;
223 this.communityRestConf = communityRestConf;
224 this.netconfSouthboundPlugin = netconfSouthboundPlugin;
225 this.transportPCE = transportPCE;
229 @SuppressWarnings({"checkstyle:Illegalcatch", "checkstyle:VariableDeclarationUsageDistance"})
231 LOG.info("lighty.io and RESTCONF-NETCONF shutting down ...");
232 long startTime = System.nanoTime();
234 transportPCE.shutdown().get();
235 } catch (Exception e) {
236 LOG.error("Exception while shutting down TransportPCE: ", e);
239 communityRestConf.shutdown().get();
240 } catch (Exception e) {
241 LOG.error("Exception while shutting down RESTCONF: ", e);
244 netconfSouthboundPlugin.shutdown().get();
245 } catch (Exception e) {
246 LOG.error("Exception while shutting down NETCONF: ", e);
249 lightyController.shutdown().get();
250 } catch (Exception e) {
251 LOG.error("Exception while shutting down lighty.io controller:", e);
253 float duration = (System.nanoTime() - startTime) / 1_000_000f;
254 LOG.info("lighty.io and RESTCONF-NETCONF stopped in {}ms", duration);