1b1746fa5f5b61e346b8c4f7043a69b9fafe8592
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
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  */
20 package org.onap.pnfsimulator.simulator.scheduler;
21
22
23 import static org.onap.pnfsimulator.simulator.scheduler.EventJob.BODY;
24 import static org.onap.pnfsimulator.simulator.scheduler.EventJob.CLIENT_ADAPTER;
25 import static org.onap.pnfsimulator.simulator.scheduler.EventJob.EVENT_ID;
26 import static org.onap.pnfsimulator.simulator.scheduler.EventJob.KEYWORDS_HANDLER;
27 import static org.onap.pnfsimulator.simulator.scheduler.EventJob.TEMPLATE_NAME;
28 import static org.onap.pnfsimulator.simulator.scheduler.EventJob.VES_URL;
29 import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
30
31 import com.google.gson.JsonObject;
32
33 import java.net.MalformedURLException;
34 import java.util.List;
35 import java.util.Optional;
36 import java.util.stream.Collectors;
37 import org.onap.pnfsimulator.simulator.KeywordsHandler;
38 import org.onap.pnfsimulator.simulator.client.HttpClientAdapterImpl;
39 import org.quartz.JobBuilder;
40 import org.quartz.JobDataMap;
41 import org.quartz.JobDetail;
42 import org.quartz.JobExecutionContext;
43 import org.quartz.JobKey;
44 import org.quartz.Scheduler;
45 import org.quartz.SchedulerException;
46 import org.quartz.SimpleTrigger;
47 import org.quartz.TriggerBuilder;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.stereotype.Component;
50
51 @Component
52 public class EventScheduler {
53
54
55     private final Scheduler scheduler;
56     private final KeywordsHandler keywordsHandler;
57
58     @Autowired
59     public EventScheduler(Scheduler scheduler, KeywordsHandler keywordsHandler) {
60         this.scheduler = scheduler;
61         this.keywordsHandler = keywordsHandler;
62     }
63
64     public String scheduleEvent(String vesUrl, Integer repeatInterval, Integer repeatCount,
65         String templateName, String eventId, JsonObject body)
66             throws SchedulerException, MalformedURLException {
67
68         JobDetail jobDetail = createJobDetail(vesUrl, templateName, eventId, body);
69         SimpleTrigger trigger = createTrigger(repeatInterval, repeatCount);
70
71         scheduler.scheduleJob(jobDetail, trigger);
72         return jobDetail.getKey().getName();
73     }
74
75     public boolean cancelAllEvents() throws SchedulerException {
76         List<JobKey> jobKeys = getActiveJobsKeys();
77         return scheduler.deleteJobs(jobKeys);
78     }
79
80     public boolean cancelEvent(String jobName) throws SchedulerException {
81         Optional<JobKey> activeJobKey = getActiveJobsKeys().stream().filter(e -> e.getName().equals(jobName)).findFirst();
82         return activeJobKey.isPresent() && scheduler.deleteJob(activeJobKey.get());
83     }
84
85     private SimpleTrigger createTrigger(int interval, int repeatCount) {
86         return TriggerBuilder.newTrigger()
87             .withSchedule(simpleSchedule()
88                 .withIntervalInSeconds(interval)
89                 .withRepeatCount(repeatCount - 1))
90             .build();
91     }
92
93     private JobDetail createJobDetail(String vesUrl, String templateName, String eventId, JsonObject body) throws MalformedURLException {
94         JobDataMap jobDataMap = new JobDataMap();
95         jobDataMap.put(TEMPLATE_NAME, templateName);
96         jobDataMap.put(VES_URL, vesUrl);
97         jobDataMap.put(EVENT_ID, eventId);
98         jobDataMap.put(KEYWORDS_HANDLER, keywordsHandler);
99         jobDataMap.put(BODY, body);
100         jobDataMap.put(CLIENT_ADAPTER, new HttpClientAdapterImpl(vesUrl));
101
102         return JobBuilder
103             .newJob(EventJob.class)
104             .withDescription(templateName)
105             .usingJobData(jobDataMap)
106             .build();
107     }
108
109     private List<JobKey> getActiveJobsKeys() throws SchedulerException {
110         return scheduler.getCurrentlyExecutingJobs()
111             .stream()
112             .map(JobExecutionContext::getJobDetail)
113             .map(JobDetail::getKey)
114             .collect(Collectors.toList());
115     }
116 }