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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*-
* ============LICENSE_START=======================================================
* ONAP - SO
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Modifications Copyright (c) 2019 Samsung
* ================================================================================
* 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.Distribution;
import org.onap.so.apihandlerinfra.tenantisolationbeans.DistributionStatus;
import org.onap.so.client.aai.entities.AAIResultWrapper;
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.stereotype.Component;
@Component
public class ActivateVnfStatusOperationalEnvironment {
private static Logger logger = LoggerFactory.getLogger(ActivateVnfStatusOperationalEnvironment.class);
private String origRequestId = "";
private String errorMessage = "";
private OperationalEnvDistributionStatus queryDistributionDbResponse = null;
private OperationalEnvServiceModelStatus queryServiceModelResponse = null;
private boolean isOverallSuccess = false;
private final int RETRY_COUNT_ZERO = 0;
private final String ERROR_REASON_ABORTED = "ABORTED";
private final String RECOVERY_ACTION_RETRY = "RETRY";
private final String RECOVERY_ACTION_ABORT = "ABORT";
private final String RECOVERY_ACTION_SKIP = "SKIP";
private final String DISTRIBUTION_STATUS_OK = DistributionStatus.DISTRIBUTION_COMPLETE_OK.toString();
private final String DISTRIBUTION_STATUS_ERROR = DistributionStatus.DISTRIBUTION_COMPLETE_ERROR.toString();
private final String DISTRIBUTION_STATUS_SENT = "SENT";
private final String MESSAGE_UNDEFINED_ID = "Undefined Error Message!";
@Autowired
private ActivateVnfDBHelper dbHelper;
@Autowired
private RequestsDBHelper requestDb;
@Autowired
private SDCClientHelper sdcClientHelper;
@Autowired
private RequestsDbClient client;
@Autowired
private AAIClientHelper aaiHelper;
/**
* The Point-Of-Entry from APIH with activate status from SDC
*
* @param requestId - String
* @param request - CloudOrchestrationRequest - object
* @return void - nothing
*/
public void execute(String requestId, CloudOrchestrationRequest request) throws ApiException {
try {
String operationalEnvironmentId = "";
String sdcDistributionId = request.getDistributionId();
Distribution sdcStatus = request.getDistribution();
// Distribution, Query for operationalEnvironmentId, serviceModelVersionId, origRequestId
this.queryDistributionDbResponse = client.getDistributionStatusById(sdcDistributionId);
operationalEnvironmentId = this.queryDistributionDbResponse.getOperationalEnvId();
this.origRequestId = this.queryDistributionDbResponse.getRequestId();
// ServiceModel, Query for recoveryAction, retryCountString
this.queryServiceModelResponse =
client.findOneByOperationalEnvIdAndServiceModelVersionIdAndRequestId(operationalEnvironmentId,
queryDistributionDbResponse.getServiceModelVersionId(), this.origRequestId);
processActivateSDCStatus(sdcDistributionId, sdcStatus, this.queryDistributionDbResponse,
this.queryServiceModelResponse);
// After EVERY status processed, need to query the status of all service modelId
// to determine the OVERALL status if "COMPLETE" or "FAILURE":
checkOrUpdateOverallStatus(operationalEnvironmentId, this.origRequestId);
// Update AAI to ACTIVE if Overall success
if (isOverallSuccess) {
String vnfOperationalEnvironmentId = this.queryServiceModelResponse.getVnfOperationalEnvId();
OperationalEnvironment aaiOpEnv = getAAIOperationalEnvironment(vnfOperationalEnvironmentId);
if (aaiOpEnv != null) {
aaiOpEnv.setOperationalEnvironmentStatus("ACTIVE");
aaiHelper.updateAaiOperationalEnvironment(vnfOperationalEnvironmentId, aaiOpEnv);
} else {
requestDb.updateInfraFailureCompletion("Unable to update ACTIVATE status in AAI. ",
this.origRequestId, this.queryServiceModelResponse.getVnfOperationalEnvId());
}
}
} catch (Exception e) {
requestDb.updateInfraFailureCompletion(e.getMessage(), this.origRequestId,
this.queryServiceModelResponse.getVnfOperationalEnvId());
}
}
/**
* The Method to process the Activation Status from SDC
*
* @param sdcDistributionId - string
* @param sdcStatus - Distribution object
* @param queryDistributionDbResponse - OperationalEnvDistributionStatus object
* @param queryServiceModelResponse - OperationalEnvServiceModelStatus object
* @return void - nothing
*/
public void processActivateSDCStatus(String sdcDistributionId, Distribution sdcStatus,
OperationalEnvDistributionStatus queryDistributionDbResponse,
OperationalEnvServiceModelStatus queryServiceModelResponse) throws ApiException {
String sdcStatusValue = sdcStatus.getStatus().toString();
String recoveryAction = queryServiceModelResponse.getRecoveryAction();
int retryCount = queryServiceModelResponse.getRetryCount();
// Validate/process status
if (sdcStatus.getStatus().toString().equals(DISTRIBUTION_STATUS_OK)) {
// should update 1 row, update status to "DISTRIBUTION_COMPLETE_OK"
OperationalEnvDistributionStatus updateDistStatusOk = dbHelper
.updateStatusInOperationalEnvDistributionStatus(queryDistributionDbResponse, sdcStatusValue, "");
client.save(updateDistStatusOk);
// should update 1 row, update status and retryCount = 0 (ie, serviceModelVersionId is DONE!)
OperationalEnvServiceModelStatus updateRetryCountZeroAndStatusOk =
dbHelper.updateRetryCountAndStatusInOperationalEnvServiceModelStatus(queryServiceModelResponse,
sdcStatusValue, RETRY_COUNT_ZERO);
client.save(updateRetryCountZeroAndStatusOk);
} else {
// "DISTRIBUTION_COMPLETE_ERROR", Check if recoveryAction is "RETRY"
if (recoveryAction.equals(RECOVERY_ACTION_RETRY) && retryCount > RETRY_COUNT_ZERO) {
// RESEND / RETRY serviceModelVersionId to SDC
JSONObject jsonResponse =
callSDClientForRetry(queryDistributionDbResponse, queryServiceModelResponse, sdcStatus);
} else { // either RETRY & Count = 0, or 'ABORT', or 'SKIP'
if (recoveryAction.equals(RECOVERY_ACTION_SKIP) || recoveryAction.equals(RECOVERY_ACTION_ABORT)) {
String modifiedStatus = "";
String errorReason = "";
if (recoveryAction.equals(RECOVERY_ACTION_SKIP)) { // considered SUCCESS
modifiedStatus = DISTRIBUTION_STATUS_OK;
} else {
if (recoveryAction.equals(RECOVERY_ACTION_ABORT)) {
modifiedStatus = DISTRIBUTION_STATUS_ERROR; // ABORT, error
errorReason = ERROR_REASON_ABORTED;
}
}
sdcStatusValue = modifiedStatus;
OperationalEnvServiceModelStatus updateRetryCountZeroAndStatus =
dbHelper.updateRetryCountAndStatusInOperationalEnvServiceModelStatus(
queryServiceModelResponse, modifiedStatus, RETRY_COUNT_ZERO);
client.save(updateRetryCountZeroAndStatus);
OperationalEnvDistributionStatus updateDistStatus =
dbHelper.updateStatusInOperationalEnvDistributionStatus(queryDistributionDbResponse,
modifiedStatus, errorReason);
client.save(updateDistStatus);
} else {
// RETRY & Count = 0 (do nothing!)
}
}
}
}
/**
* The Method to call SDC for recoveryActioin RETRY
*
* @param queryDistributionDbResponse - OperationalEnvDistributionStatus object
* @param queryServiceModelResponse - OperationalEnvServiceModelStatus object
* @param sdcStatus - Distribution object
* @return JSONObject object
*/
public JSONObject callSDClientForRetry(OperationalEnvDistributionStatus queryDistributionDbResponse,
OperationalEnvServiceModelStatus queryServiceModelResponse, Distribution sdcStatus) throws ApiException {
JSONObject jsonResponse = null;
String operEnvironmentId = queryDistributionDbResponse.getOperationalEnvId();
String serviceModelVersionId = queryDistributionDbResponse.getServiceModelVersionId();
String originalRequestId = queryServiceModelResponse.getRequestId();
int retryCount = queryServiceModelResponse.getRetryCount();
String workloadContext = queryServiceModelResponse.getWorkloadContext();
jsonResponse = sdcClientHelper.postActivateOperationalEnvironment(serviceModelVersionId, operEnvironmentId,
workloadContext);
String statusCode = jsonResponse.get("statusCode").toString();
if (statusCode.equals(String.valueOf(Response.Status.ACCEPTED.getStatusCode()))) {
String newDistributionId = jsonResponse.get("distributionId").toString();
// should insert 1 row, NEW distributionId for replacement of the serviceModelServiceId record
OperationalEnvDistributionStatus insertNewDistributionId =
dbHelper.insertRecordToOperationalEnvDistributionStatus(newDistributionId, operEnvironmentId,
serviceModelVersionId, originalRequestId, DISTRIBUTION_STATUS_SENT, "");
client.save(insertNewDistributionId);
// update retryCount (less 1) for the serviceModelServiceId
retryCount = retryCount - 1;
// should update 1 row, original insert
OperationalEnvServiceModelStatus updateRetryCountAndStatus =
dbHelper.updateRetryCountAndStatusInOperationalEnvServiceModelStatus(queryServiceModelResponse,
DISTRIBUTION_STATUS_SENT, retryCount);
client.save(updateRetryCountAndStatus);
// should update 1 row, OLD distributionId set to status error (ie, old distributionId is DONE!).
OperationalEnvDistributionStatus updateStatus = dbHelper.updateStatusInOperationalEnvDistributionStatus(
queryDistributionDbResponse, DISTRIBUTION_STATUS_ERROR, sdcStatus.getErrorReason());
client.save(updateStatus);
} else {
String dbErrorMessage = "Failure calling SDC: statusCode: " + statusCode + "; messageId: "
+ jsonResponse.get("messageId") + "; message: " + jsonResponse.get("message");
ErrorLoggerInfo errorLoggerInfo =
new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError)
.build();
ValidateException validateException =
new ValidateException.Builder(dbErrorMessage, HttpStatus.SC_BAD_REQUEST,
ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
requestDb.updateInfraFailureCompletion(dbErrorMessage, this.origRequestId, operEnvironmentId);
throw validateException;
}
return jsonResponse;
}
/**
* The Method to check the overall status of the Activation for an operationalEnvironmentId
*
* @param operationalEnvironmentId - string
* @param origRequestId - string
* @return void - nothing
*/
public void checkOrUpdateOverallStatus(String operationalEnvironmentId, String origRequestId) throws ApiException {
List<OperationalEnvServiceModelStatus> queryServiceModelResponseList =
client.getAllByOperationalEnvIdAndRequestId(operationalEnvironmentId, origRequestId);
String status = "Waiting";
int count = 0;
// loop through the statuses of the service model
for (OperationalEnvServiceModelStatus querySrvModelResponse : queryServiceModelResponseList) {
status = querySrvModelResponse.getServiceModelVersionDistrStatus();
// all should be OK to be completed.
if ((status.equals(DistributionStatus.DISTRIBUTION_COMPLETE_OK.toString())
&& (querySrvModelResponse.getRetryCount() == 0))) {
status = "Completed";
count++;
}
// one error with zero retry, means all are failures.
if ((status.equals(DistributionStatus.DISTRIBUTION_COMPLETE_ERROR.toString())
&& (querySrvModelResponse.getRetryCount() == 0))) {
status = "Failure";
count = queryServiceModelResponseList.size();
break;
}
}
if (status.equals("Completed") && queryServiceModelResponseList.size() == count) {
String messageStatus = "Overall Activation process is complete. " + status;
isOverallSuccess = true;
requestDb.updateInfraSuccessCompletion(messageStatus, origRequestId, operationalEnvironmentId);
} else {
if (status.equals("Failure") && queryServiceModelResponseList.size() == count) {
this.errorMessage = "Overall Activation process is a Failure. " + status;
ErrorLoggerInfo errorLoggerInfo =
new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError)
.build();
ValidateException validateException =
new ValidateException.Builder(this.errorMessage, HttpStatus.SC_BAD_REQUEST,
ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
requestDb.updateInfraFailureCompletion(this.errorMessage, origRequestId, operationalEnvironmentId);
throw validateException;
}
}
}
/**
* Get OperationalEnvironment object
*
* @param operationalEnvironmentId - String
* @return operationalEnv - OperationalEnvironment object
*/
private OperationalEnvironment getAAIOperationalEnvironment(String operationalEnvironmentId) {
AAIResultWrapper aaiResult = aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);
Optional<OperationalEnvironment> operationalEnvironmentOpt = aaiResult.asBean(OperationalEnvironment.class);
return operationalEnvironmentOpt.isPresent() ? operationalEnvironmentOpt.get() : null;
}
}
|