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 // Recurring string constants
36 private static final String PARAMETER = "parameter \"";
37 private static final String NOT_FOUND = "\" not found";
39 private static final int MAX_PORT = 65535;
42 * private constructor to prevent subclassing of this utility class.
44 private ParameterCheck() {
48 * The Enum StartStop is used to hold .
50 * @author Liam Fallon (liam.fallon@ericsson.com)
52 public enum StartStop {
53 /** Start of an Apex engine has been ordered. */
55 /** Stop of an Apex engine has been ordered. */
59 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ParameterCheck.class);
61 private static final String HOSTNAME_PAR = "hostname";
62 private static final String PORT_PAR = "port";
63 private static final String AXARTIFACTKEY_PAR = "AxArtifactKey";
68 * @param parameterMap the parameter map
69 * @return the host name
71 public static String getHostName(final Map<String, String[]> parameterMap) {
72 if (!parameterMap.containsKey(HOSTNAME_PAR)) {
73 LOGGER.warn(PARAMETER + HOSTNAME_PAR + NOT_FOUND);
77 final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR);
79 if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) {
80 LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + NOT_FOUND);
84 return hostNameValue[0];
90 * @param parameterMap the parameter map
93 public static int getPort(final Map<String, String[]> parameterMap) {
94 if (!parameterMap.containsKey(PORT_PAR)) {
95 LOGGER.warn(PARAMETER + PORT_PAR + NOT_FOUND);
99 final String[] portValue = parameterMap.get(PORT_PAR);
101 if (portValue.length == 0 || portValue[0].trim().length() == 0) {
102 LOGGER.warn("value of parameter \"" + PORT_PAR + NOT_FOUND);
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);
114 if (port <= 0 || port > MAX_PORT) {
115 LOGGER.warn("value \"{}\"of parameter \"" + PORT_PAR + "\" not a valid port between 0 and 65535",
124 * Gets the engine key.
126 * @param parameterMap the parameter map
127 * @return the engine key
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;
138 if (artifactKeyParameter == null) {
139 LOGGER.warn(PARAMETER + AXARTIFACTKEY_PAR + NOT_FOUND);
143 final String[] axArtifactKeyArray = artifactKeyParameter.split("#");
145 if (axArtifactKeyArray.length != 2) {
146 LOGGER.warn("value \"{}\" of parameter \"" + AXARTIFACTKEY_PAR + "\" not valid", artifactKeyParameter);
150 return new AxArtifactKey(axArtifactKeyArray[1]);
154 * Gets the start stop.
156 * @param parameterMap the parameter map
157 * @param engineKey the engine key
158 * @return the start stop
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);
168 final String[] startStopValue = parameterMap.get(startStopPar);
170 if (startStopValue.length == 0 || startStopValue[0].trim().length() == 0) {
171 LOGGER.warn("value of parameter \"{}\" not found", startStopPar);
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;
181 LOGGER.warn("value \"{}\"of parameter \"{}\" not \"start\" or \"stop\"", startStopValue[0], startStopPar);
189 * Find and return a long value with the given name.
191 * @param parameterMap The parameter map containing the value
192 * @param longName The name of the long parameter
193 * @return The long value
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);
201 final String[] longValue = parameterMap.get(longName);
203 if (longValue.length == 0 || longValue[0].trim().length() == 0) {
204 LOGGER.warn("value of parameter \"{}\" not found", longName);
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);