e4148a47058b69eec5ff5308bd4ddefdd28fa8b5
[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.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 ApexMonitoringRestParameters {
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.monitoring.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      * @return the result of the validation
50      */
51     public String validate() {
52         String validationMessage = "";
53         validationMessage += validatePort();
54         validationMessage += validateTimeToLive();
55
56         return validationMessage;
57     }
58
59     public URI getBaseUri() {
60         return URI.create(DEFAULT_SERVER_URI_ROOT + restPort + DEFAULT_REST_PATH);
61     }
62
63     public String[] getRestPackages() {
64         return DEFAULT_PACKAGES;
65     }
66
67     public String getStaticPath() {
68         return DEFAULT_STATIC_PATH;
69     }
70
71     private String validatePort() {
72         if (restPort < 1024 || restPort > 65535) {
73             return "port must be greater than 1023 and less than 65536\n";
74         } else {
75             return "";
76         }
77     }
78
79     private String validateTimeToLive() {
80         if (timeToLive < -1) {
81             return "time to live must be greater than -1 (set to -1 to wait forever)\n";
82         } else {
83             return "";
84         }
85     }
86
87     public boolean isHelpSet() {
88         return helpSet;
89     }
90
91     public void setHelp(final boolean helpSet) {
92         this.helpSet = helpSet;
93     }
94
95     public int getRestPort() {
96         return restPort;
97     }
98
99     public void setRestPort(final int restPort) {
100         this.restPort = restPort;
101     }
102
103     public long getTimeToLive() {
104         return timeToLive;
105     }
106
107     public void setTimeToLive(final long timeToLive) {
108         this.timeToLive = timeToLive;
109     }
110
111     @Override
112     public String toString() {
113         final StringBuilder ret = new StringBuilder();
114         ret.append(this.getClass().getSimpleName()).append(": URI=").append(this.getBaseUri()).append(", TTL=")
115                 .append(this.getTimeToLive()).append("sec");
116         return ret.toString();
117     }
118 }