summaryrefslogtreecommitdiffstats
path: root/appc-provider/appc-provider-bundle/src/main/java/org/onap/appc/provider/lcm/service/ActionStatusService.java
blob: ede2b28377183bcef0abcf90a4fff538649224ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.provider.lcm.service;

import org.apache.commons.lang.StringUtils;
import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.ActionStatusInput;
import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.ActionStatusOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Payload;
import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
import org.onap.appc.domainmodel.lcm.ActionLevel;
import org.onap.appc.executor.objects.LCMCommandStatus;
import org.onap.appc.requesthandler.objects.RequestHandlerInput;
import org.onap.appc.requesthandler.objects.RequestHandlerOutput;

/**
 * Provide LCM command service for Query action status of a previously issue LCM command
 */
public class ActionStatusService extends AbstractBaseService {

    /**
     * Constructor
     */
    public ActionStatusService() {
        super(Action.ActionStatus);
        logger.debug("ActionStatusService starts");
    }

    /**
     * Query action status
     * @param input of the ActionStatusInput which contains the information about the previous LCM command
     * @return ActionStatusOuputBuilder containing query results
     */
    public ActionStatusOutputBuilder queryStatus(ActionStatusInput input) {
        Payload outputPayload = null;

        validate(input);
        ActionIdentifiers actionIdentifiers = input.getActionIdentifiers();
        if (null == status) {
            RequestHandlerInput requestHandlerInput = getRequestHandlerInput(
                input.getCommonHeader(), actionIdentifiers, input.getPayload(), this.getClass().getName());
            if (requestHandlerInput != null) {
                updateToMgmtActionLevel(requestHandlerInput);

                RequestHandlerOutput reqHandlerOutput = executeAction(requestHandlerInput);

                outputPayload = new RequestExecutor().getPayload(reqHandlerOutput);
            }
        }

        logger.info(String.format("ActionStatus execute of '%s' finished with status %s. Reason: %s",
            actionIdentifiers, status == null ? "null" : status.getCode().toString(),
            status == null ? "null" : status.getMessage()));

        ActionStatusOutputBuilder outputBuilder = new ActionStatusOutputBuilder();
        outputBuilder.setPayload(outputPayload);
        outputBuilder.setCommonHeader(input.getCommonHeader());
        outputBuilder.setStatus(status);
        return outputBuilder;
    }

    /**
     * Validate input for
     *   - commonHeader
     *   - Action in the input
     *   - ActionIdentifier and only has VNF ID
     *   - and payload exists and is not empty string
     * @param input of the ActionStatusInput from the REST API
     */
    void validate(ActionStatusInput input) {
        status = validateVnfId(input.getCommonHeader(), input.getAction(), input.getActionIdentifiers());
        if (status != null) {
            return;
        }

        Payload  payload = input.getPayload();
        if (payload == null) {
            status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, "payload");
        } else if (StringUtils.isEmpty(payload.getValue())) {
            status = buildStatusForParamName(LCMCommandStatus.INVALID_INPUT_PARAMETER, "payload");
        }
    }

    /**
     * Update request to MGMT action level
     * @param request of the RequestHandlerInput
     */
    void updateToMgmtActionLevel(RequestHandlerInput request) {
        request.getRequestContext().setActionLevel(ActionLevel.MGMT);
    }

}