Add new LCM actions GetConfig, StartTraffic, StopTraffic, etc
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / service / StopTraffic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.appc.provider.lcm.service;
24
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.StopTrafficInput;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.StopTrafficOutput;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.StopTrafficOutputBuilder;
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;
36
37 import java.io.IOException;
38 import java.util.Map;
39 /**
40  * Provide LCM command service for StopTraffic VNF
41  */
42 public class StopTraffic extends AbstractBaseService {
43
44     /**
45      * Constructor
46      */
47     public StopTraffic() {
48         super(Action.StopTraffic);
49         logger.debug("StopTraffic starts");
50     }
51
52     /**
53      * Constants for characterizing payload handling
54      */
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;
58
59     /**
60      * Payload handling configuration for all object instances
61      */
62     static final byte payloadConfig = PAYLOAD_ACCEPT_NULL | PAYLOAD_AUTO_TRIM | PAYLOAD_TREAT_EMPTY_AS_NULL;
63
64     /**
65      * Process the StopTraffic request
66      * @param input of StopTrafficInput from the REST API input
67      * @return StopTrafficOutputBuilder which has the process results
68      */
69     public StopTrafficOutputBuilder process(StopTrafficInput input) {
70         CommonHeader commonHeader = input.getCommonHeader();
71         ActionIdentifiers actionIdentifiers = input.getActionIdentifiers();
72         Payload payload = input.getPayload();
73
74         validate(commonHeader, input.getAction(), actionIdentifiers, payload);
75         if (status == null) {
76             if (payload != null) {
77                 String payloadStr = payload.getValue();
78                 if (StringUtils.isEmpty(payloadStr)) {
79                     if ((payloadConfig & PAYLOAD_TREAT_EMPTY_AS_NULL) != 0) {
80                         payload = null;
81                     }
82                 } else if ((payloadConfig & PAYLOAD_AUTO_TRIM) != 0) {
83                     payloadStr = payloadStr.trim();
84                     if (StringUtils.isEmpty(payloadStr) && (payloadConfig & PAYLOAD_TREAT_EMPTY_AS_NULL) != 0) {
85                         payload = null;
86                     } else {
87                         payload = new Payload(payloadStr);
88                     }                    
89                 }
90             }
91             proceedAction(commonHeader, actionIdentifiers, payload);
92         }
93
94         StopTrafficOutputBuilder outputBuilder = new StopTrafficOutputBuilder();
95         outputBuilder.setStatus(status);
96         outputBuilder.setCommonHeader(commonHeader);
97         return outputBuilder;
98     }
99
100     /**
101      * Validate the input.
102      * Set Status if any error occurs.
103      *
104      * @param input of StopTrafficInput from the REST API input
105      */
106     void validate(CommonHeader commonHeader,
107             Action action,
108             ActionIdentifiers actionIdentifiers,
109             Payload payload) {
110         status = validateVnfId(commonHeader, action, actionIdentifiers);
111         if (status != null) {
112             return;
113         }
114
115         // validate payload
116         String keyName = "payload";
117         if (payload == null) {
118             if ((payloadConfig & PAYLOAD_ACCEPT_NULL) == 0) {
119                 status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
120             }
121             return;
122         }
123         String payloadString = payload.getValue();
124         if (payloadString != null && (payloadConfig & PAYLOAD_AUTO_TRIM) != 0) {
125             payloadString = payloadString.trim();
126         }
127         if ((payloadConfig & PAYLOAD_TREAT_EMPTY_AS_NULL) == 0) {
128             status = validateMustHaveParamValue(payloadString, "payload");
129             if (status != null) {
130                 return;
131             }
132         } else if (StringUtils.isEmpty(payloadString)) {
133             if ((payloadConfig & PAYLOAD_ACCEPT_NULL) == 0) {
134                 status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
135             }
136             return;
137         }
138
139         try {
140             Map<String, String> payloadMap = JsonUtil.convertJsonStringToFlatMap(payloadString);
141             // validateMustHaveParamValue(payloadMap.get(keyName), keyName);
142         } catch (IOException e) {
143             logger.error(String.format("StopTraffic (%s) got IOException when converting payload", rpcName), e);
144             status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, e.getMessage());
145         }
146     }
147
148     /**
149      * Proceed to action for the StopTraffic VNF traffic.
150      *
151      * @param input of StopTrafficInput from the REST API input
152      */
153     void proceedAction(CommonHeader commonHeader,
154             ActionIdentifiers actionIdentifiers,
155             Payload payload) {
156         RequestHandlerInput requestHandlerInput =
157                 getRequestHandlerInput(commonHeader, actionIdentifiers, payload, this.getClass().getName());
158         if (requestHandlerInput != null) {
159             executeAction(requestHandlerInput);
160         }
161     }
162 }