Update license header in appc-provider files
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / service / ResumeTrafficService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.provider.lcm.service;
25
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.ResumeTrafficInput;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.ResumeTrafficOutput;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.ResumeTrafficOutputBuilder;
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.Payload;
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 import java.io.IOException;
37 import java.util.Map;
38
39 /**
40  * Provide LCM command service for Resume VNF traffic
41  */
42 public class ResumeTrafficService extends AbstractBaseService {
43
44     /**
45      * Constructor
46      */
47     public ResumeTrafficService() {
48         super(Action.ResumeTraffic);
49         logger.debug("ResumeTrafficService starts");
50     }
51
52     /**
53      * Process the Resume request
54      *
55      * @param input of ResumeTrafficInput from the REST API input
56      * @return ResumeTrafficOutputBuilder which has the process results
57      */
58     public ResumeTrafficOutputBuilder process(ResumeTrafficInput input) {
59         CommonHeader commonHeader = input.getCommonHeader();
60         ActionIdentifiers actionIdentifiers = input.getActionIdentifiers();
61         Payload payload = input.getPayload();
62
63         validate(commonHeader, input.getAction(), actionIdentifiers, payload);
64         if (status == null) {
65             proceedAction(commonHeader, actionIdentifiers, payload);
66         }
67
68         ResumeTrafficOutputBuilder outputBuilder = new ResumeTrafficOutputBuilder();
69         outputBuilder.setStatus(status);
70         outputBuilder.setCommonHeader(input.getCommonHeader());
71         return outputBuilder;
72     }
73
74     /**
75      * Validate the input.
76      * Set Status if any error occurs.
77      *
78      * @param input of ResumeTrafficInput from the REST API input
79      */
80     void validate(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers, Payload payload) {
81         status = validateVnfId(commonHeader, action, actionIdentifiers);
82         if (status != null) {
83             return;
84         }
85         // validate payload
86         String keyName = "payload";
87         if (payload == null) {
88             status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
89             return;
90         }
91         String payloadString = payload.getValue();
92         status = validateMustHaveParamValue(payloadString == null ? payloadString : payloadString.trim(), "payload");
93         if (status != null) {
94             return;
95         }
96         try {
97             Map<String, String> payloadMap = JsonUtil.convertJsonStringToFlatMap(payloadString);
98             validateMustHaveParamValue(payloadMap.get(keyName), keyName);
99         } catch (IOException e) {
100             logger.error(String.format("ResumeTrafficService (%s) got IOException when converting payload", rpcName),
101                     e);
102             status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, e.getMessage());
103         }
104     }
105
106     /**
107      * Proceed to action for the Resume VNF traffic.
108      *
109      * @param input of ResumeTrafficInput from the REST API input
110      */
111     void proceedAction(CommonHeader commonHeader, ActionIdentifiers actionIdentifiers, Payload payload) {
112         RequestHandlerInput requestHandlerInput =
113                 getRequestHandlerInput(commonHeader, actionIdentifiers, payload, this.getClass().getName());
114         if (requestHandlerInput != null) {
115             executeAction(requestHandlerInput);
116         }
117     }
118 }