2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.onap.appc.provider.lcm.service;
25 import org.apache.commons.lang.StringUtils;
26 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
27 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.PreEvacuateInput;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.PreEvacuateOutput;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.PreEvacuateOutputBuilder;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Payload;
31 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
32 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
33 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
34 import org.onap.appc.executor.objects.LCMCommandStatus;
35 import org.onap.appc.util.JsonUtil;
37 import java.io.IOException;
40 * Provide LCM command service for PreEvacuate VNF
42 public class PreEvacuate extends AbstractBaseService {
47 public PreEvacuate() {
48 super(Action.PreEvacuate);
49 logger.debug("PreEvacuate starts");
53 * Constants for characterizing payload handling
55 static final byte PAYLOAD_ACCEPT_NULL = 1;
56 static final byte PAYLOAD_AUTO_TRIM = 2;
57 static final byte PAYLOAD_TREAT_EMPTY_AS_NULL = 4;
60 * Payload handling configuration for all object instances
62 static final byte payloadConfig = PAYLOAD_ACCEPT_NULL | PAYLOAD_AUTO_TRIM | PAYLOAD_TREAT_EMPTY_AS_NULL;
65 * Process the PreEvacuate request
66 * @param input of PreEvacuateInput from the REST API input
67 * @return PreEvacuateOutputBuilder which has the process results
69 public PreEvacuateOutputBuilder process(PreEvacuateInput input) {
70 CommonHeader commonHeader = input.getCommonHeader();
71 ActionIdentifiers actionIdentifiers = input.getActionIdentifiers();
72 Payload payload = input.getPayload();
74 validate(commonHeader, input.getAction(), actionIdentifiers, payload);
76 if (payload != null) {
77 String payloadStr = payload.getValue();
78 if (StringUtils.isEmpty(payloadStr)) {
79 if ((payloadConfig & PAYLOAD_TREAT_EMPTY_AS_NULL) != 0) {
82 } else if ((payloadConfig & PAYLOAD_AUTO_TRIM) != 0) {
83 payloadStr = payloadStr.trim();
84 if (StringUtils.isEmpty(payloadStr) && (payloadConfig & PAYLOAD_TREAT_EMPTY_AS_NULL) != 0) {
87 payload = new Payload(payloadStr);
91 proceedAction(commonHeader, actionIdentifiers, payload);
94 PreEvacuateOutputBuilder outputBuilder = new PreEvacuateOutputBuilder();
95 outputBuilder.setStatus(status);
96 outputBuilder.setCommonHeader(commonHeader);
101 * Validate the input.
102 * Set Status if any error occurs.
104 * @param input of PreEvacuateInput from the REST API input
106 void validate(CommonHeader commonHeader,
108 ActionIdentifiers actionIdentifiers,
110 status = validateVnfId(commonHeader, action, actionIdentifiers);
111 if (status != null) {
116 String keyName = "payload";
117 if (payload == null) {
118 if ((payloadConfig & PAYLOAD_ACCEPT_NULL) == 0) {
119 status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
123 String payloadString = payload.getValue();
124 if (payloadString != null && (payloadConfig & PAYLOAD_AUTO_TRIM) != 0) {
125 payloadString = payloadString.trim();
127 if ((payloadConfig & PAYLOAD_TREAT_EMPTY_AS_NULL) == 0) {
128 status = validateMustHaveParamValue(payloadString, "payload");
129 if (status != null) {
132 } else if (StringUtils.isEmpty(payloadString)) {
133 if ((payloadConfig & PAYLOAD_ACCEPT_NULL) == 0) {
134 status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
140 Map<String, String> payloadMap = JsonUtil.convertJsonStringToFlatMap(payloadString);
141 // validateMustHaveParamValue(payloadMap.get(keyName), keyName);
142 } catch (IOException e) {
143 logger.error(String.format("PreEvacuate (%s) got IOException when converting payload", rpcName), e);
144 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, e.getMessage());
149 * Proceed to action for the PreEvacuate VNF traffic.
151 * @param input of PreEvacuateInput from the REST API input
153 void proceedAction(CommonHeader commonHeader,
154 ActionIdentifiers actionIdentifiers,
156 RequestHandlerInput requestHandlerInput =
157 getRequestHandlerInput(commonHeader, actionIdentifiers, payload, this.getClass().getName());
158 if (requestHandlerInput != null) {
159 executeAction(requestHandlerInput);