summaryrefslogtreecommitdiffstats
path: root/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/service/JobExecutorService.java
blob: 0f6e8d080e7f5aacaf6aeedb18c7302c303cdaf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.so.cnfm.lcm.bpmn.flows.service;

import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.AS_INSTANCE_ID_PARAM_NAME;
import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.CREATE_AS_REQUEST_PARAM_NAME;
import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.INSTANTIATE_AS_REQUEST_PARAM_NAME;
import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.JOB_ID_PARAM_NAME;
import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.OCC_ID_PARAM_NAME;
import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.TERMINATE_AS_REQUEST_PARAM_NAME;
import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.CREATE_AS_WORKFLOW_NAME;
import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.DELETE_AS_WORKFLOW_NAME;
import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.INSTANTIATE_AS_WORKFLOW_NAME;
import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.TERMINATE_AS_WORKFLOW_NAME;
import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.ERROR;
import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.FINISHED;
import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.FINISHED_WITH_ERROR;
import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.IN_PROGRESS;
import static org.slf4j.LoggerFactory.getLogger;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.onap.so.cnfm.lcm.bpmn.flows.GsonProvider;
import org.onap.so.cnfm.lcm.bpmn.flows.exceptions.AsRequestProcessingException;
import org.onap.so.cnfm.lcm.database.beans.AsInst;
import org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc;
import org.onap.so.cnfm.lcm.database.beans.AsLcmOpType;
import org.onap.so.cnfm.lcm.database.beans.Job;
import org.onap.so.cnfm.lcm.database.beans.JobAction;
import org.onap.so.cnfm.lcm.database.beans.JobStatusEnum;
import org.onap.so.cnfm.lcm.database.beans.OperationStateEnum;
import org.onap.so.cnfm.lcm.database.beans.State;
import org.onap.so.cnfm.lcm.database.service.DatabaseServiceProvider;
import org.onap.so.cnfm.lcm.model.AsInstance;
import org.onap.so.cnfm.lcm.model.CreateAsRequest;
import org.onap.so.cnfm.lcm.model.ErrorDetails;
import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
import org.onap.so.cnfm.lcm.model.TerminateAsRequest;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.google.gson.Gson;

/**
 * @author Waqas Ikram (waqas.ikram@est.tech)
 *
 */
@Service
public class JobExecutorService {

    private static final Logger logger = getLogger(JobExecutorService.class);

    private static final Set<JobStatusEnum> JOB_FINISHED_STATES = Set.of(FINISHED, ERROR, FINISHED_WITH_ERROR);

    private static final int SLEEP_TIME_IN_SECONDS = 5;

    @Value("${so-cnfm-lcm.requesttimeout.timeoutInSeconds:300}")
    private int timeOutInSeconds;

    private final DatabaseServiceProvider databaseServiceProvider;
    private final WorkflowExecutorService workflowExecutorService;
    private final WorkflowQueryService workflowQueryService;
    private final Gson gson;

    @Autowired
    public JobExecutorService(final DatabaseServiceProvider databaseServiceProvider,
            final WorkflowExecutorService workflowExecutorService, final WorkflowQueryService workflowQueryService,
            final GsonProvider gsonProvider) {
        this.databaseServiceProvider = databaseServiceProvider;
        this.workflowExecutorService = workflowExecutorService;
        this.workflowQueryService = workflowQueryService;
        this.gson = gsonProvider.getGson();
    }

