blob: 98de930a7f13f21ef83283318fa7bfd89e04ca2b [file] [log] [blame]
Aharoni, Pavel (pa0916)8c70f072018-11-18 00:07:12 +02001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20package org.onap.osam.job.command.demo;
21
22import com.google.common.collect.ImmutableMap;
23import com.google.common.collect.Lists;
24import lombok.extern.slf4j.Slf4j;
25import org.onap.osam.job.command.WatchingCommand;
26import org.onap.osam.job.dao.job.JobStatus;
27import org.onap.osam.job.IJobCommand;
28import org.onap.osam.job.JobType;
29import org.onap.osam.job.NextCommand;
30import org.onap.osam.job.command.CommandBase;
31import org.onap.osam.job.impl.DummyAsyncRequest;
32import org.onap.osam.job.impl.JobFactory;
33import org.onap.osam.job.impl.JobSharedData;
34import org.onap.osam.job.impl.JobsDataAccessService;
35import org.springframework.beans.factory.annotation.Autowired;
36import org.springframework.beans.factory.config.ConfigurableBeanFactory;
37import org.springframework.context.annotation.Scope;
38import org.springframework.stereotype.Component;
39
40import java.util.List;
41import java.util.Map;
42import java.util.UUID;
43
44@Slf4j
45@Component
46@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
47
48/**
49 * In this example, Chassis job is successful and spawns off OLT job - without being dependent on it.
50 */
51
52public class ChassisCommand extends CommandBase implements IJobCommand {
53
54 private Boolean isSuccessful;
55
56 private Boolean isOltDependant;
57
58 @Autowired
59 protected JobsDataAccessService jobsDataAccessService;
60
61 @Autowired
62 protected JobFactory jobFactory;
63
64 public ChassisCommand(){}
65
66 @Override
67 public IJobCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
68 super.init(sharedData);
69 isSuccessful = (Boolean) commandData.get("isSuccessful");
70 isOltDependant = (Boolean) commandData.get("isOltDependant");
71 return this;
72 }
73
74 @Override
75 public Map<String, Object> getData() {
76 return ImmutableMap.of("isSuccessful", isSuccessful,
77 "isOltDependant", isOltDependant);
78 }
79
80 @Override
81 public NextCommand call() {
82 NextCommand nextCommand;
83 if (isSuccessful){
84 log.debug("ChassisCommand - it's your LUCKY day! :) ChassisCreation created, continuing to OLTCreation...");
85
86 //Adding an OLTCreation child job
87 final List<UUID> oltChildJobs = getOltChildJobs();
88
89 if (isOltDependant){
90 log.debug("ChassisCommand - OLT Dependent scenario. Pending to wait if OLT job succeeds before deciding if Chassis job succeeds.");
91 nextCommand = new NextCommand(JobStatus.PENDING, new WatchingCommand(getSharedData(), oltChildJobs, true));
92 } else {
93 log.debug("ChassisCommand - independent scenario. This job is completed, regardless of child OLT job.");
94 nextCommand = new NextCommand(JobStatus.COMPLETED);
95 }
96 } else {
97 log.debug("ChassisCommand - it's your UNLUCKY day! :( ChassisCreation creation failed, your bulk request is finished here.");
98 nextCommand = new NextCommand(JobStatus.FAILED);
99 }
100 return nextCommand;
101 }
102
103 private List<UUID> getOltChildJobs() {
104 log.debug("Spinning off OLT child job....");
105 Map<String, Object> dataForOLTChild = ImmutableMap.of();
106 return Lists.newArrayList(jobsDataAccessService.add(jobFactory.createChildJob(JobType.OLTCreation, JobStatus.CREATING, new DummyAsyncRequest(), getSharedData(), ImmutableMap.of())));
107 }
108}