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