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