aboutsummaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/process/ActivateVnfOperationalEnvironment.java
blob: 3957f86bab8f3a10bc47dd4fd1f90746d48744c7 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.apihandlerinfra.tenantisolation.process;

import java.util.List;
import java.util.Optional;
import javax.ws.rs.core.Response;
import org.apache.http.HttpStatus;
import org.json.JSONObject;
import org.onap.aai.domain.yang.OperationalEnvironment;
import org.onap.so.apihandler.common.ErrorNumbers;
import org.onap.so.db.request.client.RequestsDbClient;
import org.onap.so.apihandlerinfra.exceptions.ApiException;
import org.onap.so.apihandlerinfra.exceptions.ValidateException;
import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
import org.onap.so.apihandlerinfra.tenantisolation.helpers.AAIClientHelper;
import org.onap.so.apihandlerinfra.tenantisolation.helpers.ActivateVnfDBHelper;
import org.onap.so.apihandlerinfra.tenantisolation.helpers.SDCClientHelper;
import org.onap.so.apihandlerinfra.tenantisolationbeans.ServiceModelList;
import org.onap.so.client.aai.AAIObjectType;
import org.onap.so.client.aai.entities.AAIResultWrapper;
import org.onap.so.client.aai.entities.Relationships;
import org.onap.so.client.aai.entities.uri.AAIResourceUri;
import org.onap.so.db.request.beans.OperationalEnvDistributionStatus;
import org.onap.so.db.request.beans.OperationalEnvServiceModelStatus;
import org.onap.so.logger.ErrorCode;
import org.onap.so.logger.MessageEnum;
import org.onap.so.requestsdb.RequestsDBHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class ActivateVnfOperationalEnvironment {

    private static final Logger logger = LoggerFactory.getLogger(ActivateVnfOperationalEnvironment.class);
    private static final int DEFAULT_ACTIVATE_RETRY_COUNT = 3;
    private static final String DISTRIBUTION_STATUS_SENT = "SENT";
    private static final String OPER_ENVIRONMENT_ID_KEY = "operational-environment-id";

    @Autowired
    private ActivateVnfDBHelper dbHelper;
    @Autowired
    private AAIClientHelper aaiHelper;
    @Autowired
    private RequestsDBHelper requestDb;
    @Autowired
    private SDCClientHelper sdcClientHelper;

    @Value("${mso.tenant.isolation.retry.count}")
    private String sdcRetryCount;

    @Autowired
    RequestsDbClient client;

    /**
     * The Point-Of-Entry from APIH with VID request to send activate request
     * 
     * @param requestId - String
     * @param request - CloudOrchestrationRequest object
     * @return void - nothing
     */
    public void execute(String requestId, CloudOrchestrationRequest request) throws ApiException {
        String vnfOperationalEnvironmentId = request.getOperationalEnvironmentId();

        String vidWorkloadContext = request.getRequestDetails().getRequestParameters().getWorkloadContext();
        List<ServiceModelList> serviceModelVersionIdList =
                request.getRequestDetails().getRequestParameters().getManifest().getServiceModelList();

        String ecompOperationalEnvironmentId = null;
        AAIResultWrapper wrapper = getAAIOperationalEnvironment(vnfOperationalEnvironmentId);
        Optional<Relationships> optRelationships = wrapper.getRelationships();
        if (optRelationships.isPresent()) {
            Relationships relationships = optRelationships.get();
            List<AAIResourceUri> operationalEnvironments =
                    relationships.getRelatedAAIUris(AAIObjectType.OPERATIONAL_ENVIRONMENT);
            if (!operationalEnvironments.isEmpty()) {
                ecompOperationalEnvironmentId =
                        operationalEnvironments.get(0).getURIKeys().get(OPER_ENVIRONMENT_ID_KEY);
            }
        }
        logger.debug("  vnfOperationalEnvironmentId   : {}", vnfOperationalEnvironmentId);
        logger.debug("  ecompOperationalEnvironmentId : {}", ecompOperationalEnvironmentId);

        OperationalEnvironment operationalEnv = wrapper.asBean(OperationalEnvironment.class).get();
        String workloadContext = operationalEnv.getWorkloadContext();
        logger.debug("  aai workloadContext: {}", workloadContext);
        if (!vidWorkloadContext.equals(workloadContext)) {
            ErrorLoggerInfo errorLoggerInfo =
                    new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError)
                            .build();
            throw new ValidateException.Builder(
                    " The vid workloadContext did not match from aai record. " + " vid workloadContext:"
                            + vidWorkloadContext + " aai workloadContext:" + workloadContext,
                    HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
                            .build();
        }
        if (ecompOperationalEnvironmentId == null) {
            ErrorLoggerInfo errorLoggerInfo =
                    new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError)
                            .build();
            throw new ValidateException.Builder(
                    " The ECOMP OE was not in aai record; the value of relationship.relationship-data key: "
                            + OPER_ENVIRONMENT_ID_KEY,
                    HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
                            .build();
        }

        processActivateSDCRequest(requestId, ecompOperationalEnvironmentId, serviceModelVersionIdList, workloadContext,
                vnfOperationalEnvironmentId);

    }


    /**
     * The Method to send the Activation Requests to SDC
     * 
     * @param requestId - String
     * @param operationalEnvironmentId - String
     * @param serviceModelVersionIdList - List<ServiceModelList> list
     * @param workloadContext - String
     * @return jsonResponse - JSONObject object
     */
    public void processActivateSDCRequest(String requestId, String operationalEnvironmentId,
            List<ServiceModelList> serviceModelVersionIdList, String workloadContext,
            String vnfOperationalEnvironmentId) throws ApiException {

        JSONObject jsonResponse = null;
        int retryCount = 0;
        try {
            retryCount = Integer.parseInt(sdcRetryCount);
        } catch (NumberFormatException e) {
            retryCount = DEFAULT_ACTIVATE_RETRY_COUNT;
        }

        // loop through the serviceModelVersionId, and send request SDC
        for (ServiceModelList serviceModelList : serviceModelVersionIdList) {
            String serviceModelVersionId = serviceModelList.getServiceModelVersionId();
            String recoveryAction = serviceModelList.getRecoveryAction().toString().toUpperCase();

            // should insert 1 row
            OperationalEnvServiceModelStatus serviceModelStatus =
                    dbHelper.insertRecordToOperationalEnvServiceModelStatus(requestId, operationalEnvironmentId,
                            serviceModelVersionId, DISTRIBUTION_STATUS_SENT, recoveryAction, retryCount,
                            workloadContext, vnfOperationalEnvironmentId);
            client.save(serviceModelStatus);

            String distributionId = "";

            jsonResponse = sdcClientHelper.postActivateOperationalEnvironment(serviceModelVersionId,
                    operationalEnvironmentId, workloadContext);

            String statusCode = jsonResponse.get("statusCode").toString();
            if (statusCode.equals(String.valueOf(Response.Status.ACCEPTED.getStatusCode()))) {
                distributionId = jsonResponse.get("distributionId").toString();
                // should insert 1 row
                OperationalEnvDistributionStatus distStatus = dbHelper.insertRecordToOperationalEnvDistributionStatus(
                        distributionId, operationalEnvironmentId, serviceModelVersionId, requestId,
                        DISTRIBUTION_STATUS_SENT, "");
                client.save(distStatus);

            } else {
                ErrorLoggerInfo errorLoggerInfo =
                        new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError)
                                .build();
                String dbErrorMessage = " Failure calling SDC: statusCode: " + statusCode + "; messageId: "
                        + jsonResponse.get("messageId") + "; message: " + jsonResponse.get("message");

                requestDb.updateInfraFailureCompletion(dbErrorMessage, requestId, operationalEnvironmentId);
                throw new ValidateException.Builder(dbErrorMessage, HttpStatus.SC_BAD_REQUEST,
                        ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
            }
        }

    }

    /**
     * Get AAIOperationalEnvironment object
     * 
     * @param operationalEnvironmentId - String
     * @return operationalEnv - AAIOperationalEnvironment object
     */
    public AAIResultWrapper getAAIOperationalEnvironment(String operationalEnvironmentId) {
        return aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);
    }

}