    public AsInstance runCreateAsJob(final CreateAsRequest createAsRequest) {
        logger.info("Starting 'Create AS' workflow job for request:\n{}", createAsRequest);
        final Job newJob = new Job().startTime(LocalDateTime.now()).jobType("AS").jobAction(JobAction.CREATE)
                .resourceId(createAsRequest.getAsdId()).resourceName(createAsRequest.getAsInstanceName())
                .status(JobStatusEnum.STARTING);
        databaseServiceProvider.addJob(newJob);

        logger.info("New job created in database for CreateAs:\n{}", newJob);

        workflowExecutorService.executeWorkflow(newJob.getJobId(), CREATE_AS_WORKFLOW_NAME,
                getVariables(newJob.getJobId(), createAsRequest));

        final ImmutablePair<String, JobStatusEnum> immutablePair =
                waitForJobToFinish(newJob.getJobId(), JOB_FINISHED_STATES);

        if (immutablePair.getRight() == null) {
            final String message = "Failed to create AS for request: \n" + createAsRequest;
            logger.error(message);
            throw new AsRequestProcessingException(message);
        }
        final JobStatusEnum finalJobStatus = immutablePair.getRight();
        final String processInstanceId = immutablePair.getLeft();

        if (!FINISHED.equals(finalJobStatus)) {

            final Optional<ErrorDetails> optional = workflowQueryService.getErrorDetails(processInstanceId);
            if (optional.isPresent()) {
                final ErrorDetails errorDetails = optional.get();
                final String message =
                        "Failed to create AS for request: \n" + createAsRequest + " due to \n" + errorDetails;
                logger.error(message);
                throw new AsRequestProcessingException(message, errorDetails);
            }

            final String message = "Create AS request failed. Received unexpected Job Status: " + finalJobStatus
                    + " Create As request: \n" + createAsRequest;
            logger.error(message);
            throw new AsRequestProcessingException(message);
        }

        logger.debug("Will query for CreateAsResponse using processInstanceId:{}", processInstanceId);
        final Optional<AsInstance> optional = workflowQueryService.getCreateNsResponse(processInstanceId);
        if (optional.isEmpty()) {
            final String message =
                    "Unable to find CreateAsReponse in Camunda History for process instance: " + processInstanceId;
            logger.error(message);
            throw new AsRequestProcessingException(message);
        }
        return optional.get();
    }

    public String runInstantiateAsJob(final String asInstanceId, final InstantiateAsRequest instantiateAsRequest) {
        final Job newJob = new Job().startTime(LocalDateTime.now()).jobType("AS").jobAction(JobAction.INSTANTIATE)
                .resourceId(asInstanceId).status(JobStatusEnum.STARTING);
        databaseServiceProvider.addJob(newJob);
        logger.info("New job created in database for InstantiateAs :\n{}", newJob);

        final LocalDateTime currentDateTime = LocalDateTime.now();
        final AsLcmOpOcc newAsLcmOpOcc = new AsLcmOpOcc().id(asInstanceId).operation(AsLcmOpType.INSTANTIATE)
                .operationState(OperationStateEnum.PROCESSING).stateEnteredTime(currentDateTime)
                .startTime(currentDateTime).asInst(getAsInst(asInstanceId)).isAutoInvocation(false)
                .isCancelPending(false).operationParams(gson.toJson(instantiateAsRequest));
        databaseServiceProvider.addAsLcmOpOcc(newAsLcmOpOcc);
        logger.info("New AsLcmOpOcc created in database :\n{}", newAsLcmOpOcc);

        workflowExecutorService.executeWorkflow(newJob.getJobId(), INSTANTIATE_AS_WORKFLOW_NAME,
                getVariables(asInstanceId, newJob.getJobId(), newAsLcmOpOcc.getId(), instantiateAsRequest));

        final Set<JobStatusEnum> jobFinishedStates = Set.of(FINISHED, ERROR, FINISHED_WITH_ERROR, IN_PROGRESS);
        final ImmutablePair<String, JobStatusEnum> immutablePair =
                waitForJobToFinish(newJob.getJobId(), jobFinishedStates);

        if (immutablePair.getRight() == null) {
            final String message = "Failed to Instantiate AS for request: \n" + instantiateAsRequest;
            logger.error(message);
            throw new AsRequestProcessingException(message);
        }

        final JobStatusEnum finalJobStatus = immutablePair.getRight();
        if (IN_PROGRESS.equals(finalJobStatus) || FINISHED.equals(finalJobStatus)) {
            logger.info("Instantiation Job status: {}", finalJobStatus);
            return newAsLcmOpOcc.getId();
        }

        final String message = "Instantiate AS request failed. Received unexpected Job Status: " + finalJobStatus
                + " Instantiate AS request: \n" + instantiateAsRequest;
        logger.error(message);
        throw new AsRequestProcessingException(message);
    }

