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.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
42 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
44 private ShutdownHook shutdownHook;
47 start(new String[] {}, false);
50 @SuppressWarnings("checkstyle:Illegalcatch")
51 public void start(String[] args, boolean registerShutdownHook) {
52 long startTime = System.nanoTime();
54 RestConfConfiguration restConfConfig = null;
56 // 1. get controller configuration
57 ControllerConfiguration singleNodeConfiguration = ControllerConfigUtils
58 .getDefaultSingleNodeConfiguration(TPCEUtils.getYangModels());
59 // 2. get RESTCONF NBP configuration
60 if (args.length == 1) {
61 Path configPath = Paths.get(args[0]);
62 LOG.info("Using restconf configuration from file {} ...", configPath);
63 restConfConfig = RestConfConfigUtils.getRestConfConfiguration(Files.newInputStream(configPath));
66 LOG.info("Using default restconf configuration with http port 8181 ...");
68 restConfConfig = RestConfConfigUtils.getDefaultRestConfConfiguration();
69 restConfConfig.setHttpPort(8181);
72 restConfConfig.setJsonRestconfServiceType(JsonRestConfServiceType.DRAFT_02);
73 // 3. NETCONF SBP configuration
74 NetconfConfiguration netconfSBPConfig = NetconfConfigUtils.createDefaultNetconfConfiguration();
75 startLighty(singleNodeConfiguration, restConfConfig, netconfSBPConfig, registerShutdownHook);
76 float duration = (System.nanoTime() - startTime) / 1_000_000f;
77 LOG.info("lighty.io and RESTCONF-NETCONF started in {}ms", duration);
78 } catch (ConfigurationException | ExecutionException | IOException e) {
79 LOG.error("An error occured while starting application: ", e);
80 throw new TechnicalException("An error occured while starting application", e);
81 } catch (InterruptedException e) {
82 LOG.error("Application start interrupted : ", e);
83 Thread.currentThread().interrupt();
84 throw new TechnicalException("Application start interrupted", e);
86 } catch (Exception e) {
88 LOG.error("Application start unmanaged exception : ", e);
89 throw new TechnicalException("Application start unmanaged exception", e);
94 private void startLighty(ControllerConfiguration controllerConfiguration,
95 RestConfConfiguration restConfConfiguration, NetconfConfiguration netconfSBPConfiguration,
96 boolean registerShutdownHook) throws ConfigurationException, ExecutionException, InterruptedException {
98 // 1. initialize and start Lighty controller (MD-SAL, Controller, YangTools,
100 LightyControllerBuilder lightyControllerBuilder = new LightyControllerBuilder();
101 LightyController lightyController = lightyControllerBuilder.from(controllerConfiguration).build();
102 lightyController.start().get();
104 // 2. start RestConf server
105 LightyServerBuilder jettyServerBuilder = new LightyServerBuilder(
106 new InetSocketAddress(restConfConfiguration.getInetAddress(), restConfConfiguration.getHttpPort()));
107 CommunityRestConfBuilder communityRestConfBuilder = CommunityRestConfBuilder.from(
108 RestConfConfigUtils.getRestConfConfiguration(restConfConfiguration, lightyController.getServices()));
109 CommunityRestConf communityRestConf = communityRestConfBuilder.withLightyServer(jettyServerBuilder).build();
110 communityRestConf.start().get();
111 communityRestConf.startServer();
113 // 3. start NetConf SBP
114 NetconfSBPlugin netconfSouthboundPlugin;
115 netconfSBPConfiguration = NetconfConfigUtils.injectServicesToTopologyConfig(netconfSBPConfiguration,
116 lightyController.getServices());
117 NetconfTopologyPluginBuilder netconfSBPBuilder = new NetconfTopologyPluginBuilder(
118 lightyController.getServices(), netconfSBPConfiguration);
119 netconfSouthboundPlugin = netconfSBPBuilder.from(netconfSBPConfiguration, lightyController.getServices())
121 netconfSouthboundPlugin.start().get();
123 // 4. start TransportPCE beans
124 TransportPCE transportPCE = new TransportPCEImpl(lightyController.getServices());
125 transportPCE.start().get();
127 // 5. Register shutdown hook for graceful shutdown.
128 shutdownHook = new ShutdownHook(lightyController, communityRestConf, netconfSouthboundPlugin, transportPCE);
129 if (registerShutdownHook) {
130 Runtime.getRuntime().addShutdownHook(shutdownHook);
134 public void shutdown() {
138 public static void main(String[] args) {
139 Main app = new Main();
140 app.start(args, true);
143 private static class ShutdownHook extends Thread {
145 private static final Logger LOG = LoggerFactory.getLogger(ShutdownHook.class);
146 private final LightyController lightyController;
147 private final CommunityRestConf communityRestConf;
148 private final LightyModule netconfSouthboundPlugin;
149 private final TransportPCE transportPCE;
151 ShutdownHook(LightyController lightyController, CommunityRestConf communityRestConf,
152 LightyModule netconfSouthboundPlugin, TransportPCE transportPCE) {
153 this.lightyController = lightyController;
154 this.communityRestConf = communityRestConf;
155 this.netconfSouthboundPlugin = netconfSouthboundPlugin;
156 this.transportPCE = transportPCE;
160 @SuppressWarnings({"checkstyle:Illegalcatch", "checkstyle:VariableDeclarationUsageDistance"})
162 LOG.info("lighty.io and RESTCONF-NETCONF shutting down ...");
163 long startTime = System.nanoTime();
165 transportPCE.shutdown().get();
166 } catch (Exception e) {
167 LOG.error("Exception while shutting down TransportPCE: ", e);
170 communityRestConf.shutdown().get();
171 } catch (Exception e) {
172 LOG.error("Exception while shutting down RESTCONF: ", e);
175 netconfSouthboundPlugin.shutdown().get();
176 } catch (Exception e) {
177 LOG.error("Exception while shutting down NETCONF: ", e);
180 lightyController.shutdown().get();
181 } catch (Exception e) {
182 LOG.error("Exception while shutting down lighty.io controller:", e);
184 float duration = (System.nanoTime() - startTime) / 1_000_000f;
185 LOG.info("lighty.io and RESTCONF-NETCONF stopped in {}ms", duration);