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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.monitoring.rest;
24 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
25 import org.slf4j.ext.XLogger;
26 import org.slf4j.ext.XLoggerFactory;
29 * The Class ParameterCheck is used to check parameters passed to the servlet.
31 * @author Liam Fallon (liam.fallon@ericsson.com)
33 public final class ParameterCheck {
34 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ParameterCheck.class);
36 private static final String HOSTNAME_PAR = "hostname";
37 private static final String PORT_PAR = "port";
38 private static final String AXARTIFACTKEY_PAR = "AxArtifactKey";
40 // Recurring string constants
41 private static final String PARAMETER = "parameter \"";
42 private static final String NOT_FOUND = "\" not found";
44 private static final int MAX_PORT = 65535;
47 * private constructor to prevent subclassing of this utility class.
49 private ParameterCheck() {
53 * The Enum StartStop is used to hold .
55 * @author Liam Fallon (liam.fallon@ericsson.com)
57 public enum StartStop {
58 /** Start of an Apex engine has been ordered. */
60 /** Stop of an Apex engine has been ordered. */
67 * @param parameterMap the parameter map
68 * @return the host name
70 public static String getHostName(final Map<String, String[]> parameterMap) {
71 if (parameterMap == null) {
75 if (!parameterMap.containsKey(HOSTNAME_PAR)) {
76 LOGGER.warn(PARAMETER + HOSTNAME_PAR + NOT_FOUND);
80 final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR);
82 if (hostNameValue == null) {
86 if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) {
87 LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + NOT_FOUND);
91 return hostNameValue[0];
97 * @param parameterMap the parameter map
100 public static int getPort(final Map<String, String[]> parameterMap) {
101 if (parameterMap == null) {
105 if (!parameterMap.containsKey(PORT_PAR)) {
106 LOGGER.warn(PARAMETER + PORT_PAR + NOT_FOUND);
110 final String[] portValue = parameterMap.get(PORT_PAR);
112 if (portValue == null) {
116 if (portValue.length == 0 || portValue[0].trim().length() == 0) {
117 LOGGER.warn("value of parameter \"" + PORT_PAR + NOT_FOUND);
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);
129 if (port <= 0 || port > MAX_PORT) {
130 LOGGER.warn("value \"{}\"of parameter \"" + PORT_PAR + "\" not a valid port between 0 and 65535",
139 * Gets the engine key.
141 * @param parameterMap the parameter map
142 * @return the engine key
144 public static AxArtifactKey getEngineKey(final Map<String, String[]> parameterMap) {
145 if (parameterMap == null) {
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;
157 if (artifactKeyParameter == null) {
158 LOGGER.warn(PARAMETER + AXARTIFACTKEY_PAR + NOT_FOUND);
162 final String[] axArtifactKeyArray = artifactKeyParameter.split("#");
164 if (axArtifactKeyArray.length != 2) {
165 LOGGER.warn("value \"{}\" of parameter \"" + AXARTIFACTKEY_PAR + "\" not valid", artifactKeyParameter);
170 return new AxArtifactKey(axArtifactKeyArray[1]);
171 } catch (Exception apEx) {
172 LOGGER.trace("invalid artifact key ID {}", axArtifactKeyArray[1], apEx);
178 * Gets the start stop.
180 * @param parameterMap the parameter map
181 * @param engineKey the engine key
182 * @return the start stop
184 public static ParameterCheck.StartStop getStartStop(final Map<String, String[]> parameterMap,
185 final AxArtifactKey engineKey) {
186 if (parameterMap == null || engineKey == null) {
190 final String startStopPar = AXARTIFACTKEY_PAR + '#' + engineKey.getId();
191 if (!parameterMap.containsKey(startStopPar)) {
192 LOGGER.warn("parameter \"{}\" not found", startStopPar);
196 final String[] startStopValue = parameterMap.get(startStopPar);
198 if (startStopValue == null) {
202 if (startStopValue.length == 0 || startStopValue[0].trim().length() == 0) {
203 LOGGER.warn("value of parameter \"{}\" not found", startStopPar);
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;
213 LOGGER.warn("value \"{}\"of parameter \"{}\" not \"start\" or \"stop\"", startStopValue[0], startStopPar);
221 * Find and return a long value with the given name.
223 * @param parameterMap The parameter map containing the value
224 * @param longName The name of the long parameter
225 * @return The long value
227 public static long getLong(final Map<String, String[]> parameterMap, final String longName) {
228 if (parameterMap == null || longName == null) {
232 if (!parameterMap.containsKey(longName)) {
233 LOGGER.warn("parameter \"{}\" not found", longName);
237 final String[] longValue = parameterMap.get(longName);
239 if (longValue == null) {
243 if (longValue.length == 0 || longValue[0].trim().length() == 0) {
244 LOGGER.warn("value of parameter \"{}\" not found", longName);
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);