    public String runTerminateAsJob(final String asInstanceId, final TerminateAsRequest terminateAsRequest) {
        doInitialTerminateChecks(asInstanceId, terminateAsRequest);

        final Job newJob = new Job().startTime(LocalDateTime.now()).jobType("AS").jobAction(JobAction.TERMINATE)
                .resourceId(asInstanceId).status(JobStatusEnum.STARTING);
        databaseServiceProvider.addJob(newJob);
        logger.info("New job created in database for TerminateAs :\n{}", newJob);

        final LocalDateTime currentDateTime = LocalDateTime.now();
        final AsLcmOpOcc newAsLcmOpOcc = new AsLcmOpOcc().id(asInstanceId).operation(AsLcmOpType.TERMINATE)
                .operationState(OperationStateEnum.PROCESSING).stateEnteredTime(currentDateTime)
                .startTime(currentDateTime).asInst(getAsInst(asInstanceId)).isAutoInvocation(false)
                .isCancelPending(false).operationParams(gson.toJson(terminateAsRequest));
        databaseServiceProvider.addAsLcmOpOcc(newAsLcmOpOcc);
        logger.info("New AsLcmOpOcc created in database :\n{}", newAsLcmOpOcc);

        workflowExecutorService.executeWorkflow(newJob.getJobId(), TERMINATE_AS_WORKFLOW_NAME,
                getVariables(asInstanceId, newJob.getJobId(), newAsLcmOpOcc.getId(), terminateAsRequest));

        final Set<JobStatusEnum> jobFinishedStates = Set.of(FINISHED, ERROR, FINISHED_WITH_ERROR, IN_PROGRESS);
        final ImmutablePair<String, JobStatusEnum> immutablePair =
                waitForJobToFinish(newJob.getJobId(), jobFinishedStates);

        if (immutablePair.getRight() == null) {
            final String message =
                    "Failed to Terminate AS with id: " + asInstanceId + " for request: \n" + terminateAsRequest;
            logger.error(message);
            throw new AsRequestProcessingException(message);
        }

        final JobStatusEnum finalJobStatus = immutablePair.getRight();

        if (IN_PROGRESS.equals(finalJobStatus) || FINISHED.equals(finalJobStatus)) {
            logger.info("Termination Job status: {}", finalJobStatus);
            return newAsLcmOpOcc.getId();
        }

        final String message = "Terminate AS request failed. Received unexpected Job Status: " + finalJobStatus
                + " id: " + asInstanceId + " Terminate AS request: \n" + terminateAsRequest;
        logger.error(message);
        throw new AsRequestProcessingException(message);
    }

    public void runDeleteAsJob(final String asInstanceId) {
        final Job newJob = new Job().startTime(LocalDateTime.now()).jobType("AS").jobAction(JobAction.DELETE)
                .resourceId(asInstanceId).status(JobStatusEnum.STARTING);
        databaseServiceProvider.addJob(newJob);
        logger.info("New job created in database for DeleteAs :\n{}", newJob);

        workflowExecutorService.executeWorkflow(newJob.getJobId(), DELETE_AS_WORKFLOW_NAME,
                getVariables(asInstanceId, newJob.getJobId()));

        final ImmutablePair<String, JobStatusEnum> immutablePair =
                waitForJobToFinish(newJob.getJobId(), JOB_FINISHED_STATES);

        if (immutablePair.getRight() == null) {
            final String message = "Failed to Delete AS with id: " + asInstanceId;
            logger.error(message);
            throw new AsRequestProcessingException(message);
        }

        final JobStatusEnum finalJobStatus = immutablePair.getRight();
        final String processInstanceId = immutablePair.getLeft();

        logger.info("Delete Job status: {}", finalJobStatus);

        if (!FINISHED.equals(finalJobStatus)) {

            final Optional<ErrorDetails> optional = workflowQueryService.getErrorDetails(processInstanceId);
            if (optional.isPresent()) {
                final ErrorDetails errorDetails = optional.get();
                final String message = "Failed to Delete AS with id: " + asInstanceId + " due to \n" + errorDetails;
                logger.error(message);
                throw new AsRequestProcessingException(message, errorDetails);
            }

            final String message = "Delete AS request failed. Received unexpected Job Status: " + finalJobStatus
                    + " Delete AS with id: " + asInstanceId;
            logger.error(message);
            throw new AsRequestProcessingException(message);
        }

        logger.debug("Delete AS finished successfully ...");
    }

    private AsInst getAsInst(final String asInstId) {
        logger.info("Getting AsInst with nsInstId: {}", asInstId);
        final Optional<AsInst> optionalNfvoNsInst = databaseServiceProvider.getAsInst(asInstId);

        if (optionalNfvoNsInst.isEmpty()) {
            final String message = "No matching AS Instance for id: " + asInstId + " found in database.";
            throw new AsRequestProcessingException(message);
        }

        return optionalNfvoNsInst.get();
    }

