fcc29ed29e847e28b6ec9523a3b23a38c8e6aa6d
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.monitoring.rest;
22
23 import java.io.PrintStream;
24
25 /**
26  * User: ewatkmi Date: 31 Jul 2017
27  */
28 public class ApexMonitoringRestMain {
29     // Services state
30     public enum ServicesState {
31         STOPPED, READY, INITIALIZING, RUNNING
32     };
33
34     private ServicesState state = ServicesState.STOPPED;
35
36     // The parameters for the client
37     private ApexMonitoringRestParameters parameters = null;
38
39     // Output and error streams for messages
40     private final PrintStream outStream;
41
42     // The Apex services client this class is running
43     private ApexMonitoringRest apexMonitoringRest = null;
44
45     /**
46      * Main method, main entry point for command
47      *
48      * @param args The command line arguments for the client
49      */
50     public static void main(final String[] args) {
51         try {
52             final ApexMonitoringRestMain restMain = new ApexMonitoringRestMain(args, System.out);
53             restMain.init();
54         } catch (final Exception e) {
55             System.err.println(e.getMessage());
56         }
57     }
58
59     /**
60      * Constructor, kicks off the rest service
61      *
62      * @param args The command line arguments for the RESTful service
63      * @param outStream The stream for output messages
64      */
65     public ApexMonitoringRestMain(final String[] args, final PrintStream outStream) {
66         // Save the streams for output and error
67         this.outStream = outStream;
68
69         // Client parameter parsing
70         final ApexMonitoringRestParameterParser parser = new ApexMonitoringRestParameterParser();
71
72         try {
73             // Get and check the parameters
74             parameters = parser.parse(args);
75         } catch (final ApexMonitoringRestParameterException e) {
76             throw new ApexMonitoringRestParameterException(
77                     "Apex Services REST endpoint (" + this.toString() + ") parameter error, " + e.getMessage() + '\n'
78                             + parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
79         }
80
81         if (parameters.isHelpSet()) {
82             throw new ApexMonitoringRestParameterException(
83                     parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
84         }
85
86         // Validate the parameters
87         final String validationMessage = parameters.validate();
88         if (validationMessage.length() > 0) {
89             throw new ApexMonitoringRestParameterException(
90                     "Apex Services REST endpoint (" + this.toString() + ") parameters invalid, " + validationMessage
91                             + '\n' + parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName()));
92         }
93
94         state = ServicesState.READY;
95     }
96
97     /**
98      * Initialize the rest service
99      */
100     public void init() {
101         outStream.println("Apex Services REST endpoint (" + this.toString() + ") starting at "
102                 + parameters.getBaseURI().toString() + " . . .");
103
104         try {
105             state = ServicesState.INITIALIZING;
106
107             // Start the REST service
108             apexMonitoringRest = new ApexMonitoringRest(parameters);
109
110             // Add a shutdown hook to shut down the rest services when the process is exiting
111             Runtime.getRuntime().addShutdownHook(new Thread(new ApexServicesShutdownHook()));
112
113             state = ServicesState.RUNNING;
114
115             if (parameters.getTimeToLive() == ApexMonitoringRestParameters.INFINITY_TIME_TO_LIVE) {
116                 outStream.println("Apex Services REST endpoint (" + this.toString() + ") started at "
117                         + parameters.getBaseURI().toString());
118             } else {
119                 outStream.println("Apex Services REST endpoint (" + this.toString() + ") started");
120             }
121
122             // Find out how long is left to wait
123             long timeRemaining = parameters.getTimeToLive();
124             while (timeRemaining == ApexMonitoringRestParameters.INFINITY_TIME_TO_LIVE || timeRemaining > 0) {
125                 // decrement the time to live in the non-infinity case
126                 if (timeRemaining > 0) {
127                     timeRemaining--;
128                 }
129
130                 // Wait for a second
131                 Thread.sleep(1000);
132             }
133         } catch (final Exception e) {
134             outStream.println(
135                     "Apex Services REST endpoint (" + this.toString() + ") failed at with error: " + e.getMessage());
136         } finally {
137             if (apexMonitoringRest != null) {
138                 apexMonitoringRest.shutdown();
139                 apexMonitoringRest = null;
140             }
141             state = ServicesState.STOPPED;
142         }
143
144     }
145
146     /**
147      * Get services state.
148      *
149      * @return the service state
150      */
151     public ServicesState getState() {
152         return state;
153     }
154
155     @Override
156     public String toString() {
157         final StringBuilder ret = new StringBuilder();
158         ret.append(this.getClass().getSimpleName()).append(": Config=[").append(this.parameters).append("], State=")
159                 .append(this.getState());
160         return ret.toString();
161     }
162
163     /**
164      * Explicitly shut down the services
165      */
166     public void shutdown() {
167         if (apexMonitoringRest != null) {
168             outStream.println("Apex Services REST endpoint (" + this.toString() + ") shutting down");
169             apexMonitoringRest.shutdown();
170         }
171         state = ServicesState.STOPPED;
172         outStream.println("Apex Services REST endpoint (" + this.toString() + ") shut down");
173     }
174
175     /**
176      * This class is a shutdown hook for the Apex services command
177      */
178     private class ApexServicesShutdownHook implements Runnable {
179         /*
180          * (non-Javadoc)
181          *
182          * @see java.lang.Runnable#run()
183          */
184         @Override
185         public void run() {
186             if (apexMonitoringRest != null) {
187                 apexMonitoringRest.shutdown();
188             }
189         }
190     }
191
192 }