Lighty refactor
[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.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;
39
40 public class Main {
41
42     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
43
44     private ShutdownHook shutdownHook;
45
46     public void start() {
47         start(new String[] {}, false);
48     }
49
50     @SuppressWarnings("checkstyle:Illegalcatch")
51     public void start(String[] args, boolean registerShutdownHook) {
52         long startTime = System.nanoTime();
53         TpceBanner.print();
54         RestConfConfiguration restConfConfig = null;
55         try {
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));
64
65             } else {
66                 LOG.info("Using default restconf configuration with http port 8181 ...");
67
68                 restConfConfig = RestConfConfigUtils.getDefaultRestConfConfiguration();
69                 restConfConfig.setHttpPort(8181);
70
71             }
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);
85           //CHECKSTYLE:OFF
86         } catch (Exception e) {
87           //CHECKSTYLE:ON
88             LOG.error("Application start unmanaged exception : ", e);
89             throw new TechnicalException("Application start unmanaged exception", e);
90
91         }
92     }
93
94     private void startLighty(ControllerConfiguration controllerConfiguration,
95             RestConfConfiguration restConfConfiguration, NetconfConfiguration netconfSBPConfiguration,
96             boolean registerShutdownHook) throws ConfigurationException, ExecutionException, InterruptedException {
97
98         // 1. initialize and start Lighty controller (MD-SAL, Controller, YangTools,
99         // Akka)
100         LightyControllerBuilder lightyControllerBuilder = new LightyControllerBuilder();
101         LightyController lightyController = lightyControllerBuilder.from(controllerConfiguration).build();
102         lightyController.start().get();
103
104         // 2. start RestConf server
105         CommunityRestConfBuilder communityRestConfBuilder = new CommunityRestConfBuilder();
106         LightyServerBuilder jettyServerBuilder = new LightyServerBuilder(
107                 new InetSocketAddress(restConfConfiguration.getInetAddress(), restConfConfiguration.getHttpPort()));
108         CommunityRestConf communityRestConf = communityRestConfBuilder.from(
109                 RestConfConfigUtils.getRestConfConfiguration(restConfConfiguration, lightyController.getServices()))
110                 .withLightyServer(jettyServerBuilder).build();
111         communityRestConf.start().get();
112         communityRestConf.startServer();
113
114         // 3. start NetConf SBP
115         NetconfSBPlugin netconfSouthboundPlugin;
116         netconfSBPConfiguration = NetconfConfigUtils.injectServicesToTopologyConfig(netconfSBPConfiguration,
117                 lightyController.getServices());
118         NetconfTopologyPluginBuilder netconfSBPBuilder = new NetconfTopologyPluginBuilder();
119         netconfSouthboundPlugin = netconfSBPBuilder.from(netconfSBPConfiguration, lightyController.getServices())
120                 .build();
121         netconfSouthboundPlugin.start().get();
122
123         // 4. start TransportPCE beans
124         TransportPCE transportPCE = new TransportPCEImpl(lightyController.getServices());
125         transportPCE.start().get();
126
127         // 5. Register shutdown hook for graceful shutdown.
128         shutdownHook = new ShutdownHook(lightyController, communityRestConf, netconfSouthboundPlugin, transportPCE);
129         if (registerShutdownHook) {
130             Runtime.getRuntime().addShutdownHook(shutdownHook);
131         }
132     }
133
134     public void shutdown() {
135         shutdownHook.run();
136     }
137
138     public static void main(String[] args) {
139         Main app = new Main();
140         app.start(args, true);
141     }
142
143     private static class ShutdownHook extends Thread {
144
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;
150
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;
157         }
158
159         @Override
160         @SuppressWarnings({"checkstyle:Illegalcatch", "checkstyle:VariableDeclarationUsageDistance"})
161         public void run() {
162             LOG.info("lighty.io and RESTCONF-NETCONF shutting down ...");
163             long startTime = System.nanoTime();
164             try {
165                 transportPCE.shutdown().get();
166             } catch (Exception e) {
167                 LOG.error("Exception while shutting down TransportPCE: ", e);
168             }
169             try {
170                 communityRestConf.shutdown().get();
171             } catch (Exception e) {
172                 LOG.error("Exception while shutting down RESTCONF: ", e);
173             }
174             try {
175                 netconfSouthboundPlugin.shutdown().get();
176             } catch (Exception e) {
177                 LOG.error("Exception while shutting down NETCONF: ", e);
178             }
179             try {
180                 lightyController.shutdown().get();
181             } catch (Exception e) {
182                 LOG.error("Exception while shutting down lighty.io controller:", e);
183             }
184             float duration = (System.nanoTime() - startTime) / 1_000_000f;
185             LOG.info("lighty.io and RESTCONF-NETCONF stopped in {}ms", duration);
186         }
187
188     }
189
190 }