Adding missing tests
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / JobManager.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
17
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.ApiException;
23 import com.nokia.cbam.lcm.v32.api.OperationExecutionsApi;
24 import com.nokia.cbam.lcm.v32.api.VnfsApi;
25 import com.nokia.cbam.lcm.v32.model.OperationExecution;
26 import com.nokia.cbam.lcm.v32.model.VnfInfo;
27 import org.apache.http.HttpStatus;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
29 import org.onap.vnfmdriver.model.JobDetailInfo;
30 import org.onap.vnfmdriver.model.JobDetailInfoResponseDescriptor;
31 import org.onap.vnfmdriver.model.JobResponseInfo;
32 import org.onap.vnfmdriver.model.JobStatus;
33 import org.slf4j.Logger;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36
37 import javax.servlet.http.HttpServletResponse;
38 import java.util.*;
39
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 java.util.Optional.empty;
47 import static java.util.Optional.of;
48 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
49 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
50 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
51 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION;
52 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.LifecycleChangeNotificationManager.NEWEST_OPERATIONS_FIRST;
53 import static org.slf4j.LoggerFactory.getLogger;
54 import static org.springframework.util.StringUtils.isEmpty;
55
56 /**
57  * Responsible for providing the status of jobs
58  * The job id is a composite field of the VNF identifier and an UUID.
59  * The second UUID is passed as mandatory parameter to each executed operation.
60  * This UUID is used to locate the operation execution from the ONAP job identifier
61  */
62 @Component
63 public class JobManager {
64     public static final String OPERATION_STARTED_DESCRIPTION = "Operation started";
65     private static final Ordering<JobResponseInfo> OLDEST_FIRST = new Ordering<JobResponseInfo>() {
66         @Override
67         public int compare(JobResponseInfo left, JobResponseInfo right) {
68             return Long.valueOf(left.getResponseId()).compareTo(Long.valueOf(right.getResponseId()));
69         }
70     };
71     private static Logger logger = getLogger(JobManager.class);
72     private final Set<String> ongoingJobs = Sets.newConcurrentHashSet();
73     private final CbamRestApiProvider cbamRestApiProvider;
74     private final SelfRegistrationManager selfRegistrationManager;
75     private volatile boolean preparingForShutDown = false;
76
77     @Autowired
78     JobManager(CbamRestApiProvider cbamRestApiProvider, SelfRegistrationManager selfRegistrationManager) {
79         this.cbamRestApiProvider = cbamRestApiProvider;
80         this.selfRegistrationManager = selfRegistrationManager;
81     }
82
83     /**
84      * @param operationParams the operation execution
85      * @return the ONAP job identifier of belonging to the operation execution
86      */
87     public static String extractOnapJobId(Object operationParams) {
88         JsonElement operationParamsAsJson = new Gson().toJsonTree(operationParams);
89         JsonElement additionalParams = operationParamsAsJson.getAsJsonObject().get("additionalParams");
90         if (additionalParams == null) {
91             throw new NoSuchElementException("The operation result " + operationParamsAsJson + " does not contain the mandatory additionalParams structure");
92         }
93         JsonElement jobId = additionalParams.getAsJsonObject().get("jobId");
94         if (jobId == null) {
95             throw new NoSuchElementException("The operation result " + operationParamsAsJson + " does not contain the mandatory jobId in the additionalParams structure");
96         }
97         return jobId.getAsString();
98     }
99
100     /**
101      * Throws an exception in case the service is not ready to serve requests due to
102      * not being able to register to MSB or to subscribe to CBAM LCNs
103      *
104      * @param vnfId    the identifier of the VNF
105      * @param response the HTTP response of the current sVNFM incomming request
106      * @return the identifier of the job
107      */
108     public String spawnJob(String vnfId, HttpServletResponse response) {
109         String jobId = vnfId + SEPARATOR + UUID.randomUUID().toString();
110         synchronized (this) {
111             if (preparingForShutDown) {
112                 response.setStatus(HttpStatus.SC_SERVICE_UNAVAILABLE);
113                 throw buildFatalFailure(logger, "The service is preparing to shut down");
114             }
115             if (!selfRegistrationManager.isReady()) {
116                 response.setStatus(HttpStatus.SC_SERVICE_UNAVAILABLE);
117                 throw buildFatalFailure(logger, "The service is not yet ready");
118             }
119         }
120         ongoingJobs.add(jobId);
121         return jobId;
122     }
123
124     /**
125      * Signal that a job has finished
126      *
127      * @param jobId the identifier of the job
128      */
129     public void jobFinished(String jobId) {
130         ongoingJobs.remove(jobId);
131     }
132
133     /**
134      * @return the system has any ongoing jobs
135      */
136     public boolean hasOngoingJobs() {
137         return !ongoingJobs.isEmpty();
138     }
139
140
141     /**
142      * Wait for all jobs to be cleared from the system the refuses to let additional request in
143      */
144     public void prepareForShutdown() {
145         preparingForShutDown = true;
146         while (true) {
147             synchronized (this) {
148                 if (!hasOngoingJobs()) {
149                     return;
150                 }
151             }
152             systemFunctions().sleep(500L);
153         }
154     }
155
156     /**
157      * @param vnfmId the identifier of the VNFM
158      * @param jobId  the identifier of the job
159      * @return detailed information of the job
160      */
161     public JobDetailInfo getJob(String vnfmId, String jobId) {
162         logger.debug("Retrieving the details for job with {} identifier", jobId);
163         ArrayList<String> jobParts = newArrayList(on(SEPARATOR).split(jobId));
164         if (jobParts.size() != 2) {
165             throw new IllegalArgumentException("The jobId should be in the <vnfId>" + SEPARATOR + "<UUID> format, but was " + jobId);
166         }
167         String vnfId = jobParts.get(0);
168         if (isEmpty(vnfId)) {
169             throw new IllegalArgumentException("The vnfId in the jobId (" + jobId + ") can not be empty");
170         }
171         String operationExecutionId = jobParts.get(1);
172         if (isEmpty(operationExecutionId)) {
173             throw new IllegalArgumentException("The UUID in the jobId (" + jobId + ") can not be empty");
174         }
175         Optional<VnfInfo> vnf = getVnf(vnfmId, vnfId);
176         if (!vnf.isPresent()) {
177             return getJobDetailInfoForMissingVnf(jobId);
178         } else {
179             return getJobInfoForExistingVnf(vnfmId, jobId, vnfId, vnf.get());
180         }
181     }
182
183     private JobDetailInfo getJobDetailInfoForMissingVnf(String jobId) {
184         if (ongoingJobs.contains(jobId)) {
185             return reportOngoing(jobId);
186         } else {
187             return reportFinished(jobId);
188         }
189     }
190
191     private JobDetailInfo getJobInfoForExistingVnf(String vnfmId, String jobId, String vnfId, VnfInfo vnf) {
192         try {
193             OperationExecution operation = findOperationByJobId(vnfmId, vnf, jobId);
194             return getJobDetailInfo(vnfmId, jobId, vnfId, operation);
195         } catch (NoSuchElementException e) {
196             logger.warn("No operation could be identified for job with {} identifier", jobId, e);
197             if (ongoingJobs.contains(jobId)) {
198                 return reportOngoing(jobId);
199             } else {
200                 return reportFailed(jobId, "The requested operation was not able to start on CBAM");
201             }
202         }
203     }
204
205     private JobDetailInfo getJobDetailInfo(String vnfmId, String jobId, String vnfId, OperationExecution operation) {
206         if (operation.getStatus() == STARTED) {
207             return reportOngoing(jobId);
208         } else if (operation.getStatus() == FAILED) {
209             return reportFailed(jobId, operation.getError().getTitle() + ": " + operation.getError().getDetail());
210         } else {
211             return getJobForTerminalOperationState(vnfmId, jobId, vnfId, operation);
212         }
213     }
214
215     private JobDetailInfo getJobForTerminalOperationState(String vnfmId, String jobId, String vnfId, OperationExecution operation) {
216         //termination includes VNF deletion in ONAP terminology
217         if (operation.getOperationType() == com.nokia.cbam.lcm.v32.model.OperationType.TERMINATE) {
218             if (ongoingJobs.contains(jobId)) {
219                 return reportOngoing(jobId);
220             } else {
221                 //the VNF must be queried again since it could have been deleted since the VNF has been terminated
222                 if (getVnf(vnfmId, vnfId).isPresent()) {
223                     return reportFailed(jobId, "unable to delete VNF");
224                 } else {
225                     return reportFinished(jobId);
226                 }
227             }
228         } else {
229             return reportFinished(jobId);
230         }
231     }
232
233     private JobDetailInfo buildJob(String jobId, JobResponseInfo... history) {
234         JobDetailInfo job = new JobDetailInfo();
235         job.setJobId(jobId);
236         JobDetailInfoResponseDescriptor jobDetailInfoResponseDescriptor = new JobDetailInfoResponseDescriptor();
237         job.setResponseDescriptor(jobDetailInfoResponseDescriptor);
238         List<JobResponseInfo> oldestFirst = OLDEST_FIRST.sortedCopy(newArrayList(history));
239         JobResponseInfo newestJob = oldestFirst.get(oldestFirst.size() - 1);
240         jobDetailInfoResponseDescriptor.setResponseId(newestJob.getResponseId());
241         jobDetailInfoResponseDescriptor.setStatus(JobStatus.valueOf(newestJob.getStatus()));
242         jobDetailInfoResponseDescriptor.setProgress(newestJob.getProgress());
243         jobDetailInfoResponseDescriptor.setStatusDescription(newestJob.getStatusDescription());
244         jobDetailInfoResponseDescriptor.setErrorCode(newestJob.getErrorCode());
245         jobDetailInfoResponseDescriptor.setResponseHistoryList(oldestFirst);
246         return job;
247     }
248
249     private JobResponseInfo buildJobPart(String description, JobStatus status, Integer progress, Integer responseId) {
250         JobResponseInfo currentJob = new JobResponseInfo();
251         currentJob.setProgress(progress.toString());
252         currentJob.setResponseId(responseId.toString());
253         currentJob.setStatus(status.name());
254         currentJob.setStatusDescription(description);
255         return currentJob;
256     }
257
258     private JobDetailInfo reportOngoing(String jobId) {
259         return buildJob(jobId, buildJobPart(OPERATION_STARTED_DESCRIPTION, JobStatus.STARTED, 50, 1));
260     }
261
262     private JobDetailInfo reportFailed(String jobId, String reason) {
263         return buildJob(jobId,
264                 buildJobPart(OPERATION_STARTED_DESCRIPTION, JobStatus.STARTED, 50, 1),
265                 buildJobPart("Operation failed due to " + reason, JobStatus.ERROR, 100, 2)
266         );
267     }
268
269     private JobDetailInfo reportFinished(String jobId) {
270         return buildJob(jobId,
271                 buildJobPart(OPERATION_STARTED_DESCRIPTION, JobStatus.STARTED, 50, 1),
272                 buildJobPart("Operation finished", JobStatus.FINISHED, 100, 2)
273         );
274     }
275
276     private OperationExecution findOperationByJobId(String vnfmId, VnfInfo vnf, String jobId) {
277         OperationExecutionsApi cbamOperationExecutionApi = cbamRestApiProvider.getCbamOperationExecutionApi(vnfmId);
278         //the operations are sorted so that the newest operations are queried first
279         //performance optimization that usually the core system is interested in the operations executed last
280         if (vnf.getOperationExecutions() != null) {
281             List<OperationExecution> sortedOperation = NEWEST_OPERATIONS_FIRST.sortedCopy(vnf.getOperationExecutions());
282             return find(sortedOperation, operation -> isCurrentOperationTriggeredByJob(jobId, cbamOperationExecutionApi, operation));
283         }
284         throw new NoSuchElementException();
285     }
286
287     private boolean isCurrentOperationTriggeredByJob(String jobId, OperationExecutionsApi cbamOperationExecutionApi, OperationExecution operationExecution) {
288         try {
289             Object operationParams = cbamOperationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operationExecution.getId(), NOKIA_LCM_API_VERSION);
290             if (extractOnapJobId(operationParams).equals(jobId)) {
291                 return true;
292             }
293         } catch (ApiException e) {
294             throw buildFatalFailure(logger, "Unable to retrieve operation parameters", e);
295         }
296         return false;
297     }
298
299     private Optional<VnfInfo> getVnf(String vnfmId, String vnfId) {
300         try {
301             //test if the VNF exists (required to be able to distingush between failed request )
302             VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(vnfmId);
303             logger.debug("Listing VNFs");
304             List<VnfInfo> vnfs = cbamLcmApi.vnfsGet(NOKIA_LCM_API_VERSION);
305             com.google.common.base.Optional<VnfInfo> vnf = tryFind(vnfs, vnfInfo -> vnfId.equals(vnfInfo.getId()));
306             if (!vnf.isPresent()) {
307                 logger.debug("VNF with {} identifier is missing", vnfId);
308                 return empty();
309             } else {
310                 logger.debug("VNF with {} identifier still exists", vnfId);
311                 //query the VNF again to get operation execution result
312                 return of(cbamLcmApi.vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION));
313             }
314         } catch (ApiException e) {
315             throw buildFatalFailure(logger, "Unable to retrieve VNF", e);
316         }
317     }
318 }