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