4e75763dbc79e388177b96d0aae1cee09d7aee1c
[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.deployment.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 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.containsKey(HOSTNAME_PAR)) {
75             LOGGER.warn(PARAMETER + HOSTNAME_PAR + NOT_FOUND);
76             return null;
77         }
78
79         final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR);
80
81         if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) {
82             LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + NOT_FOUND);
83             return null;
84         }
85
86         return hostNameValue[0];
87     }
88
89     /**
90      * Gets the port.
91      *
92      * @param parameterMap the parameter map
93      * @return the port
94      */
95     public static int getPort(final Map<String, String[]> parameterMap) {
96         if (!parameterMap.containsKey(PORT_PAR)) {
97             LOGGER.warn(PARAMETER + PORT_PAR + NOT_FOUND);
98             return -1;
99         }
100
101         final String[] portValue = parameterMap.get(PORT_PAR);
102
103         if (portValue.length == 0 || portValue[0].trim().length() == 0) {
104             LOGGER.warn("value of parameter \"" + PORT_PAR + NOT_FOUND);
105             return -1;
106         }
107
108         int port = -1;
109         try {
110             port = Integer.parseInt(portValue[0]);
111         } catch (final Exception e) {
112             LOGGER.warn(VALUE + portValue[0] + OF_PARAMETER + PORT_PAR + "\" not a valid integer", e);
113             return -1;
114         }
115
116         if (port <= 0 || port > MAX_PORT) {
117             String message = VALUE + portValue[0] + OF_PARAMETER + PORT_PAR
118                             + "\" not a valid port between 0 and 65535";
119             LOGGER.warn(message);
120             return -1;
121         }
122
123         return port;
124     }
125
126     /**
127      * Gets the engine key.
128      *
129      * @param parameterMap the parameter map
130      * @return the engine key
131      */
132     public static AxArtifactKey getEngineKey(final Map<String, String[]> parameterMap) {
133         String artifactKeyParameter = null;
134         for (final String parameter : parameterMap.keySet()) {
135             // Check for an AxArtifactKey parameter
136             if (parameter.startsWith(AXARTIFACTKEY_PAR)) {
137                 artifactKeyParameter = parameter;
138                 break;
139             }
140         }
141         if (artifactKeyParameter == null) {
142             LOGGER.warn(PARAMETER + AXARTIFACTKEY_PAR + NOT_FOUND);
143             return null;
144         }
145
146         final String[] axArtifactKeyArray = artifactKeyParameter.split("#");
147
148         if (axArtifactKeyArray.length != 2) {
149             String message = VALUE + artifactKeyParameter + "\" of parameter \"" + AXARTIFACTKEY_PAR
150                             + "\" not valid";
151             LOGGER.warn(message);
152             return null;
153         }
154
155         return new AxArtifactKey(axArtifactKeyArray[1]);
156     }
157
158     /**
159      * Gets the start stop.
160      *
161      * @param parameterMap the parameter map
162      * @param engineKey the engine key
163      * @return the start stop
164      */
165     public static ParameterCheck.StartStop getStartStop(final Map<String, String[]> parameterMap,
166                     final AxArtifactKey engineKey) {
167         final String startStopPar = AXARTIFACTKEY_PAR + '#' + engineKey.getId();
168         if (!parameterMap.containsKey(startStopPar)) {
169             LOGGER.warn("parameter \"{}\" not found", startStopPar);
170             return null;
171         }
172
173         final String[] startStopValue = parameterMap.get(startStopPar);
174
175         if (startStopValue.length == 0 || startStopValue[0].trim().length() == 0) {
176             LOGGER.warn("value of parameter \"{}\" not found", startStopPar);
177             return null;
178         }
179
180         ParameterCheck.StartStop startStop;
181         if ("start".equalsIgnoreCase(startStopValue[0])) {
182             startStop = ParameterCheck.StartStop.START;
183         } else if ("stop".equalsIgnoreCase(startStopValue[0])) {
184             startStop = ParameterCheck.StartStop.STOP;
185         } else {
186             LOGGER.warn("value \"{}\"of parameter \"{}\" not \"start\" or \"stop\"", startStopValue[0], startStopPar);
187             return null;
188         }
189
190         return startStop;
191     }
192
193     /**
194      * Find and return a long value with the given name.
195      *
196      * @param parameterMap The parameter map containing the value
197      * @param longName The name of the long parameter
198      * @return The long value
199      */
200     public static long getLong(final Map<String, String[]> parameterMap, final String longName) {
201         if (!parameterMap.containsKey(longName)) {
202             LOGGER.warn("parameter \"{}\" not found", longName);
203             return -1;
204         }
205
206         final String[] longValue = parameterMap.get(longName);
207
208         if (longValue.length == 0 || longValue[0].trim().length() == 0) {
209             LOGGER.warn("value of parameter \"{}\" not found", longName);
210             return -1;
211         }
212
213         try {
214             return Long.parseLong(longValue[0]);
215         } catch (final Exception e) {
216             LOGGER.warn(VALUE + longValue[0] + OF_PARAMETER + longName + "\" not a valid long", e);
217             return -1;
218         }
219     }
220 }