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;
25 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
26 import org.slf4j.ext.XLogger;
27 import org.slf4j.ext.XLoggerFactory;
30 * The Class ParameterCheck is used to check parameters passed to the servlet.
32 * @author Liam Fallon (liam.fallon@ericsson.com)
34 public final class ParameterCheck {
35 private static final int MAX_PORT = 65535;
38 * private constructor to prevent subclassing of this utility class.
40 private ParameterCheck() {}
43 * The Enum StartStop is used to hold .
45 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public enum StartStop {
48 /** Start of an Apex engine has been ordered. */
50 /** Stop of an Apex engine has been ordered. */
54 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ParameterCheck.class);
56 private static final String HOSTNAME_PAR = "hostname";
57 private static final String PORT_PAR = "port";
58 private static final String AXARTIFACTKEY_PAR = "AxArtifactKey";
63 * @param parameterMap the parameter map
64 * @return the host name
66 public static String getHostName(final Map<String, String[]> parameterMap) {
67 if (!parameterMap.containsKey(HOSTNAME_PAR)) {
68 LOGGER.warn("parameter \"" + HOSTNAME_PAR + "\" not found");
72 final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR);
74 if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) {
75 LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + "\" not found");
79 return hostNameValue[0];
85 * @param parameterMap the parameter map
88 public static int getPort(final Map<String, String[]> parameterMap) {
89 if (!parameterMap.containsKey(PORT_PAR)) {
90 LOGGER.warn("parameter \"" + PORT_PAR + "\" not found");
94 final String[] portValue = parameterMap.get(PORT_PAR);
96 if (portValue.length == 0 || portValue[0].trim().length() == 0) {
97 LOGGER.warn("value of parameter \"" + PORT_PAR + "\" not found");
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);
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");
119 * Gets the engine key.
121 * @param parameterMap the parameter map
122 * @return the engine key
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;
133 if (artifactKeyParameter == null) {
134 LOGGER.warn("parameter \"" + AXARTIFACTKEY_PAR + "\" not found");
138 final String[] axArtifactKeyArray = artifactKeyParameter.split("#");
140 if (axArtifactKeyArray.length != 2) {
141 LOGGER.warn("value \"" + artifactKeyParameter + "\" of parameter \"" + AXARTIFACTKEY_PAR + "\" not valid");
145 return new AxArtifactKey(axArtifactKeyArray[1]);
149 * Gets the start stop.
151 * @param parameterMap the parameter map
152 * @param engineKey the engine key
153 * @return the start stop
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");
163 final String[] startStopValue = parameterMap.get(startStopPar);
165 if (startStopValue.length == 0 || startStopValue[0].trim().length() == 0) {
166 LOGGER.warn("value of parameter \"" + startStopPar + "\" not found");
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;
176 LOGGER.warn("value \"" + startStopValue[0] + "\"of parameter \"" + startStopPar
177 + "\" not \"start\" or \"stop\"");
185 * Find and return a long value with the given name.
187 * @param parameterMap The parameter map containing the value
188 * @param longName The name of the long parameter
189 * @return The long value
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");
197 final String[] longValue = parameterMap.get(longName);
199 if (longValue.length == 0 || longValue[0].trim().length() == 0) {
200 LOGGER.warn("value of parameter \"" + longName + "\" not found");
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);