Merge "Release version 0.6.0"
[ccsdk/distribution.git] / lighty / ccsdk-lighty-distribution / src / main / java / org / onap / ccsdk / distribution / lighty / Main.java
1 /*
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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============================================
15  *
16  */
17 package org.onap.ccsdk.distribution.lighty;
18
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;
27 import java.util.Set;
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;
34
35 /**
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.
38  */
39 public class Main {
40
41     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
42
43     private ShutdownHook shutdownHook;
44
45     public static void main(String[] args) {
46         Main app = new Main();
47         app.start(args, true);
48     }
49
50     public void start(String[] args, boolean registerShutdownHook) {
51         long startTime = System.nanoTime();
52         LOG.info(".__  .__       .__     __              .__                   _________ _________   _________________ "
53                 + "  ____  __.");
54         LOG.info("|  | |__| ____ |  |___/  |_ ___.__.    |__| ____             \\_   ___ \\\\_   ___ \\ /   _____/\\___"
55                 + "___ \\ |    |/ _|");
56         LOG.info("|  | |  |/ ___\\|  |  \\   __<   |  |    |  |/  _ \\    ______  /    \\  \\//    \\  \\/ \\_____  \\ "
57                 + " |    |  \\|      <");
58         LOG.info("|  |_|  / /_/  >   Y  \\  |  \\___  |    |  (  <_> )  /_____/  \\     \\___\\     \\____/        \\ |"
59                 + "    `   \\    |  \\");
60         LOG.info("|____/__\\___  /|___|  /__|  / ____| /\\ |__|\\____/             \\______  /\\______  /_______  //___"
61                 + "____  /____|__ \\");
62         LOG.info("       /_____/      \\/      \\/      \\/                               \\/        \\/        \\/    "
63                 + "     \\/        \\/");
64
65         LOG.info("Starting lighty.io CCSDK application ...");
66         LOG.info("https://lighty.io/");
67         LOG.info("https://github.com/PantheonTechnologies/lighty-core");
68         try {
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));
78                 //3. start lighty
79                 startLighty(singleNodeConfiguration, restConfConfiguration, registerShutdownHook);
80             } else {
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);
92                 //3. start lighty
93                 startLighty(defaultSingleNodeConfiguration, restConfConfig, registerShutdownHook);
94             }
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);
99         }
100     }
101
102     private void startLighty(ControllerConfiguration singleNodeConfiguration,
103             RestConfConfiguration restConfConfiguration, boolean registerShutdownHook)
104             throws ExecutionException, InterruptedException {
105         CcsdkLightyApplication ccsdkLightyApplication = new CcsdkLightyApplication(singleNodeConfiguration,
106                 restConfConfiguration);
107
108         if (registerShutdownHook) {
109             shutdownHook = new ShutdownHook(ccsdkLightyApplication);
110             Runtime.getRuntime().addShutdownHook(shutdownHook);
111         }
112
113         ccsdkLightyApplication.start().get();
114     }
115
116     private static class ShutdownHook extends Thread {
117
118         private static final Logger LOG = LoggerFactory.getLogger(ShutdownHook.class);
119         private final CcsdkLightyApplication ccsdkLightyApplication;
120
121         ShutdownHook(CcsdkLightyApplication ccsdkLightyApplication) {
122             this.ccsdkLightyApplication = ccsdkLightyApplication;
123         }
124
125         @Override
126         public void run() {
127             LOG.info("lighty.io and CCSDK shutting down ...");
128             long startTime = System.nanoTime();
129             try {
130                 ccsdkLightyApplication.shutdown();
131             } catch (Exception e) {
132                 LOG.error("Exception while shutting down lighty.io CCSDK application:", e);
133             }
134             float duration = (System.nanoTime() - startTime)/1_000_000f;
135             LOG.info("lighty.io and CCSDK stopped in {}ms", duration);
136         }
137
138     }
139 }