50117e52b950dc8f08aa7fbf00588039d5f6f1d0
[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 int MAX_PORT = 65535;
36
37     /**
38      * private constructor to prevent subclassing of this utility class.
39      */
40     private ParameterCheck() {}
41
42     /**
43      * The Enum StartStop is used to hold.
44      *
45      * @author Liam Fallon (liam.fallon@ericsson.com)
46      */
47     public enum StartStop {
48         /** Start of an Apex engine has been ordered. */
49         START,
50         /** Stop of an Apex engine has been ordered. */
51         STOP
52     }
53
54     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ParameterCheck.class);
55
56     private static final String HOSTNAME_PAR = "hostname";
57     private static final String PORT_PAR = "port";
58     private static final String AXARTIFACTKEY_PAR = "AxArtifactKey";
59
60     /**
61      * Gets the host name.
62      *
63      * @param parameterMap the parameter map
64      * @return the host name
65      */
66     public static String getHostName(final Map<String, String[]> parameterMap) {
67         if (!parameterMap.containsKey(HOSTNAME_PAR)) {
68             LOGGER.warn("parameter \"" + HOSTNAME_PAR + "\" not found");
69             return null;
70         }
71
72         final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR);
73
74         if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) {
75             LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + "\" not found");
76             return null;
77         }
78
79         return hostNameValue[0];
80     }
81
82     /**
83      * Gets the port.
84      *
85      * @param parameterMap the parameter map
86      * @return the port
87      */
88     public static int getPort(final Map<String, String[]> parameterMap) {
89         if (!parameterMap.containsKey(PORT_PAR)) {
90             LOGGER.warn("parameter \"" + PORT_PAR + "\" not found");
91             return -1;
92         }
93
94         final String[] portValue = parameterMap.get(PORT_PAR);
95
96         if (portValue.length == 0 || portValue[0].trim().length() == 0) {
97             LOGGER.warn("value of parameter \"" + PORT_PAR + "\" not found");
98             return -1;
99         }
100
101         int port = -1;
102         try {
103             port = Integer.parseInt(portValue[0]);
104         } catch (final Exception e) {
105             LOGGER.warn("value \"" + portValue[0] + "\"of parameter \"" + PORT_PAR + "\" not a valid integer", e);
106             return -1;
107         }
108
109         if (port <= 0 || port > MAX_PORT) {
110             LOGGER.warn("value \"" + portValue[0] + "\"of parameter \"" + PORT_PAR
111                     + "\" not a valid port between 0 and 65535");
112             return -1;
113         }
114
115         return port;
116     }
117
118     /**
119      * Gets the engine key.
120      *
121      * @param parameterMap the parameter map
122      * @return the engine key
123      */
124     public static AxArtifactKey getEngineKey(final Map<String, String[]> parameterMap) {
125         String artifactKeyParameter = null;
126         for (final String parameter : parameterMap.keySet()) {
127             // Check for an AxArtifactKey parameter
128             if (parameter.startsWith(AXARTIFACTKEY_PAR)) {
129                 artifactKeyParameter = parameter;
130                 break;
131             }
132         }
133         if (artifactKeyParameter == null) {
134             LOGGER.warn("parameter \"" + AXARTIFACTKEY_PAR + "\" not found");
135             return null;
136         }
137
138         final String[] axArtifactKeyArray = artifactKeyParameter.split("#");
139
140         if (axArtifactKeyArray.length != 2) {
141             LOGGER.warn("value \"" + artifactKeyParameter + "\" of parameter \"" + AXARTIFACTKEY_PAR + "\" not valid");
142             return null;
143         }
144
145         return new AxArtifactKey(axArtifactKeyArray[1]);
146     }
147
148     /**
149      * Gets the start stop.
150      *
151      * @param parameterMap the parameter map
152      * @param engineKey the engine key
153      * @return the start stop
154      */
155     public static ParameterCheck.StartStop getStartStop(final Map<String, String[]> parameterMap,
156             final AxArtifactKey engineKey) {
157         final String startStopPar = AXARTIFACTKEY_PAR + '#' + engineKey.getID();
158         if (!parameterMap.containsKey(startStopPar)) {
159             LOGGER.warn("parameter \"" + startStopPar + "\" not found");
160             return null;
161         }
162
163         final String[] startStopValue = parameterMap.get(startStopPar);
164
165         if (startStopValue.length == 0 || startStopValue[0].trim().length() == 0) {
166             LOGGER.warn("value of parameter \"" + startStopPar + "\" not found");
167             return null;
168         }
169
170         ParameterCheck.StartStop startStop;
171         if (startStopValue[0].equalsIgnoreCase("start")) {
172             startStop = ParameterCheck.StartStop.START;
173         } else if (startStopValue[0].equalsIgnoreCase("stop")) {
174             startStop = ParameterCheck.StartStop.STOP;
175         } else {
176             LOGGER.warn("value \"" + startStopValue[0] + "\"of parameter \"" + startStopPar
177                     + "\" not \"start\" or \"stop\"");
178             return null;
179         }
180
181         return startStop;
182     }
183
184     /**
185      * Find and return a long value with the given name.
186      *
187      * @param parameterMap The parameter map containing the value
188      * @param longName The name of the long parameter
189      * @return The long value
190      */
191     public static long getLong(final Map<String, String[]> parameterMap, final String longName) {
192         if (!parameterMap.containsKey(longName)) {
193             LOGGER.warn("parameter \"" + longName + "\" not found");
194             return -1;
195         }
196
197         final String[] longValue = parameterMap.get(longName);
198
199         if (longValue.length == 0 || longValue[0].trim().length() == 0) {
200             LOGGER.warn("value of parameter \"" + longName + "\" not found");
201             return -1;
202         }
203
204         try {
205             return Long.parseLong(longValue[0]);
206         } catch (final Exception e) {
207             LOGGER.warn("value \"" + longValue[0] + "\"of parameter \"" + longName + "\" not a valid long", e);
208             return -1;
209         }
210     }
211 }