cad0911f113759f75756181fea2afc41ffcd7e4c
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.client.deployment.rest;
23
24 import java.util.Map;
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 OF_PARAMETER = "\"of parameter \"";
43     private static final String VALUE = "value \"";
44     private static final String PARAMETER = "parameter \"";
45     private static final String NOT_FOUND = "\" not found";
46
47     private static final int MAX_PORT = 65535;
48
49     /**
50      * private constructor to prevent subclassing of this utility class.
51      */
52     private ParameterCheck() {
53     }
54
55     /**
56      * The Enum StartStop is used to hold.
57      *
58      * @author Liam Fallon (liam.fallon@ericsson.com)
59      */
60     public enum StartStop {
61         /** Start of an Apex engine has been ordered. */
62         START,
63         /** Stop of an Apex engine has been ordered. */
64         STOP
65     }
66
67     /**
68      * Gets the host name.
69      *
70      * @param parameterMap the parameter map
71      * @return the host name
72      */
73     public static String getHostName(final Map<String, String[]> parameterMap) {
74         if (parameterMap == null) {
75             return null;
76         }
77
78         if (!parameterMap.containsKey(HOSTNAME_PAR)) {
79             LOGGER.warn(PARAMETER + HOSTNAME_PAR + NOT_FOUND);
80             return null;
81         }
82
83         final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR);
84         if (hostNameValue == null) {
85             return null;
86         }
87
88         if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) {
89             LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + NOT_FOUND);
90             return null;
91         }
92
93         return hostNameValue[0];
94     }
95
96     /**
97      * Gets the port.
98      *
99      * @param parameterMap the parameter map
100      * @return the port
101      */
102     public static int getPort(final Map<String, String[]> parameterMap) {
103         if (parameterMap == null) {
104             return -1;
105         }
106
107         if (!parameterMap.containsKey(PORT_PAR)) {
108             LOGGER.warn(PARAMETER + PORT_PAR + NOT_FOUND);
109             return -1;
110         }
111
112         final String[] portValue = parameterMap.get(PORT_PAR);
113
114         if (portValue.length == 0 || portValue[0].trim().length() == 0) {
115             LOGGER.warn("value of parameter \"" + PORT_PAR + NOT_FOUND);
116             return -1;
117         }
118
119         int port = -1;
120         try {
121             port = Integer.parseInt(portValue[0]);
122         } catch (final Exception e) {
123             LOGGER.warn(VALUE + portValue[0] + OF_PARAMETER + PORT_PAR + "\" not a valid integer", e);
124             return -1;
125         }
126
127         if (port <= 0 || port > MAX_PORT) {
128             String message = VALUE + portValue[0] + OF_PARAMETER + PORT_PAR
129                             + "\" not a valid port between 0 and 65535";
130             LOGGER.warn(message);
131             return -1;
132         }
133
134         return port;
135     }
136
137     /**
138      * Gets the engine key.
139      *
140      * @param parameterMap the parameter map
141      * @return the engine key
142      */
143     public static AxArtifactKey getEngineKey(final Map<String, String[]> parameterMap) {
144         if (parameterMap == null) {
145             return null;
146         }
147
148         String artifactKeyParameter = null;
149         for (final String parameter : parameterMap.keySet()) {
150             // Check for an AxArtifactKey parameter
151             if (parameter.startsWith(AXARTIFACTKEY_PAR)) {
152                 artifactKeyParameter = parameter;
153                 break;
154             }
155         }
156         if (artifactKeyParameter == null) {
157             LOGGER.warn(PARAMETER + AXARTIFACTKEY_PAR + NOT_FOUND);
158             return null;
159         }
160
161         final String[] axArtifactKeyArray = artifactKeyParameter.split("#");
162
163         if (axArtifactKeyArray.length != 2) {
164             String message = VALUE + artifactKeyParameter + "\" of parameter \"" + AXARTIFACTKEY_PAR
165                             + "\" not valid";
166             LOGGER.warn(message);
167             return null;
168         }
169
170         try {
171             return new AxArtifactKey(axArtifactKeyArray[1]);
172         } catch (Exception apEx) {
173             LOGGER.trace("invalid artifact key ID {}", axArtifactKeyArray[1], apEx);
174             return null;
175         }
176     }
177
178     /**
179      * Gets the start stop.
180      *
181      * @param parameterMap the parameter map
182      * @param engineKey the engine key
183      * @return the start stop
184      */
185     public static ParameterCheck.StartStop getStartStop(final Map<String, String[]> parameterMap,
186                     final AxArtifactKey engineKey) {
187         if (parameterMap == null || engineKey == null) {
188             return null;
189         }
190
191         final String startStopPar = AXARTIFACTKEY_PAR + '#' + engineKey.getId();
192         if (!parameterMap.containsKey(startStopPar)) {
193             LOGGER.warn("parameter \"{}\" not found", startStopPar);
194             return null;
195         }
196
197         final String[] startStopValue = parameterMap.get(startStopPar);
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 + longValue[0] + OF_PARAMETER + longName + "\" not a valid long", e);
252             return -1;
253         }
254     }
255 }