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.deployment.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 OF_PARAMETER = "\"of parameter \"";
43 private static final String VALUE = "value \"";
44 private static final String PARAMETER = "parameter \"";
45 private static final String NOT_FOUND = "\" not found";
47 private static final int MAX_PORT = 65535;
50 * private constructor to prevent subclassing of this utility class.
52 private ParameterCheck() {
56 * The Enum StartStop is used to hold.
58 * @author Liam Fallon (liam.fallon@ericsson.com)
60 public enum StartStop {
61 /** Start of an Apex engine has been ordered. */
63 /** Stop of an Apex engine has been ordered. */
70 * @param parameterMap the parameter map
71 * @return the host name
73 public static String getHostName(final Map<String, String[]> parameterMap) {
74 if (!parameterMap.containsKey(HOSTNAME_PAR)) {
75 LOGGER.warn(PARAMETER + HOSTNAME_PAR + NOT_FOUND);
79 final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR);
81 if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) {
82 LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + NOT_FOUND);
86 return hostNameValue[0];
92 * @param parameterMap the parameter map
95 public static int getPort(final Map<String, String[]> parameterMap) {
96 if (!parameterMap.containsKey(PORT_PAR)) {
97 LOGGER.warn(PARAMETER + PORT_PAR + NOT_FOUND);
101 final String[] portValue = parameterMap.get(PORT_PAR);
103 if (portValue.length == 0 || portValue[0].trim().length() == 0) {
104 LOGGER.warn("value of parameter \"" + PORT_PAR + NOT_FOUND);
110 port = Integer.parseInt(portValue[0]);
111 } catch (final Exception e) {
112 LOGGER.warn(VALUE + portValue[0] + OF_PARAMETER + PORT_PAR + "\" not a valid integer", e);
116 if (port <= 0 || port > MAX_PORT) {
117 String message = VALUE + portValue[0] + OF_PARAMETER + PORT_PAR
118 + "\" not a valid port between 0 and 65535";
119 LOGGER.warn(message);
127 * Gets the engine key.
129 * @param parameterMap the parameter map
130 * @return the engine key
132 public static AxArtifactKey getEngineKey(final Map<String, String[]> parameterMap) {
133 String artifactKeyParameter = null;
134 for (final String parameter : parameterMap.keySet()) {
135 // Check for an AxArtifactKey parameter
136 if (parameter.startsWith(AXARTIFACTKEY_PAR)) {
137 artifactKeyParameter = parameter;
141 if (artifactKeyParameter == null) {
142 LOGGER.warn(PARAMETER + AXARTIFACTKEY_PAR + NOT_FOUND);
146 final String[] axArtifactKeyArray = artifactKeyParameter.split("#");
148 if (axArtifactKeyArray.length != 2) {
149 String message = VALUE + artifactKeyParameter + "\" of parameter \"" + AXARTIFACTKEY_PAR
151 LOGGER.warn(message);
155 return new AxArtifactKey(axArtifactKeyArray[1]);
159 * Gets the start stop.
161 * @param parameterMap the parameter map
162 * @param engineKey the engine key
163 * @return the start stop
165 public static ParameterCheck.StartStop getStartStop(final Map<String, String[]> parameterMap,
166 final AxArtifactKey engineKey) {
167 final String startStopPar = AXARTIFACTKEY_PAR + '#' + engineKey.getId();
168 if (!parameterMap.containsKey(startStopPar)) {
169 LOGGER.warn("parameter \"{}\" not found", startStopPar);
173 final String[] startStopValue = parameterMap.get(startStopPar);
175 if (startStopValue.length == 0 || startStopValue[0].trim().length() == 0) {
176 LOGGER.warn("value of parameter \"{}\" not found", startStopPar);
180 ParameterCheck.StartStop startStop;
181 if ("start".equalsIgnoreCase(startStopValue[0])) {
182 startStop = ParameterCheck.StartStop.START;
183 } else if ("stop".equalsIgnoreCase(startStopValue[0])) {
184 startStop = ParameterCheck.StartStop.STOP;
186 LOGGER.warn("value \"{}\"of parameter \"{}\" not \"start\" or \"stop\"", startStopValue[0], startStopPar);
194 * Find and return a long value with the given name.
196 * @param parameterMap The parameter map containing the value
197 * @param longName The name of the long parameter
198 * @return The long value
200 public static long getLong(final Map<String, String[]> parameterMap, final String longName) {
201 if (!parameterMap.containsKey(longName)) {
202 LOGGER.warn("parameter \"{}\" not found", longName);
206 final String[] longValue = parameterMap.get(longName);
208 if (longValue.length == 0 || longValue[0].trim().length() == 0) {
209 LOGGER.warn("value of parameter \"{}\" not found", longName);
214 return Long.parseLong(longValue[0]);
215 } catch (final Exception e) {
216 LOGGER.warn(VALUE + longValue[0] + OF_PARAMETER + longName + "\" not a valid long", e);