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.module.TransportPCE;
11 import io.lighty.controllers.tpce.module.TransportPCEImpl;
12 import io.lighty.controllers.tpce.utils.TPCEUtils;
13 import io.lighty.core.controller.api.LightyController;
14 import io.lighty.core.controller.api.LightyModule;
15 import io.lighty.core.controller.impl.LightyControllerBuilder;
16 import io.lighty.core.controller.impl.config.ConfigurationException;
17 import io.lighty.core.controller.impl.config.ControllerConfiguration;
18 import io.lighty.core.controller.impl.util.ControllerConfigUtils;
19 import io.lighty.modules.northbound.restconf.community.impl.CommunityRestConf;
20 import io.lighty.modules.northbound.restconf.community.impl.CommunityRestConfBuilder;
21 import io.lighty.modules.northbound.restconf.community.impl.config.JsonRestConfServiceType;
22 import io.lighty.modules.northbound.restconf.community.impl.config.RestConfConfiguration;
23 import io.lighty.modules.northbound.restconf.community.impl.util.RestConfConfigUtils;
24 import io.lighty.modules.southbound.netconf.impl.NetconfSBPlugin;
25 import io.lighty.modules.southbound.netconf.impl.NetconfTopologyPluginBuilder;
26 import io.lighty.modules.southbound.netconf.impl.config.NetconfConfiguration;
27 import io.lighty.modules.southbound.netconf.impl.util.NetconfConfigUtils;
28 import io.lighty.server.LightyServerBuilder;
29 import java.net.InetSocketAddress;
30 import java.util.concurrent.ExecutionException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
36 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
38 private ShutdownHook shutdownHook;
41 start(new String[] {}, false);
44 public void start(String[] args, boolean registerShutdownHook) {
45 long startTime = System.nanoTime();
46 LOG.info(" ___________ ___________________ ___________");
47 LOG.info(" \\__ ___/ \\______ \\_ ___ \\\\_ _____/");
48 LOG.info(" | | ______ | ___/ \\ \\/ | __)_");
49 LOG.info(" | | /_____/ | | \\ \\____| \\");
50 LOG.info(" |____| |____| \\______ /_______ /");
52 LOG.info(".__ .__ .__ __ .__ ");
53 LOG.info("| | |__| ____ | |___/ |_ ___.__. |__| ____ ");
54 LOG.info("| | | |/ ___\\| | \\ __< | | | |/ _ \\");
55 LOG.info("| |_| / /_/ > Y \\ | \\___ | | ( <_> )");
56 LOG.info("|____/__\\___ /|___| /__| / ____| /\\ |__|\\____/");
57 LOG.info(" /_____/ \\/ \\/ \\/ ");
58 LOG.info("Starting lighty.io TransportPCE application ...");
59 LOG.info("https://lighty.io/");
60 LOG.info("https://github.com/PantheonTechnologies/lighty-core");
62 LOG.info("using default configuration ...");
63 //1. get controller configuration
64 ControllerConfiguration defaultSingleNodeConfiguration =
65 ControllerConfigUtils.getDefaultSingleNodeConfiguration(TPCEUtils.yangModels);
66 //2. get RESTCONF NBP configuration
67 RestConfConfiguration restConfConfig =
68 RestConfConfigUtils.getDefaultRestConfConfiguration();
69 restConfConfig.setJsonRestconfServiceType(JsonRestConfServiceType.DRAFT_02);
70 restConfConfig.setHttpPort(8181);
71 //3. NETCONF SBP configuration
72 NetconfConfiguration netconfSBPConfig = NetconfConfigUtils.createDefaultNetconfConfiguration();
73 startLighty(defaultSingleNodeConfiguration, restConfConfig, netconfSBPConfig, registerShutdownHook);
74 float duration = (System.nanoTime() - startTime) / 1_000_000f;
75 LOG.info("lighty.io and RESTCONF-NETCONF started in {}ms", duration);
76 } catch (Exception e) {
77 LOG.error("Main RESTCONF-NETCONF application exception: ", e);
81 private void startLighty(ControllerConfiguration controllerConfiguration,
82 RestConfConfiguration restConfConfiguration,
83 NetconfConfiguration netconfSBPConfiguration, boolean registerShutdownHook)
84 throws ConfigurationException, ExecutionException, InterruptedException {
86 //1. initialize and start Lighty controller (MD-SAL, Controller, YangTools, Akka)
87 LightyControllerBuilder lightyControllerBuilder = new LightyControllerBuilder();
88 LightyController lightyController = lightyControllerBuilder.from(controllerConfiguration).build();
89 lightyController.start().get();
91 //2. start RestConf server
92 CommunityRestConfBuilder communityRestConfBuilder = new CommunityRestConfBuilder();
93 LightyServerBuilder jettyServerBuilder = new LightyServerBuilder(new InetSocketAddress(
94 restConfConfiguration.getInetAddress(), restConfConfiguration.getHttpPort()));
95 CommunityRestConf communityRestConf = communityRestConfBuilder.from(RestConfConfigUtils
96 .getRestConfConfiguration(restConfConfiguration, lightyController.getServices()))
97 .withLightyServer(jettyServerBuilder)
99 communityRestConf.start().get();
100 communityRestConf.startServer();
102 //3. start NetConf SBP
103 NetconfSBPlugin netconfSouthboundPlugin;
104 netconfSBPConfiguration = NetconfConfigUtils.injectServicesToTopologyConfig(
105 netconfSBPConfiguration, lightyController.getServices());
106 NetconfTopologyPluginBuilder netconfSBPBuilder = new NetconfTopologyPluginBuilder();
107 netconfSouthboundPlugin = netconfSBPBuilder
108 .from(netconfSBPConfiguration, lightyController.getServices())
110 netconfSouthboundPlugin.start().get();
112 //4. start TransportPCE beans
113 TransportPCE transportPCE = new TransportPCEImpl(lightyController.getServices());
114 transportPCE.start().get();
116 //5. Register shutdown hook for graceful shutdown.
117 shutdownHook = new ShutdownHook(lightyController, communityRestConf, netconfSouthboundPlugin, transportPCE);
118 if (registerShutdownHook) {
119 Runtime.getRuntime().addShutdownHook(shutdownHook);
123 public void shutdown() {
127 public static void main(String[] args) {
128 Main app = new Main();
129 app.start(args, true);
132 private static class ShutdownHook extends Thread {
134 private static final Logger LOG = LoggerFactory.getLogger(ShutdownHook.class);
135 private final LightyController lightyController;
136 private final CommunityRestConf communityRestConf;
137 private final LightyModule netconfSouthboundPlugin;
138 private final TransportPCE transportPCE;
140 ShutdownHook(LightyController lightyController, CommunityRestConf communityRestConf,
141 LightyModule netconfSouthboundPlugin, TransportPCE transportPCE) {
142 this.lightyController = lightyController;
143 this.communityRestConf = communityRestConf;
144 this.netconfSouthboundPlugin = netconfSouthboundPlugin;
145 this.transportPCE = transportPCE;
150 LOG.info("lighty.io and RESTCONF-NETCONF shutting down ...");
151 long startTime = System.nanoTime();
153 transportPCE.shutdown().get();
154 } catch (Exception e) {
155 LOG.error("Exception while shutting down TransportPCE: ", e);
158 communityRestConf.shutdown().get();
159 } catch (Exception e) {
160 LOG.error("Exception while shutting down RESTCONF: ", e);
163 netconfSouthboundPlugin.shutdown().get();
164 } catch (Exception e) {
165 LOG.error("Exception while shutting down NETCONF: ", e);
168 lightyController.shutdown().get();
169 } catch (Exception e) {
170 LOG.error("Exception while shutting down lighty.io controller:", e);
172 float duration = (System.nanoTime() - startTime)/1_000_000f;
173 LOG.info("lighty.io and RESTCONF-NETCONF stopped in {}ms", duration);