lighty.io initializer
[transportpce.git] / lighty / src / main / java / io / lighty / controllers / tpce / Main.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies s.r.o. All Rights Reserved.
3  *
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
7  */
8 package io.lighty.controllers.tpce;
9
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;
33
34 public class Main {
35
36     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
37
38     private ShutdownHook shutdownHook;
39
40     public void start() {
41         start(new String[] {}, false);
42     }
43
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("   |____|         |____|    \\______  /_______  /");
51         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");
61         try {
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);
78         }
79     }
80
81     private void startLighty(ControllerConfiguration controllerConfiguration,
82                              RestConfConfiguration restConfConfiguration,
83                              NetconfConfiguration netconfSBPConfiguration, boolean registerShutdownHook)
84             throws ConfigurationException, ExecutionException, InterruptedException {
85
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();
90
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)
98                 .build();
99         communityRestConf.start().get();
100         communityRestConf.startServer();
101
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())
109                 .build();
110         netconfSouthboundPlugin.start().get();
111
112         //4. start TransportPCE beans
113         TransportPCE transportPCE = new TransportPCEImpl(lightyController.getServices());
114         transportPCE.start().get();
115
116         //5. Register shutdown hook for graceful shutdown.
117         shutdownHook = new ShutdownHook(lightyController, communityRestConf, netconfSouthboundPlugin, transportPCE);
118         if (registerShutdownHook) {
119             Runtime.getRuntime().addShutdownHook(shutdownHook);
120         }
121     }
122
123     public void shutdown() {
124         shutdownHook.run();
125     }
126
127     public static void main(String[] args) {
128         Main app = new Main();
129         app.start(args, true);
130     }
131
132     private static class ShutdownHook extends Thread {
133
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;
139
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;
146         }
147
148         @Override
149         public void run() {
150             LOG.info("lighty.io and RESTCONF-NETCONF shutting down ...");
151             long startTime = System.nanoTime();
152             try {
153                 transportPCE.shutdown().get();
154             } catch (Exception e) {
155                 LOG.error("Exception while shutting down TransportPCE: ", e);
156             }
157             try {
158                 communityRestConf.shutdown().get();
159             } catch (Exception e) {
160                 LOG.error("Exception while shutting down RESTCONF: ", e);
161             }
162             try {
163                 netconfSouthboundPlugin.shutdown().get();
164             } catch (Exception e) {
165                 LOG.error("Exception while shutting down NETCONF: ", e);
166             }
167             try {
168                 lightyController.shutdown().get();
169             } catch (Exception e) {
170                 LOG.error("Exception while shutting down lighty.io controller:", e);
171             }
172             float duration = (System.nanoTime() - startTime)/1_000_000f;
173             LOG.info("lighty.io and RESTCONF-NETCONF stopped in {}ms", duration);
174         }
175
176     }
177
178 }