2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
18 import com.google.common.collect.Ordering;
19 import com.google.common.collect.Sets;
20 import com.google.gson.Gson;
21 import com.google.gson.JsonElement;
22 import com.nokia.cbam.lcm.v32.api.OperationExecutionsApi;
23 import com.nokia.cbam.lcm.v32.api.VnfsApi;
24 import com.nokia.cbam.lcm.v32.model.OperationExecution;
25 import com.nokia.cbam.lcm.v32.model.OperationType;
26 import com.nokia.cbam.lcm.v32.model.VnfInfo;
28 import javax.servlet.http.HttpServletResponse;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
30 import org.onap.vnfmdriver.model.JobDetailInfo;
31 import org.onap.vnfmdriver.model.JobDetailInfoResponseDescriptor;
32 import org.onap.vnfmdriver.model.JobResponseInfo;
33 import org.onap.vnfmdriver.model.JobStatus;
34 import org.slf4j.Logger;
36 import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
37 import static java.util.Optional.empty;
38 import static java.util.Optional.of;
40 import static com.google.common.base.Splitter.on;
41 import static com.google.common.collect.Iterables.find;
42 import static com.google.common.collect.Iterables.tryFind;
43 import static com.google.common.collect.Lists.newArrayList;
44 import static com.nokia.cbam.lcm.v32.model.OperationStatus.FAILED;
45 import static com.nokia.cbam.lcm.v32.model.OperationStatus.STARTED;
46 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
47 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
48 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
49 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION;
50 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.LifecycleChangeNotificationManager.NEWEST_OPERATIONS_FIRST;
51 import static org.slf4j.LoggerFactory.getLogger;
52 import static org.springframework.util.StringUtils.isEmpty;
55 * Responsible for providing the status of jobs
56 * The job id is a composite field of the VNF identifier and an UUID.
57 * The second UUID is passed as mandatory parameter to each executed operation.
58 * This UUID is used to locate the operation execution from the ONAP job identifier
60 public class JobManager {
61 public static final String OPERATION_STARTED_DESCRIPTION = "Operation started";
62 private static final Ordering<JobResponseInfo> OLDEST_FIRST = new Ordering<JobResponseInfo>() {
64 public int compare(JobResponseInfo left, JobResponseInfo right) {
65 return Long.valueOf(left.getResponseId()).compareTo(Long.valueOf(right.getResponseId()));
68 private static Logger logger = getLogger(JobManager.class);
69 private final Set<String> ongoingJobs = Sets.newConcurrentHashSet();
70 private final CbamRestApiProvider cbamRestApiProvider;
71 private final SelfRegistrationManager selfRegistrationManager;
72 private volatile boolean preparingForShutDown = false;
74 JobManager(CbamRestApiProvider cbamRestApiProvider, SelfRegistrationManager selfRegistrationManager) {
75 this.cbamRestApiProvider = cbamRestApiProvider;
76 this.selfRegistrationManager = selfRegistrationManager;
80 * @param operationParams the operation execution
81 * @return the ONAP job identifier of belonging to the operation execution
83 public static String extractOnapJobId(Object operationParams) {
84 JsonElement operationParamsAsJson = new Gson().toJsonTree(operationParams);
85 JsonElement additionalParams = operationParamsAsJson.getAsJsonObject().get("additionalParams");
86 if (additionalParams == null) {
87 throw new NoSuchElementException("The operation result " + operationParamsAsJson + " does not contain the mandatory additionalParams structure");
89 JsonElement jobId = additionalParams.getAsJsonObject().get("jobId");
91 throw new NoSuchElementException("The operation result " + operationParamsAsJson + " does not contain the mandatory jobId in the additionalParams structure");
93 return jobId.getAsString();
97 * @return is the component preparing for shutdown
99 public boolean isPreparingForShutDown() {
100 return preparingForShutDown;
104 * Throws an exception in case the service is not ready to serve requests due to
105 * not being able to register to MSB or to subscribe to CBAM LCNs
107 * @param vnfId the identifier of the VNF
108 * @param response the HTTP response of the current sVNFM incomming request
109 * @return the identifier of the job
111 public String spawnJob(String vnfId, HttpServletResponse response) {
112 String jobId = vnfId + SEPARATOR + UUID.randomUUID().toString();
113 synchronized (this) {
114 if (preparingForShutDown) {
115 response.setStatus(SC_SERVICE_UNAVAILABLE);
116 throw buildFatalFailure(logger, "The service is preparing to shut down");
118 if (!selfRegistrationManager.isReady()) {
119 response.setStatus(SC_SERVICE_UNAVAILABLE);
120 throw buildFatalFailure(logger, "The service is not yet ready");
123 ongoingJobs.add(jobId);
128 * Signal that a job has finished
130 * @param jobId the identifier of the job
132 public void jobFinished(String jobId) {
133 ongoingJobs.remove(jobId);
137 * @return the system has any ongoing jobs
139 public boolean hasOngoingJobs() {
140 return !ongoingJobs.isEmpty();
145 * Wait for all jobs to be cleared from the system the refuses to let additional request in
147 public void prepareForShutdown() {
148 preparingForShutDown = true;
150 synchronized (this) {
151 if (!hasOngoingJobs()) {
155 systemFunctions().sleep(500L);
160 * @param vnfmId the identifier of the VNFM
161 * @param jobId the identifier of the job
162 * @return detailed information of the job
164 public JobDetailInfo getJob(String vnfmId, String jobId) {
165 logger.debug("Retrieving the details for job with {} identifier", jobId);
166 ArrayList<String> jobParts = newArrayList(on(SEPARATOR).split(jobId));
167 if (jobParts.size() != 2) {
168 throw new IllegalArgumentException("The jobId should be in the <vnfId>" + SEPARATOR + "<UUID> format, but was " + jobId);
170 String vnfId = jobParts.get(0);
171 if (isEmpty(vnfId)) {
172 throw new IllegalArgumentException("The vnfId in the jobId (" + jobId + ") can not be empty");
174 String operationExecutionId = jobParts.get(1);
175 if (isEmpty(operationExecutionId)) {
176 throw new IllegalArgumentException("The UUID in the jobId (" + jobId + ") can not be empty");
178 Optional<VnfInfo> vnf = getVnf(vnfmId, vnfId);
179 if (!vnf.isPresent()) {
180 return getJobDetailInfoForMissingVnf(jobId);
182 return getJobInfoForExistingVnf(vnfmId, jobId, vnfId, vnf.get());
186 private JobDetailInfo getJobDetailInfoForMissingVnf(String jobId) {
187 if (ongoingJobs.contains(jobId)) {
188 return reportOngoing(jobId);
190 return reportFinished(jobId);
194 private JobDetailInfo getJobInfoForExistingVnf(String vnfmId, String jobId, String vnfId, VnfInfo vnf) {
196 OperationExecution operation = findOperationByJobId(vnfmId, vnf, jobId);
197 return getJobDetailInfo(vnfmId, jobId, vnfId, operation);
198 } catch (NoSuchElementException e) {
199 logger.warn("No operation could be identified for job with {} identifier", jobId, e);
200 if (ongoingJobs.contains(jobId)) {
201 return reportOngoing(jobId);
203 return reportFailed(jobId, "The requested operation was not able to start on CBAM");
208 private JobDetailInfo getJobDetailInfo(String vnfmId, String jobId, String vnfId, OperationExecution operation) {
209 if (operation.getStatus() == STARTED) {
210 return reportOngoing(jobId);
211 } else if (operation.getStatus() == FAILED) {
212 return reportFailed(jobId, operation.getError().getTitle() + ": " + operation.getError().getDetail());
214 return getJobForTerminalOperationState(vnfmId, jobId, vnfId, operation);
218 private JobDetailInfo getJobForTerminalOperationState(String vnfmId, String jobId, String vnfId, OperationExecution operation) {
219 //termination includes VNF deletion in ONAP terminology
220 if (operation.getOperationType() == com.nokia.cbam.lcm.v32.model.OperationType.TERMINATE) {
221 if (ongoingJobs.contains(jobId)) {
222 return reportOngoing(jobId);
224 //the VNF must be queried again since it could have been deleted since the VNF has been terminated
225 if (getVnf(vnfmId, vnfId).isPresent()) {
226 return reportFailed(jobId, "unable to delete VNF");
228 return reportFinished(jobId);
232 return reportFinished(jobId);
236 private JobDetailInfo buildJob(String jobId, JobResponseInfo... history) {
237 JobDetailInfo job = new JobDetailInfo();
239 JobDetailInfoResponseDescriptor jobDetailInfoResponseDescriptor = new JobDetailInfoResponseDescriptor();
240 job.setResponseDescriptor(jobDetailInfoResponseDescriptor);
241 List<JobResponseInfo> oldestFirst = OLDEST_FIRST.sortedCopy(newArrayList(history));
242 JobResponseInfo newestJob = oldestFirst.get(oldestFirst.size() - 1);
243 jobDetailInfoResponseDescriptor.setResponseId(newestJob.getResponseId());
244 jobDetailInfoResponseDescriptor.setStatus(JobStatus.valueOf(newestJob.getStatus()));
245 jobDetailInfoResponseDescriptor.setProgress(newestJob.getProgress());
246 jobDetailInfoResponseDescriptor.setStatusDescription(newestJob.getStatusDescription());
247 jobDetailInfoResponseDescriptor.setErrorCode(newestJob.getErrorCode());
248 jobDetailInfoResponseDescriptor.setResponseHistoryList(oldestFirst);
252 private JobResponseInfo buildJobPart(String description, JobStatus status, Integer progress, Integer responseId) {
253 JobResponseInfo currentJob = new JobResponseInfo();
254 currentJob.setProgress(progress.toString());
255 currentJob.setResponseId(responseId.toString());
256 currentJob.setStatus(status.name());
257 currentJob.setStatusDescription(description);
261 private JobDetailInfo reportOngoing(String jobId) {
262 return buildJob(jobId, buildJobPart(OPERATION_STARTED_DESCRIPTION, JobStatus.STARTED, 50, 1));
265 private JobDetailInfo reportFailed(String jobId, String reason) {
266 return buildJob(jobId,
267 buildJobPart(OPERATION_STARTED_DESCRIPTION, JobStatus.STARTED, 50, 1),
268 buildJobPart("Operation failed due to " + reason, JobStatus.ERROR, 100, 2)
272 private JobDetailInfo reportFinished(String jobId) {
273 return buildJob(jobId,
274 buildJobPart(OPERATION_STARTED_DESCRIPTION, JobStatus.STARTED, 50, 1),
275 buildJobPart("Operation finished", JobStatus.FINISHED, 100, 2)
279 private OperationExecution findOperationByJobId(String vnfmId, VnfInfo vnf, String jobId) {
280 OperationExecutionsApi cbamOperationExecutionApi = cbamRestApiProvider.getCbamOperationExecutionApi(vnfmId);
281 //the operations are sorted so that the newest operations are queried first
282 //performance optimization that usually the core system is interested in the operations executed last
283 if (vnf.getOperationExecutions() != null) {
284 List<OperationExecution> sortedOperation = NEWEST_OPERATIONS_FIRST.sortedCopy(vnf.getOperationExecutions());
285 return find(sortedOperation, operation -> isCurrentOperationTriggeredByJob(jobId, cbamOperationExecutionApi, operation));
287 throw new NoSuchElementException();
290 private boolean isCurrentOperationTriggeredByJob(String jobId, OperationExecutionsApi cbamOperationExecutionApi, OperationExecution operationExecution) {
291 if (OperationType.MODIFY_INFO.equals(operationExecution.getOperationType())) {
292 //the modify info is never triggered by an external job
296 Object operationParams = cbamOperationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operationExecution.getId(), NOKIA_LCM_API_VERSION).blockingFirst();
297 if (extractOnapJobId(operationParams).equals(jobId)) {
300 } catch (Exception e) {
301 throw buildFatalFailure(logger, "Unable to retrieve operation parameters of operation with " + operationExecution.getId() + " identifier", e);
306 private Optional<VnfInfo> getVnf(String vnfmId, String vnfId) {
308 //test if the VNF exists (required to be able to distingush between failed request )
309 VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(vnfmId);
310 logger.debug("Listing VNFs");
311 List<VnfInfo> vnfs = cbamLcmApi.vnfsGet(NOKIA_LCM_API_VERSION).blockingSingle();
312 com.google.common.base.Optional<VnfInfo> vnf = tryFind(vnfs, vnfInfo -> vnfId.equals(vnfInfo.getId()));
313 if (!vnf.isPresent()) {
314 logger.debug("VNF with {} identifier is missing", vnfId);
317 logger.debug("VNF with {} identifier still exists", vnfId);
318 //query the VNF again to get operation execution result
319 return of(cbamLcmApi.vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst());
321 } catch (Exception e) {
322 throw buildFatalFailure(logger, "Unable to retrieve VNF with " + vnfId + " identifier", e);