34bacb9743091b515549bcf7ed4ce5bb00aeb14a
[policy/apex-pdp.git] / client / client-monitoring / src / main / java / org / onap / policy / apex / client / monitoring / rest / ParameterCheck.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.monitoring.rest;
22
23 import java.util.Map;
24
25 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
26 import org.slf4j.ext.XLogger;
27 import org.slf4j.ext.XLoggerFactory;
28
29 /**
30  * The Class ParameterCheck is used to check parameters passed to the servlet.
31  *
32  * @author Liam Fallon (liam.fallon@ericsson.com)
33  */
34 public final class ParameterCheck {
35     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ParameterCheck.class);
36
37     private static final String HOSTNAME_PAR = "hostname";
38     private static final String PORT_PAR = "port";
39     private static final String AXARTIFACTKEY_PAR = "AxArtifactKey";
40
41     // Recurring string constants
42     private static final String PARAMETER = "parameter \"";
43     private static final String NOT_FOUND = "\" not found";
44
45     private static final int MAX_PORT = 65535;
46
47     /**
48      * private constructor to prevent subclassing of this utility class.
49      */
50     private ParameterCheck() {
51     }
52
53     /**
54      * The Enum StartStop is used to hold .
55      *
56      * @author Liam Fallon (liam.fallon@ericsson.com)
57      */
58     public enum StartStop {
59         /** Start of an Apex engine has been ordered. */
60         START,
61         /** Stop of an Apex engine has been ordered. */
62         STOP
63     }
64
65     /**
66      * Gets the host name.
67      *
68      * @param parameterMap the parameter map
69      * @return the host name
70      */
71     public static String getHostName(final Map<String, String[]> parameterMap) {
72         if (parameterMap == null) {
73             return null;
74         }
75         
76         if (!parameterMap.containsKey(HOSTNAME_PAR)) {
77             LOGGER.warn(PARAMETER + HOSTNAME_PAR + NOT_FOUND);
78             return null;
79         }
80
81         final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR);
82
83         if (hostNameValue == null) {
84             return null;
85         }
86
87         if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) {
88             LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + NOT_FOUND);
89             return null;
90         }
91
92         return hostNameValue[0];
93     }
94
95     /**
96      * Gets the port.
97      *
98      * @param parameterMap the parameter map
99      * @return the port
100      */
101     public static int getPort(final Map<String, String[]> parameterMap) {
102         if (parameterMap == null) {
103             return -1;
104         }
105         
106         if (!parameterMap.containsKey(PORT_PAR)) {
107             LOGGER.warn(PARAMETER + PORT_PAR + NOT_FOUND);
108             return -1;
109         }
110
111         final String[] portValue = parameterMap.get(PORT_PAR);
112         
113         if (portValue == null) {
114             return -1;
115         }
116         
117         if (portValue.length == 0 || portValue[0].trim().length() == 0) {
118             LOGGER.warn("value of parameter \"" + PORT_PAR + NOT_FOUND);
119             return -1;
120         }
121
122         int port = -1;
123         try {
124             port = Integer.parseInt(portValue[0]);
125         } catch (final Exception e) {
126             LOGGER.warn("value \"{}\"of parameter \"" + PORT_PAR + "\" not a valid integer", portValue[0], e);
127             return -1;
128         }
129
130         if (port <= 0 || port > MAX_PORT) {
131             LOGGER.warn("value \"{}\"of parameter \"" + PORT_PAR + "\" not a valid port between 0 and 65535",
132                             portValue[0]);
133             return -1;
134         }
135
136         return port;
137     }
138
139     /**
140      * Gets the engine key.
141      *
142      * @param parameterMap the parameter map
143      * @return the engine key
144      */
145     public static AxArtifactKey getEngineKey(final Map<String, String[]> parameterMap) {
146         if (parameterMap == null) {
147             return null;
148         }
149         
150         String artifactKeyParameter = null;
151         for (final String parameter : parameterMap.keySet()) {
152             // Check for an AxArtifactKey parameter
153             if (parameter.startsWith(AXARTIFACTKEY_PAR)) {
154                 artifactKeyParameter = parameter;
155                 break;
156             }
157         }
158         if (artifactKeyParameter == null) {
159             LOGGER.warn(PARAMETER + AXARTIFACTKEY_PAR + NOT_FOUND);
160             return null;
161         }
162
163         final String[] axArtifactKeyArray = artifactKeyParameter.split("#");
164
165         if (axArtifactKeyArray.length != 2) {
166             LOGGER.warn("value \"{}\" of parameter \"" + AXARTIFACTKEY_PAR + "\" not valid", artifactKeyParameter);
167             return null;
168         }
169
170         try {
171             return new AxArtifactKey(axArtifactKeyArray[1]);
172         }
173         catch (Exception apEx) {
174             LOGGER.trace("invalid artifact key ID {}", axArtifactKeyArray[1], apEx);
175             return null;
176         }
177     }
178
179     /**
180      * Gets the start stop.
181      *
182      * @param parameterMap the parameter map
183      * @param engineKey the engine key
184      * @return the start stop
185      */
186     public static ParameterCheck.StartStop getStartStop(final Map<String, String[]> parameterMap,
187                     final AxArtifactKey engineKey) {
188         if (parameterMap == null || engineKey == null) {
189             return null;
190         }
191         
192         final String startStopPar = AXARTIFACTKEY_PAR + '#' + engineKey.getId();
193         if (!parameterMap.containsKey(startStopPar)) {
194             LOGGER.warn("parameter \"{}\" not found", startStopPar);
195             return null;
196         }
197
198         final String[] startStopValue = parameterMap.get(startStopPar);
199         
200         if (startStopValue == null) {
201             return null;
202         }
203
204         if (startStopValue.length == 0 || startStopValue[0].trim().length() == 0) {
205             LOGGER.warn("value of parameter \"{}\" not found", startStopPar);
206             return null;
207         }
208
209         ParameterCheck.StartStop startStop;
210         if ("start".equalsIgnoreCase(startStopValue[0])) {
211             startStop = ParameterCheck.StartStop.START;
212         } else if ("stop".equalsIgnoreCase(startStopValue[0])) {
213             startStop = ParameterCheck.StartStop.STOP;
214         } else {
215             LOGGER.warn("value \"{}\"of parameter \"{}\" not \"start\" or \"stop\"", startStopValue[0], startStopPar);
216             return null;
217         }
218
219         return startStop;
220     }
221
222     /**
223      * Find and return a long value with the given name.
224      *
225      * @param parameterMap The parameter map containing the value
226      * @param longName The name of the long parameter
227      * @return The long value
228      */
229     public static long getLong(final Map<String, String[]> parameterMap, final String longName) {
230         if (parameterMap == null || longName == null) {
231             return -1;
232         }
233         
234         if (!parameterMap.containsKey(longName)) {
235             LOGGER.warn("parameter \"{}\" not found", longName);
236             return -1;
237         }
238
239         final String[] longValue = parameterMap.get(longName);
240
241         if (longValue == null) {
242             return -1;
243         }
244         
245         if (longValue.length == 0 || longValue[0].trim().length() == 0) {
246             LOGGER.warn("value of parameter \"{}\" not found", longName);
247             return -1;
248         }
249
250         try {
251             return Long.parseLong(longValue[0]);
252         } catch (final Exception e) {
253             LOGGER.warn("value \"{}\"of parameter \"{}\" not a valid long", longValue[0], longName, e);
254             return -1;
255         }
256     }
257 }