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 XLogger LOGGER = XLoggerFactory.getXLogger(ParameterCheck.class);
37 private static final String HOSTNAME_PAR = "hostname";
38 private static final String PORT_PAR = "port";
39 private static final String AXARTIFACTKEY_PAR = "AxArtifactKey";
41 // Recurring string constants
42 private static final String PARAMETER = "parameter \"";
43 private static final String NOT_FOUND = "\" not found";
45 private static final int MAX_PORT = 65535;
48 * private constructor to prevent subclassing of this utility class.
50 private ParameterCheck() {
54 * The Enum StartStop is used to hold .
56 * @author Liam Fallon (liam.fallon@ericsson.com)
58 public enum StartStop {
59 /** Start of an Apex engine has been ordered. */
61 /** Stop of an Apex engine has been ordered. */
68 * @param parameterMap the parameter map
69 * @return the host name
71 public static String getHostName(final Map<String, String[]> parameterMap) {
72 if (parameterMap == null) {
76 if (!parameterMap.containsKey(HOSTNAME_PAR)) {
77 LOGGER.warn(PARAMETER + HOSTNAME_PAR + NOT_FOUND);
81 final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR);
83 if (hostNameValue == null) {
87 if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) {
88 LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + NOT_FOUND);
92 return hostNameValue[0];
98 * @param parameterMap the parameter map
101 public static int getPort(final Map<String, String[]> parameterMap) {
102 if (parameterMap == null) {
106 if (!parameterMap.containsKey(PORT_PAR)) {
107 LOGGER.warn(PARAMETER + PORT_PAR + NOT_FOUND);
111 final String[] portValue = parameterMap.get(PORT_PAR);
113 if (portValue == null) {
117 if (portValue.length == 0 || portValue[0].trim().length() == 0) {
118 LOGGER.warn("value of parameter \"" + PORT_PAR + NOT_FOUND);
124 port = Integer.parseInt(portValue[0]);
125 } catch (final Exception e) {
126 LOGGER.warn("value \"{}\"of parameter \"" + PORT_PAR + "\" not a valid integer", portValue[0], e);
130 if (port <= 0 || port > MAX_PORT) {
131 LOGGER.warn("value \"{}\"of parameter \"" + PORT_PAR + "\" not a valid port between 0 and 65535",
140 * Gets the engine key.
142 * @param parameterMap the parameter map
143 * @return the engine key
145 public static AxArtifactKey getEngineKey(final Map<String, String[]> parameterMap) {
146 if (parameterMap == null) {
150 String artifactKeyParameter = null;
151 for (final String parameter : parameterMap.keySet()) {
152 // Check for an AxArtifactKey parameter
153 if (parameter.startsWith(AXARTIFACTKEY_PAR)) {
154 artifactKeyParameter = parameter;
158 if (artifactKeyParameter == null) {
159 LOGGER.warn(PARAMETER + AXARTIFACTKEY_PAR + NOT_FOUND);
163 final String[] axArtifactKeyArray = artifactKeyParameter.split("#");
165 if (axArtifactKeyArray.length != 2) {
166 LOGGER.warn("value \"{}\" of parameter \"" + AXARTIFACTKEY_PAR + "\" not valid", artifactKeyParameter);
171 return new AxArtifactKey(axArtifactKeyArray[1]);
173 catch (Exception apEx) {
174 LOGGER.trace("invalid artifact key ID {}", axArtifactKeyArray[1], apEx);
180 * Gets the start stop.
182 * @param parameterMap the parameter map
183 * @param engineKey the engine key
184 * @return the start stop
186 public static ParameterCheck.StartStop getStartStop(final Map<String, String[]> parameterMap,
187 final AxArtifactKey engineKey) {
188 if (parameterMap == null || engineKey == null) {
192 final String startStopPar = AXARTIFACTKEY_PAR + '#' + engineKey.getId();
193 if (!parameterMap.containsKey(startStopPar)) {
194 LOGGER.warn("parameter \"{}\" not found", startStopPar);
198 final String[] startStopValue = parameterMap.get(startStopPar);
200 if (startStopValue == null) {
204 if (startStopValue.length == 0 || startStopValue[0].trim().length() == 0) {
205 LOGGER.warn("value of parameter \"{}\" not found", startStopPar);
209 ParameterCheck.StartStop startStop;
210 if ("start".equalsIgnoreCase(startStopValue[0])) {
211 startStop = ParameterCheck.StartStop.START;
212 } else if ("stop".equalsIgnoreCase(startStopValue[0])) {
213 startStop = ParameterCheck.StartStop.STOP;
215 LOGGER.warn("value \"{}\"of parameter \"{}\" not \"start\" or \"stop\"", startStopValue[0], startStopPar);
223 * Find and return a long value with the given name.
225 * @param parameterMap The parameter map containing the value
226 * @param longName The name of the long parameter
227 * @return The long value
229 public static long getLong(final Map<String, String[]> parameterMap, final String longName) {
230 if (parameterMap == null || longName == null) {
234 if (!parameterMap.containsKey(longName)) {
235 LOGGER.warn("parameter \"{}\" not found", longName);
239 final String[] longValue = parameterMap.get(longName);
241 if (longValue == null) {
245 if (longValue.length == 0 || longValue[0].trim().length() == 0) {
246 LOGGER.warn("value of parameter \"{}\" not found", longName);
251 return Long.parseLong(longValue[0]);
252 } catch (final Exception e) {
253 LOGGER.warn("value \"{}\"of parameter \"{}\" not a valid long", longValue[0], longName, e);