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