89356c1b67a4ad96e6e963caaf8e6bad06b6f730
[so.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.jobmanagement;
22
23 import static org.slf4j.LoggerFactory.getLogger;
24 import com.google.common.base.Optional;
25 import com.google.common.collect.Maps;
26 import java.util.Map;
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.so.adapters.vnfmadapter.rest.exceptions.JobNotFoundException;
31 import org.onap.vnfmadapter.v1.model.OperationEnum;
32 import org.onap.vnfmadapter.v1.model.OperationStateEnum;
33 import org.onap.vnfmadapter.v1.model.OperationStatusRetrievalStatusEnum;
34 import org.onap.vnfmadapter.v1.model.QueryJobResponse;
35 import org.slf4j.Logger;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38
39 /**
40  * Manages jobs enabling the status of jobs to be queried. A job is associated with an operation on
41  * a VNFM.
42  */
43 @Component
44 public class JobManager {
45     private static final String SEPARATOR = "_";
46     private static Logger logger = getLogger(JobManager.class);
47     private final Map<String, VnfmOperation> mapOfJobIdToVnfmOperation = Maps.newConcurrentMap();
48     private final VnfmServiceProvider vnfmServiceProvider;
49
50     @Autowired
51     JobManager(final VnfmServiceProvider vnfmServiceProvider) {
52         this.vnfmServiceProvider = vnfmServiceProvider;
53     }
54
55     /**
56      * Create a job associated with an operation on a VNFM.
57      *
58      * @param vnfmId the VNFM the operation relates to
59      * @param operationId the ID of the associated VNFM operation
60      * @param waitForNotificationForSuccess if set to <code>true</code> the
61      *        {@link QueryJobResponse#getOperationState()} shall not return
62      *        {@link org.onap.vnfmadapter.v1.model.OperationStateEnum#COMPLETED} unless a required
63      *        notification has been processed
64      * @return the ID of the job. Can be used to query the job using {@link #getVnfmOperation(String)}
65      */
66     public String createJob(final String vnfmId, final String operationId,
67             final boolean waitForNotificationForSuccess) {
68         final String jobId = vnfmId + SEPARATOR + UUID.randomUUID().toString();
69         final VnfmOperation vnfmOperation = new VnfmOperation(vnfmId, operationId, waitForNotificationForSuccess);
70         mapOfJobIdToVnfmOperation.put(jobId, vnfmOperation);
71         return jobId;
72     }
73
74     /**
75      * Get the operation, associated with the given job ID, from the VNFM.
76      *
77      * @param jobId the job ID
78      * @return the associated operation from the VNFM, or <code>null</code> of no operation is
79      *         associated with the given job ID
80      */
81     public QueryJobResponse getVnfmOperation(final String jobId) {
82         final VnfmOperation vnfmOperation = mapOfJobIdToVnfmOperation.get(jobId);
83         final QueryJobResponse response = new QueryJobResponse();
84
85         if (vnfmOperation == null) {
86             throw new JobNotFoundException("No job found with ID: " + jobId);
87         }
88
89         final Optional<InlineResponse200> operationOptional =
90                 vnfmServiceProvider.getOperation(vnfmOperation.getVnfmId(), vnfmOperation.getOperationId());
91         if (!operationOptional.isPresent()) {
92             return response.operationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.OPERATION_NOT_FOUND);
93         }
94         final InlineResponse200 operation = operationOptional.get();
95
96         logger.debug("Job Id: " + jobId + ", operationId: " + operation.getId() + ", operation details: " + operation);
97
98         response.setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
99         response.setId(operation.getId());
100         response.setOperation(OperationEnum.fromValue(operation.getOperation().getValue()));
101         response.setOperationState(getOperationState(vnfmOperation, operation));
102         response.setStartTime(operation.getStartTime());
103         response.setStateEnteredTime(operation.getStateEnteredTime());
104         response.setVnfInstanceId(operation.getVnfInstanceId());
105
106         return response;
107     }
108
109     private OperationStateEnum getOperationState(final VnfmOperation vnfmOperation,
110             final InlineResponse200 operationResponse) {
111         final OperationStateEnum operationState =
112                 OperationStateEnum.fromValue(operationResponse.getOperationState().getValue());
113         switch (vnfmOperation.getNotificationStatus()) {
114             case NOTIFICATION_PROCESSING_NOT_REQUIRED:
115             default:
116                 return operationState;
117             case NOTIFICATION_PROCESSING_PENDING:
118                 return org.onap.vnfmadapter.v1.model.OperationStateEnum.PROCESSING;
119             case NOTIFICATION_PROCEESING_SUCCESSFUL:
120                 return operationState;
121             case NOTIFICATION_PROCESSING_FAILED:
122                 return org.onap.vnfmadapter.v1.model.OperationStateEnum.FAILED;
123         }
124     }
125
126     public void notificationProcessedForOperation(final String operationId,
127             final boolean notificationProcessingWasSuccessful) {
128         final java.util.Optional<VnfmOperation> relatedOperation = mapOfJobIdToVnfmOperation.values().stream()
129                 .filter(operation -> operation.getOperationId().equals(operationId)).findFirst();
130         if (relatedOperation.isPresent()) {
131             relatedOperation.get().setNotificationProcessed(notificationProcessingWasSuccessful);
132         }
133         logger.debug("No operation found for operation ID " + operationId);
134     }
135
136 }