2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 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 == null) {
78 if (!parameterMap.containsKey(HOSTNAME_PAR)) {
79 LOGGER.warn(PARAMETER + HOSTNAME_PAR + NOT_FOUND);
83 final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR);
84 if (hostNameValue == null) {
88 if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) {
89 LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + NOT_FOUND);
93 return hostNameValue[0];
99 * @param parameterMap the parameter map
102 public static int getPort(final Map<String, String[]> parameterMap) {
103 if (parameterMap == null) {
107 if (!parameterMap.containsKey(PORT_PAR)) {
108 LOGGER.warn(PARAMETER + PORT_PAR + NOT_FOUND);
112 final String[] portValue = parameterMap.get(PORT_PAR);
114 if (portValue.length == 0 || portValue[0].trim().length() == 0) {
115 LOGGER.warn("value of parameter \"" + PORT_PAR + NOT_FOUND);
121 port = Integer.parseInt(portValue[0]);
122 } catch (final Exception e) {
123 LOGGER.warn(VALUE + portValue[0] + OF_PARAMETER + PORT_PAR + "\" not a valid integer", e);
127 if (port <= 0 || port > MAX_PORT) {
128 String message = VALUE + portValue[0] + OF_PARAMETER + PORT_PAR
129 + "\" not a valid port between 0 and 65535";
130 LOGGER.warn(message);
138 * Gets the engine key.
140 * @param parameterMap the parameter map
141 * @return the engine key
143 public static AxArtifactKey getEngineKey(final Map<String, String[]> parameterMap) {
144 if (parameterMap == null) {
148 String artifactKeyParameter = null;
149 for (final String parameter : parameterMap.keySet()) {
150 // Check for an AxArtifactKey parameter
151 if (parameter.startsWith(AXARTIFACTKEY_PAR)) {
152 artifactKeyParameter = parameter;
156 if (artifactKeyParameter == null) {
157 LOGGER.warn(PARAMETER + AXARTIFACTKEY_PAR + NOT_FOUND);
161 final String[] axArtifactKeyArray = artifactKeyParameter.split("#");
163 if (axArtifactKeyArray.length != 2) {
164 String message = VALUE + artifactKeyParameter + "\" of parameter \"" + AXARTIFACTKEY_PAR
166 LOGGER.warn(message);
171 return new AxArtifactKey(axArtifactKeyArray[1]);
172 } catch (Exception apEx) {
173 LOGGER.trace("invalid artifact key ID {}", axArtifactKeyArray[1], apEx);
179 * Gets the start stop.
181 * @param parameterMap the parameter map
182 * @param engineKey the engine key
183 * @return the start stop
185 public static ParameterCheck.StartStop getStartStop(final Map<String, String[]> parameterMap,
186 final AxArtifactKey engineKey) {
187 if (parameterMap == null || engineKey == null) {
191 final String startStopPar = AXARTIFACTKEY_PAR + '#' + engineKey.getId();
192 if (!parameterMap.containsKey(startStopPar)) {
193 LOGGER.warn("parameter \"{}\" not found", startStopPar);
197 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 + longValue[0] + OF_PARAMETER + longName + "\" not a valid long", e);