Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / service / QuiesceTrafficService.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.QuiesceTrafficInput;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.QuiesceTrafficOutput;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.QuiesceTrafficOutputBuilder;
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.action.identifiers.ActionIdentifiers;
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
38 import java.io.IOException;
39 import java.util.Map;
40 /**
41  * Provide LCM command service for Quiesce VNF traffic
42  */
43 public class QuiesceTrafficService extends AbstractBaseService {
44
45     /**
46      * Constructor
47      */
48     public QuiesceTrafficService() {
49         super(Action.QuiesceTraffic);
50         logger.debug("QuiesceTrafficService starts");
51     }
52
53     /**
54      * Process the quiesce request
55      * @param input of QuiesceTrafficInput from the REST API input
56      * @return QuiesceTrafficOutputBuilder which has the process results
57      */
58     public QuiesceTrafficOutputBuilder process(QuiesceTrafficInput 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         QuiesceTrafficOutputBuilder outputBuilder = new QuiesceTrafficOutputBuilder();
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 QuiesceTrafficInput from the REST API input
79      */
80     void validate(CommonHeader commonHeader,
81             Action action,
82             ActionIdentifiers actionIdentifiers,
83                         Payload payload) {
84                 status = validateVnfId(commonHeader, action, actionIdentifiers);
85                 if (status != null) {
86                         return;
87                 }
88
89                 // validate payload
90                 String keyName = "payload";
91                 if (payload == null) {
92                         status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
93                         return;
94                 }
95                 String payloadString = payload.getValue();
96                 status = validateMustHaveParamValue(payloadString == null ? payloadString : payloadString.trim(), "payload");
97                 if (status != null) {
98                         return;
99                 }
100
101                 try {
102                         Map<String, String> payloadMap = JsonUtil.convertJsonStringToFlatMap(payloadString);
103                         validateMustHaveParamValue(payloadMap.get(keyName), keyName);
104                 } catch (IOException e) {
105                         logger.error(String.format("QuiesceTrafficService (%s) got IOException when converting payload", rpcName), e);
106                         status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, e.getMessage());
107                 }
108         }
109
110     /**
111      * Proceed to action for the quiesce VNF traffic.
112      *
113      * @param input of QuiesceTrafficInput from the REST API input
114      */
115     void proceedAction(CommonHeader commonHeader,
116             ActionIdentifiers actionIdentifiers,
117                         Payload payload) {
118                 RequestHandlerInput requestHandlerInput = getRequestHandlerInput(commonHeader, actionIdentifiers, payload,
119                                 this.getClass().getName());
120                 if (requestHandlerInput != null) {
121                         executeAction(requestHandlerInput);
122                 }
123         }
124 }