Add new LCM actions GetConfig, StartTraffic, StopTraffic, etc
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / service / StatusTraffic.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.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
26 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.StatusTrafficInput;
27 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.StatusTrafficOutput;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.StatusTrafficOutputBuilder;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Payload;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
31 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
32 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
33 import org.onap.appc.executor.objects.LCMCommandStatus;
34 import org.onap.appc.util.JsonUtil;
35
36 import java.io.IOException;
37 import java.util.Map;
38 /**
39  * Provide LCM command service for StatusTraffic VNF
40  */
41 public class StatusTraffic extends AbstractBaseService {
42
43         /**
44          * Constructor
45          */
46         public StatusTraffic() {
47                 super(Action.StatusTraffic);
48                 logger.debug("StatusTraffic starts");
49         }
50
51         /**
52          * Process the StatusTraffic request
53          * @param input of StatusTrafficInput from the REST API input
54          * @return StatusTrafficOutputBuilder which has the process results
55          */
56         public StatusTrafficOutputBuilder process(StatusTrafficInput input) {
57                 CommonHeader commonHeader = input.getCommonHeader();
58                 ActionIdentifiers actionIdentifiers = input.getActionIdentifiers();
59                 Payload payload = input.getPayload();
60
61                 validate(commonHeader, input.getAction(), actionIdentifiers, payload);
62                 if (status == null) {
63                         proceedAction(commonHeader,actionIdentifiers,payload);
64                 }
65
66                 StatusTrafficOutputBuilder outputBuilder = new StatusTrafficOutputBuilder();
67                 outputBuilder.setStatus(status);
68                 outputBuilder.setCommonHeader(input.getCommonHeader());
69                 return outputBuilder;
70         }
71
72         /**
73          * Validate the input.
74          * Set Status if any error occurs.
75          *
76          * @param input of StatusTrafficInput from the REST API input
77          */
78         void validate(CommonHeader commonHeader,
79                         Action action,
80                         ActionIdentifiers actionIdentifiers,
81                         Payload payload) {
82                 status = validateVnfId(commonHeader, action, actionIdentifiers);
83                 if (status != null) {
84                         return;
85                 }
86
87                 // validate payload
88                 String keyName = "payload";
89                 if (payload == null) {
90                         status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
91                         return;
92                 }
93                 String payloadString = payload.getValue();
94                 status = validateMustHaveParamValue(payloadString == null ? payloadString : payloadString.trim(), "payload");
95                 if (status != null) {
96                         return;
97                 }
98
99                 try {
100                         Map<String, String> payloadMap = JsonUtil.convertJsonStringToFlatMap(payloadString);
101                         validateMustHaveParamValue(payloadMap.get(keyName), keyName);
102                 } catch (IOException e) {
103                         logger.error(String.format("StatusTraffic (%s) got IOException when converting payload", rpcName), e);
104                         status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, e.getMessage());
105                 }
106         }
107
108         /**
109          * Proceed to action for the StatusTraffic VNF traffic.
110          *
111          * @param input of StatusTrafficInput from the REST API input
112          */
113         void proceedAction(CommonHeader commonHeader,
114                         ActionIdentifiers actionIdentifiers,
115                         Payload payload) {
116                 RequestHandlerInput requestHandlerInput = getRequestHandlerInput(commonHeader, actionIdentifiers, payload,
117                                 this.getClass().getName());
118                 if (requestHandlerInput != null) {
119                         executeAction(requestHandlerInput);
120                 }
121         }
122 }