    private ImmutablePair<String, JobStatusEnum> waitForJobToFinish(final String jobId,
            final Set<JobStatusEnum> jobFinishedStates) {
        try {
            final long startTimeInMillis = System.currentTimeMillis();
            final long timeOutTime = startTimeInMillis + TimeUnit.SECONDS.toMillis(timeOutInSeconds);

            logger.info("Will wait till {} for {} job to finish", Instant.ofEpochMilli(timeOutTime), jobId);
            JobStatusEnum currentJobStatus = null;
            while (timeOutTime > System.currentTimeMillis()) {

                final Optional<Job> optional = databaseServiceProvider.getRefreshedJob(jobId);

                if (optional.isEmpty()) {
                    logger.error("Unable to find Job using jobId: {}", jobId);
                    return ImmutablePair.nullPair();
                }

                final Job job = optional.get();
                currentJobStatus = job.getStatus();
                logger.debug("Received job status response: \n {}", job);
                if (jobFinishedStates.contains(currentJobStatus)) {
                    logger.info("Job finished \n {}", currentJobStatus);
                    return ImmutablePair.of(job.getProcessInstanceId(), currentJobStatus);
                }

                logger.info("Haven't received one of finish state {} yet, will try again in {} seconds",
                        jobFinishedStates, SLEEP_TIME_IN_SECONDS);
                TimeUnit.SECONDS.sleep(SLEEP_TIME_IN_SECONDS);

            }
            logger.warn("Timeout current job status: {}", currentJobStatus);
            return ImmutablePair.nullPair();
        } catch (final InterruptedException interruptedException) {
            Thread.currentThread().interrupt();
            logger.error("Sleep was interrupted", interruptedException);
            return ImmutablePair.nullPair();
        }
    }

    private void doInitialTerminateChecks(final String asInstanceId, final TerminateAsRequest terminateAsRequest) {
        final AsInst asInst = getAsInst(asInstanceId);
        if (isNotInstantiated(asInst)) {
            final String message = "TerminateAsRequest received: " + terminateAsRequest + " for asInstanceId: "
                    + asInstanceId + "\nUnable to terminate.  AS Instance is already in " + State.NOT_INSTANTIATED
                    + " state." + "\nThis method can only be used with an AS instance in the " + State.INSTANTIATED
                    + " state.";
            logger.error(message);
            throw new AsRequestProcessingException(message);
        }
    }

    private boolean isNotInstantiated(final AsInst asInst) {
        return State.NOT_INSTANTIATED.equals(asInst.getStatus());
    }

    private Map<String, Object> getVariables(final String jobId, final CreateAsRequest createAsRequest) {
        final Map<String, Object> variables = new HashMap<>();
        variables.put(JOB_ID_PARAM_NAME, jobId);
        variables.put(CREATE_AS_REQUEST_PARAM_NAME, createAsRequest);
        return variables;
    }

    private Map<String, Object> getVariables(final String asInstanceId, final String jobId, final String occId,
            final InstantiateAsRequest instantiateAsRequest) {
        final Map<String, Object> variables = new HashMap<>();
        variables.put(AS_INSTANCE_ID_PARAM_NAME, asInstanceId);
        variables.put(JOB_ID_PARAM_NAME, jobId);
        variables.put(OCC_ID_PARAM_NAME, occId);
        variables.put(INSTANTIATE_AS_REQUEST_PARAM_NAME, instantiateAsRequest);
        return variables;
    }

    private Map<String, Object> getVariables(final String asInstanceId, final String jobId, final String occId,
            final TerminateAsRequest terminateAsRequest) {
        final Map<String, Object> variables = new HashMap<>();
        variables.put(AS_INSTANCE_ID_PARAM_NAME, asInstanceId);
        variables.put(JOB_ID_PARAM_NAME, jobId);
        variables.put(OCC_ID_PARAM_NAME, occId);
        variables.put(TERMINATE_AS_REQUEST_PARAM_NAME, terminateAsRequest);
        return variables;
    }

    private Map<String, Object> getVariables(final String asInstanceId, final String jobId) {
        final Map<String, Object> variables = new HashMap<>();
        variables.put(AS_INSTANCE_ID_PARAM_NAME, asInstanceId);
        variables.put(JOB_ID_PARAM_NAME, jobId);
        return variables;
    }
}