a0e6641db40f5c58bd95384d7afdcd69fa5aee5b
[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.deployment.rest;
22
23 import java.io.PrintStream;
24
25 /**
26  * The main class for ApexDeploymentRest.
27  *
28  * @author Michael Watkins (michael.watkins@ericsson.com)
29  */
30 public class ApexDeploymentRestMain {
31     // Services state
32     public enum ServicesState {
33         STOPPED, READY, INITIALIZING, RUNNING
34     }
35
36     private ServicesState state = ServicesState.STOPPED;
37
38     // The parameters for the client
39     private ApexDeploymentRestParameters parameters = null;
40
41     // Output and error streams for messages
42     private final PrintStream outStream;
43
44     // The Apex services client this class is running
45     private ApexDeploymentRest apexDeploymentRest = null;
46
47     /**
48      * Main method, main entry point for command.
49      *
50      * @param args The command line arguments for the client
51      */
52     public static void main(final String[] args) {
53         try {
54             final ApexDeploymentRestMain restMain = new ApexDeploymentRestMain(args, System.out);
55             restMain.init();
56         } catch (final Exception e) {
57             System.err.println(e.getMessage());
58         }
59     }
60
61     /**
62      * Constructor, kicks off the rest service.
63      *
64      * @param args The command line arguments for the RESTful service
65      * @param outStream The stream for output messages
66      */
67     public ApexDeploymentRestMain(final String[] args, final PrintStream outStream) {
68         // Save the streams for output and error
69         this.outStream = outStream;
70
71         // Client parameter parsing
72         final ApexDeploymentRestParameterParser parser = new ApexDeploymentRestParameterParser();
73
74         try {
75             // Get and check the parameters
76             parameters = parser.parse(args);
77         } catch (final ApexDeploymentRestParameterException e) {
78             throw new ApexDeploymentRestParameterException(
79                     "Apex Services REST endpoint (" + this.toString() + ") parameter error, " + e.getMessage() + '\n'
80                             + parser.getHelp(ApexDeploymentRestMain.class.getCanonicalName()));
81         }
82
83         if (parameters.isHelpSet()) {
84             throw new ApexDeploymentRestParameterException(
85                     parser.getHelp(ApexDeploymentRestMain.class.getCanonicalName()));
86         }
87
88         // Validate the parameters
89         final String validationMessage = parameters.validate();
90         if (validationMessage.length() > 0) {
91             throw new ApexDeploymentRestParameterException(
92                     "Apex Services REST endpoint (" + this.toString() + ") parameters invalid, " + validationMessage
93                             + '\n' + parser.getHelp(ApexDeploymentRestMain.class.getCanonicalName()));
94         }
95
96         state = ServicesState.READY;
97     }
98
99     /**
100      * Initialize the rest service.
101      */
102     public void init() {
103         outStream.println("Apex Services REST endpoint (" + this.toString() + ") starting at "
104                 + parameters.getBaseURI().toString() + " . . .");
105
106         try {
107             state = ServicesState.INITIALIZING;
108
109             // Start the REST service
110             apexDeploymentRest = new ApexDeploymentRest(parameters);
111
112             // Add a shutdown hook to shut down the rest services when the process is exiting
113             Runtime.getRuntime().addShutdownHook(new Thread(new ApexServicesShutdownHook()));
114
115             state = ServicesState.RUNNING;
116
117             if (parameters.getTimeToLive() == ApexDeploymentRestParameters.INFINITY_TIME_TO_LIVE) {
118                 outStream.println("Apex Services REST endpoint (" + this.toString() + ") started at "
119                         + parameters.getBaseURI().toString());
120             } else {
121                 outStream.println("Apex Services REST endpoint (" + this.toString() + ") started");
122             }
123
124             // Find out how long is left to wait
125             long timeRemaining = parameters.getTimeToLive();
126             while (timeRemaining == ApexDeploymentRestParameters.INFINITY_TIME_TO_LIVE || timeRemaining > 0) {
127                 // decrement the time to live in the non-infinity case
128                 if (timeRemaining > 0) {
129                     timeRemaining--;
130                 }
131
132                 // Wait for a second
133                 Thread.sleep(1000);
134             }
135         } catch (final Exception e) {
136             outStream.println(
137                     "Apex Services REST endpoint (" + this.toString() + ") failed at with error: " + e.getMessage());
138         } finally {
139             if (apexDeploymentRest != null) {
140                 apexDeploymentRest.shutdown();
141                 apexDeploymentRest = null;
142             }
143             state = ServicesState.STOPPED;
144         }
145
146     }
147
148     /**
149      * Get services state.
150      *
151      * @return the service state
152      */
153     public ServicesState getState() {
154         return state;
155     }
156
157     @Override
158     public String toString() {
159         final StringBuilder ret = new StringBuilder();
160         ret.append(this.getClass().getSimpleName()).append(": Config=[").append(this.parameters).append("], State=")
161                 .append(this.getState());
162         return ret.toString();
163     }
164
165     /**
166      * Explicitly shut down the services.
167      */
168     public void shutdown() {
169         if (apexDeploymentRest != null) {
170             outStream.println("Apex Services REST endpoint (" + this.toString() + ") shutting down");
171             apexDeploymentRest.shutdown();
172         }
173         state = ServicesState.STOPPED;
174         outStream.println("Apex Services REST endpoint (" + this.toString() + ") shut down");
175     }
176
177     /**
178      * This class is a shutdown hook for the Apex services command.
179      */
180     private class ApexServicesShutdownHook implements Runnable {
181         /*
182          * (non-Javadoc)
183          *
184          * @see java.lang.Runnable#run()
185          */
186         @Override
187         public void run() {
188             if (apexDeploymentRest != null) {
189                 apexDeploymentRest.shutdown();
190             }
191         }
192     }
193
194 }