Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / service / ActionStatusService.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.apache.commons.lang.StringUtils;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.ActionStatusInput;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.ActionStatusOutputBuilder;
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.onap.appc.domainmodel.lcm.ActionLevel;
34 import org.onap.appc.executor.objects.LCMCommandStatus;
35 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
36 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
37
38 /**
39  * Provide LCM command service for Query action status of a previously issue LCM command
40  */
41 public class ActionStatusService extends AbstractBaseService {
42
43     /**
44      * Constructor
45      */
46     public ActionStatusService() {
47         super(Action.ActionStatus);
48         logger.debug("ActionStatusService starts");
49     }
50
51     /**
52      * Query action status
53      * @param input of the ActionStatusInput which contains the information about the previous LCM command
54      * @return ActionStatusOuputBuilder containing query results
55      */
56     public ActionStatusOutputBuilder queryStatus(ActionStatusInput input) {
57         Payload outputPayload = null;
58
59         validate(input);
60         ActionIdentifiers actionIdentifiers = input.getActionIdentifiers();
61         if (null == status) {
62             RequestHandlerInput requestHandlerInput = getRequestHandlerInput(
63                 input.getCommonHeader(), actionIdentifiers, input.getPayload(), this.getClass().getName());
64             if (requestHandlerInput != null) {
65                 updateToMgmtActionLevel(requestHandlerInput);
66
67                 RequestHandlerOutput reqHandlerOutput = executeAction(requestHandlerInput);
68
69                 outputPayload = new RequestExecutor().getPayload(reqHandlerOutput);
70             }
71         }
72
73         logger.info(String.format("ActionStatus execute of '%s' finished with status %s. Reason: %s",
74             actionIdentifiers, status == null ? "null" : status.getCode().toString(),
75             status == null ? "null" : status.getMessage()));
76
77         ActionStatusOutputBuilder outputBuilder = new ActionStatusOutputBuilder();
78         outputBuilder.setPayload(outputPayload);
79         outputBuilder.setCommonHeader(input.getCommonHeader());
80         outputBuilder.setStatus(status);
81         return outputBuilder;
82     }
83
84     /**
85      * Validate input for
86      *   - commonHeader
87      *   - Action in the input
88      *   - ActionIdentifier and only has VNF ID
89      *   - and payload exists and is not empty string
90      * @param input of the ActionStatusInput from the REST API
91      */
92     void validate(ActionStatusInput input) {
93         status = validateVnfId(input.getCommonHeader(), input.getAction(), input.getActionIdentifiers());
94         if (status != null) {
95             return;
96         }
97
98         Payload  payload = input.getPayload();
99         if (payload == null) {
100             status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, "payload");
101         } else if (StringUtils.isEmpty(payload.getValue())) {
102             status = buildStatusForParamName(LCMCommandStatus.INVALID_INPUT_PARAMETER, "payload");
103         }
104     }
105
106     /**
107      * Update request to MGMT action level
108      * @param request of the RequestHandlerInput
109      */
110     void updateToMgmtActionLevel(RequestHandlerInput request) {
111         request.getRequestContext().setActionLevel(ActionLevel.MGMT);
112     }
113
114 }