Merge "Try new version of JRuby"
[policy/apex-pdp.git] / client / client-deployment / src / main / java / org / onap / policy / apex / client / deployment / rest / ApexDeploymentRestParameters.java
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.net.URI;
24
25 /**
26  * This class reads and handles command line parameters to the Apex RESTful services.
27  *
28  * @author Michael Watkins (michael.watkins@ericsson.com)
29  */
30 public class ApexDeploymentRestParameters {
31     public static final int DEFAULT_REST_PORT = 18989;
32     public static final int INFINITY_TIME_TO_LIVE = -1;
33
34     // Base URI the HTTP server will listen on
35     private static final String DEFAULT_SERVER_URI_ROOT = "http://localhost:";
36     private static final String DEFAULT_REST_PATH = "/apexservices/";
37     private static final String DEFAULT_STATIC_PATH = "/";
38
39     // Package that will field REST requests
40     private static final String[] DEFAULT_PACKAGES = new String[] {"org.onap.policy.apex.client.deployment.rest"};
41
42     // The services parameters
43     private boolean helpSet = false;
44     private int restPort = DEFAULT_REST_PORT;
45     private long timeToLive = INFINITY_TIME_TO_LIVE;
46
47     /**
48      * Validate the parameters.
49      *
50      * @return the result of the validation
51      */
52     public String validate() {
53         String validationMessage = "";
54         validationMessage += validatePort();
55         validationMessage += validateTimeToLive();
56
57         return validationMessage;
58     }
59
60     /**
61      * Gets the base uri.
62      *
63      * @return the base uri
64      */
65     public URI getBaseUri() {
66         return URI.create(DEFAULT_SERVER_URI_ROOT + restPort + DEFAULT_REST_PATH);
67     }
68
69     /**
70      * Gets the rest packages.
71      *
72      * @return the rest packages
73      */
74     public String[] getRestPackages() {
75         return DEFAULT_PACKAGES;
76     }
77
78     /**
79      * Gets the static path.
80      *
81      * @return the static path
82      */
83     public String getStaticPath() {
84         return DEFAULT_STATIC_PATH;
85     }
86
87     /**
88      * Validate port.
89      *
90      * @return the string
91      */
92     private String validatePort() {
93         if (restPort < 1024 || restPort > 65535) {
94             return "port must be greater than 1023 and less than 65536\n";
95         } else {
96             return "";
97         }
98     }
99
100     /**
101      * Validate time to live.
102      *
103      * @return the string
104      */
105     private String validateTimeToLive() {
106         if (timeToLive < -1) {
107             return "time to live must be greater than -1 (set to -1 to wait forever)\n";
108         } else {
109             return "";
110         }
111     }
112
113     /**
114      * Checks if is help set.
115      *
116      * @return true, if is help set
117      */
118     public boolean isHelpSet() {
119         return helpSet;
120     }
121
122     /**
123      * Sets the help.
124      *
125      * @param helpSet the new help
126      */
127     public void setHelp(final boolean helpSet) {
128         this.helpSet = helpSet;
129     }
130
131     /**
132      * Gets the rest port.
133      *
134      * @return the rest port
135      */
136     public int getRestPort() {
137         return restPort;
138     }
139
140     /**
141      * Sets the rest port.
142      *
143      * @param restPort the new rest port
144      */
145     public void setRestPort(final int restPort) {
146         this.restPort = restPort;
147     }
148
149     /**
150      * Gets the time to live.
151      *
152      * @return the time to live
153      */
154     public long getTimeToLive() {
155         return timeToLive;
156     }
157
158     /**
159      * Sets the time to live.
160      *
161      * @param timeToLive the new time to live
162      */
163     public void setTimeToLive(final long timeToLive) {
164         this.timeToLive = timeToLive;
165     }
166
167     /* (non-Javadoc)
168      * @see java.lang.Object#toString()
169      */
170     @Override
171     public String toString() {
172         final StringBuilder ret = new StringBuilder();
173         ret.append(this.getClass().getSimpleName()).append(": URI=").append(this.getBaseUri()).append(", TTL=")
174                 .append(this.getTimeToLive()).append("sec");
175         return ret.toString();
176     }
177 }