d01e5b186c9ad653bce5c4d0857d6df6258fa60b
[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.aai.AaiServiceProvider;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmServiceProvider;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
31 import org.onap.so.adapters.vnfmadapter.rest.exceptions.JobNotFoundException;
32 import org.onap.vnfmadapter.v1.model.OperationEnum;
33 import org.onap.vnfmadapter.v1.model.OperationStateEnum;
34 import org.onap.vnfmadapter.v1.model.OperationStatusRetrievalStatusEnum;
35 import org.onap.vnfmadapter.v1.model.QueryJobResponse;
36 import org.slf4j.Logger;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39
40 /**
41  * Manages jobs enabling the status of jobs to be queried. A job is associated with an operation on 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     private final AaiServiceProvider aaiServiceProvider;
50
51     @Autowired
52     JobManager(final VnfmServiceProvider vnfmServiceProvider, final AaiServiceProvider aaiServiceProvider) {
53         this.vnfmServiceProvider = vnfmServiceProvider;
54         this.aaiServiceProvider = aaiServiceProvider;
55     }
56
57     /**
58      * Create a job associated with an operation on a VNFM.
59      *
60      * @param vnfmId the VNFM the operation relates to
61      * @param operationId the ID of the associated VNFM operation
62      * @param waitForNotificationForSuccess if set to <code>true</code> the {@link QueryJobResponse#getOperationState()}
63      *        shall not return {@link org.onap.vnfmadapter.v1.model.OperationStateEnum#COMPLETED} unless a required
64      *        notification has been processed
65      * @return the ID of the job. Can be used to query the job using {@link #getVnfmOperation(String)}
66      */
67     public String createJob(final String vnfmId, final String operationId,
68             final boolean waitForNotificationForSuccess) {
69         final String jobId = vnfmId + SEPARATOR + UUID.randomUUID().toString();
70         final VnfmOperation vnfmOperation = new VnfmOperation(vnfmId, operationId, waitForNotificationForSuccess);
71         mapOfJobIdToVnfmOperation.put(jobId, vnfmOperation);
72         return jobId;
73     }
74
75     /**
76      * Get the operation, associated with the given job ID, from the VNFM.
77      *
78      * @param jobId the job ID
79      * @return the associated operation from the VNFM, or <code>null</code> of no operation is associated with the given
80      *         job ID
81      */
82     public QueryJobResponse getVnfmOperation(final String jobId) {
83         final VnfmOperation vnfmOperation = mapOfJobIdToVnfmOperation.get(jobId);
84         final QueryJobResponse response = new QueryJobResponse();
85
86         if (vnfmOperation == null) {
87             throw new JobNotFoundException("No job found with ID: " + jobId);
88         }
89
90         if (vnfmOperation.isVnfDeleted()) {
91             response.setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
92             return response.operationState(getOperationState(vnfmOperation, null));
93         }
94
95         try {
96             final Optional<InlineResponse200> operationOptional = vnfmServiceProvider.getOperation(
97                     aaiServiceProvider.invokeGetVnfm(vnfmOperation.getVnfmId()), vnfmOperation.getOperationId());
98
99             if (!operationOptional.isPresent()) {
100                 return response.operationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.OPERATION_NOT_FOUND);
101             }
102             final InlineResponse200 operation = operationOptional.get();
103
104             logger.debug("Job Id: {} operationId: {} operation details: {} ", jobId, operation.getId(), operation);
105
106             if (operation.getOperationState() == null) {
107                 return response.operationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.WAITING_FOR_STATUS);
108             }
109
110             response.setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
111             response.setId(operation.getId());
112             response.setOperation(OperationEnum.fromValue(operation.getOperation().getValue()));
113             response.setOperationState(getOperationState(vnfmOperation, operation));
114             response.setStartTime(operation.getStartTime());
115             response.setStateEnteredTime(operation.getStateEnteredTime());
116             response.setVnfInstanceId(operation.getVnfInstanceId());
117
118             return response;
119         } catch (final Exception exception) {
120             logger.error("Exception encountered trying to get operation status for operation id "
121                     + vnfmOperation.getOperationId(), exception);
122             return response.operationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.WAITING_FOR_STATUS);
123         }
124     }
125
126     private OperationStateEnum getOperationState(final VnfmOperation vnfmOperation,
127             final InlineResponse200 operationResponse) {
128         switch (vnfmOperation.getNotificationStatus()) {
129             case NOTIFICATION_PROCESSING_NOT_REQUIRED:
130             default:
131                 return OperationStateEnum.fromValue(operationResponse.getOperationState().getValue());
132             case NOTIFICATION_PROCESSING_PENDING:
133                 return org.onap.vnfmadapter.v1.model.OperationStateEnum.PROCESSING;
134             case NOTIFICATION_PROCEESING_SUCCESSFUL:
135                 return org.onap.vnfmadapter.v1.model.OperationStateEnum.COMPLETED;
136             case NOTIFICATION_PROCESSING_FAILED:
137                 return org.onap.vnfmadapter.v1.model.OperationStateEnum.FAILED;
138         }
139     }
140
141     public void notificationProcessedForOperation(final String operationId,
142             final boolean notificationProcessingWasSuccessful) {
143         logger.debug("Notification processed for operation ID {} success?: {}", operationId,
144                 notificationProcessingWasSuccessful);
145         final java.util.Optional<VnfmOperation> relatedOperation = mapOfJobIdToVnfmOperation.values().stream()
146                 .filter(operation -> operation.getOperationId().equals(operationId)).findFirst();
147         if (relatedOperation.isPresent()) {
148             relatedOperation.get().setNotificationProcessed(notificationProcessingWasSuccessful);
149         } else {
150             logger.debug("No operation found for operation ID {} ", operationId);
151
152         }
153     }
154
155     public void vnfDeleted(final String operationId) {
156         logger.debug("VNF deleyed for operation ID {}", operationId);
157         final java.util.Optional<VnfmOperation> relatedOperation = mapOfJobIdToVnfmOperation.values().stream()
158                 .filter(operation -> operation.getOperationId().equals(operationId)).findFirst();
159         if (relatedOperation.isPresent()) {
160             relatedOperation.get().setVnfDeleted();;
161         } else {
162             logger.debug("No operation found for operation ID {} ", operationId);
163         }
164     }
165
166 }