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