2 * ============LICENSE_START==========================================
3 * Copyright (c) 2019 PANTHEON.tech s.r.o.
4 * ===================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
12 * OF ANY KIND, either express or implied. See the License for the specific language governing permissions and
13 * limitations under the License.
14 * ============LICENSE_END============================================
17 package org.onap.ccsdk.distribution.lighty;
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.config.RestConfConfiguration;
22 import io.lighty.modules.northbound.restconf.community.impl.util.RestConfConfigUtils;
23 import java.net.InetAddress;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
28 import java.util.concurrent.ExecutionException;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Main class of the CCSDK lighty.io application. In order to start the application run main method. Path to
37 * the configuration file can be provided as argument. If not, then default configuration will be used.
41 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
43 private ShutdownHook shutdownHook;
45 public static void main(String[] args) {
46 Main app = new Main();
47 app.start(args, true);
50 public void start(String[] args, boolean registerShutdownHook) {
51 long startTime = System.nanoTime();
52 LOG.info(".__ .__ .__ __ .__ _________ _________ _________________ "
54 LOG.info("| | |__| ____ | |___/ |_ ___.__. |__| ____ \\_ ___ \\\\_ ___ \\ / _____/\\___"
56 LOG.info("| | | |/ ___\\| | \\ __< | | | |/ _ \\ ______ / \\ \\// \\ \\/ \\_____ \\ "
58 LOG.info("| |_| / /_/ > Y \\ | \\___ | | ( <_> ) /_____/ \\ \\___\\ \\____/ \\ |"
60 LOG.info("|____/__\\___ /|___| /__| / ____| /\\ |__|\\____/ \\______ /\\______ /_______ //___"
61 + "____ /____|__ \\");
62 LOG.info(" /_____/ \\/ \\/ \\/ \\/ \\/ \\/ "
65 LOG.info("Starting lighty.io CCSDK application ...");
66 LOG.info("https://lighty.io/");
67 LOG.info("https://github.com/PantheonTechnologies/lighty-core");
69 if (args.length > 0) {
70 Path configPath = Paths.get(args[0]);
71 LOG.info("Using configuration from file {} ...", configPath);
72 //1. get controller configuration
73 ControllerConfiguration singleNodeConfiguration =
74 ControllerConfigUtils.getConfiguration(Files.newInputStream(configPath));
75 //2. get RESTCONF NBP configuration
76 RestConfConfiguration restConfConfiguration = RestConfConfigUtils
77 .getRestConfConfiguration(Files.newInputStream(configPath));
79 startLighty(singleNodeConfiguration, restConfConfiguration, registerShutdownHook);
81 LOG.info("Using default configuration ...");
82 Set<YangModuleInfo> modelPaths = Stream.concat(RestConfConfigUtils.YANG_MODELS.stream(),
83 CcsdkLightyModule.YANG_MODELS.stream()).collect(Collectors.toSet());
84 //1. get controller configuration
85 ControllerConfiguration defaultSingleNodeConfiguration =
86 ControllerConfigUtils.getDefaultSingleNodeConfiguration(modelPaths);
87 //2. get RESTCONF NBP configuration
88 RestConfConfiguration restConfConfig =
89 RestConfConfigUtils.getDefaultRestConfConfiguration();
90 restConfConfig.setInetAddress(InetAddress.getLocalHost());
91 restConfConfig.setHttpPort(8181);
93 startLighty(defaultSingleNodeConfiguration, restConfConfig, registerShutdownHook);
95 float duration = (System.nanoTime() - startTime)/1_000_000f;
96 LOG.info("lighty.io and CCSDK started in {}ms", duration);
97 } catch (Exception e) {
98 LOG.error("Main CCSDK lighty.io application exception: ", e);
102 private void startLighty(ControllerConfiguration singleNodeConfiguration,
103 RestConfConfiguration restConfConfiguration, boolean registerShutdownHook)
104 throws ExecutionException, InterruptedException {
105 CcsdkLightyApplication ccsdkLightyApplication = new CcsdkLightyApplication(singleNodeConfiguration,
106 restConfConfiguration);
108 if (registerShutdownHook) {
109 shutdownHook = new ShutdownHook(ccsdkLightyApplication);
110 Runtime.getRuntime().addShutdownHook(shutdownHook);
113 ccsdkLightyApplication.start().get();
116 private static class ShutdownHook extends Thread {
118 private static final Logger LOG = LoggerFactory.getLogger(ShutdownHook.class);
119 private final CcsdkLightyApplication ccsdkLightyApplication;
121 ShutdownHook(CcsdkLightyApplication ccsdkLightyApplication) {
122 this.ccsdkLightyApplication = ccsdkLightyApplication;
127 LOG.info("lighty.io and CCSDK shutting down ...");
128 long startTime = System.nanoTime();
130 ccsdkLightyApplication.shutdown();
131 } catch (Exception e) {
132 LOG.error("Exception while shutting down lighty.io CCSDK application:", e);
134 float duration = (System.nanoTime() - startTime)/1_000_000f;
135 LOG.info("lighty.io and CCSDK stopped in {}ms", duration);