2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnfmadapter.jobmanagement;
23 import static org.slf4j.LoggerFactory.getLogger;
24 import com.google.common.base.Optional;
25 import com.google.common.collect.Maps;
27 import java.util.UUID;
28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmServiceProvider;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
30 import org.onap.vnfmadapter.v1.model.OperationEnum;
31 import org.onap.vnfmadapter.v1.model.OperationStateEnum;
32 import org.onap.vnfmadapter.v1.model.OperationStatusRetrievalStatusEnum;
33 import org.onap.vnfmadapter.v1.model.QueryJobResponse;
34 import org.slf4j.Logger;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
39 * Manages jobs enabling the status of jobs to be queried. A job is associated with an operation on
43 public class JobManager {
44 private static final String SEPARATOR = "_";
45 private static Logger logger = getLogger(JobManager.class);
46 private final Map<String, VnfmOperation> mapOfJobIdToVnfmOperation = Maps.newConcurrentMap();
47 private final VnfmServiceProvider vnfmServiceProvider;
50 JobManager(final VnfmServiceProvider vnfmServiceProvider) {
51 this.vnfmServiceProvider = vnfmServiceProvider;
55 * Create a job associated with an operation on a VNFM.
57 * @param vnfmId the VNFM the operation relates to
58 * @param operationId the ID of the associated VNFM operation
59 * @param waitForNotificationForSuccess if set to <code>true</code> the
60 * {@link QueryJobResponse#getOperationState()} shall not return
61 * {@link org.onap.vnfmadapter.v1.model.OperationStateEnum#COMPLETED} unless a required
62 * notification has been processed
63 * @return the ID of the job. Can be used to query the job using {@link #getVnfmOperation(String)}
65 public String createJob(final String vnfmId, final String operationId,
66 final boolean waitForNotificationForSuccess) {
67 final String jobId = vnfmId + SEPARATOR + UUID.randomUUID().toString();
68 final VnfmOperation vnfmOperation = new VnfmOperation(vnfmId, operationId, waitForNotificationForSuccess);
69 mapOfJobIdToVnfmOperation.put(jobId, vnfmOperation);
74 * Get the operation, associated with the given job ID, from the VNFM.
76 * @param jobId the job ID
77 * @return the associated operation from the VNFM, or <code>null</code> of no operation is
78 * associated with the given job ID
80 public QueryJobResponse getVnfmOperation(final String jobId) {
81 final VnfmOperation vnfmOperation = mapOfJobIdToVnfmOperation.get(jobId);
82 final QueryJobResponse response = new QueryJobResponse();
84 if (vnfmOperation == null) {
88 final Optional<InlineResponse200> operationOptional =
89 vnfmServiceProvider.getOperation(vnfmOperation.getVnfmId(), vnfmOperation.getOperationId());
90 if (!operationOptional.isPresent()) {
91 return response.operationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.OPERATION_NOT_FOUND);
93 final InlineResponse200 operation = operationOptional.get();
95 logger.debug("Job Id: " + jobId + ", operationId: " + operation.getId() + ", operation details: " + operation);
97 response.setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
98 response.setId(operation.getId());
99 response.setOperation(OperationEnum.fromValue(operation.getOperation().getValue()));
100 response.setOperationState(getOperationState(vnfmOperation, operation));
101 response.setStartTime(operation.getStartTime());
102 response.setStateEnteredTime(operation.getStateEnteredTime());
103 response.setVnfInstanceId(operation.getVnfInstanceId());
108 private OperationStateEnum getOperationState(final VnfmOperation vnfmOperation,
109 final InlineResponse200 operationResponse) {
110 final OperationStateEnum operationState =
111 OperationStateEnum.fromValue(operationResponse.getOperationState().getValue());
112 if (operationState == OperationStateEnum.COMPLETED && vnfmOperation.isWaitForNotificationForSuccess()
113 && !vnfmOperation.isNotificationProcessed()) {
114 return org.onap.vnfmadapter.v1.model.OperationStateEnum.PROCESSING;
116 return operationState;