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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  20 package org.onap.pnfsimulator.simulator.scheduler;
 
  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;
 
  31 import com.google.gson.JsonObject;
 
  33 import java.io.IOException;
 
  34 import java.net.MalformedURLException;
 
  35 import java.security.GeneralSecurityException;
 
  36 import java.util.List;
 
  37 import java.util.Optional;
 
  38 import java.util.stream.Collectors;
 
  39 import org.onap.pnfsimulator.simulator.KeywordsHandler;
 
  40 import org.onap.pnfsimulator.simulator.client.HttpClientAdapterImpl;
 
  41 import org.onap.pnfsimulator.simulator.client.utils.ssl.SSLAuthenticationHelper;
 
  42 import org.quartz.JobBuilder;
 
  43 import org.quartz.JobDataMap;
 
  44 import org.quartz.JobDetail;
 
  45 import org.quartz.JobExecutionContext;
 
  46 import org.quartz.JobKey;
 
  47 import org.quartz.Scheduler;
 
  48 import org.quartz.SchedulerException;
 
  49 import org.quartz.SimpleTrigger;
 
  50 import org.quartz.TriggerBuilder;
 
  51 import org.springframework.beans.factory.annotation.Autowired;
 
  52 import org.springframework.stereotype.Component;
 
  55 public class EventScheduler {
 
  58     private final Scheduler scheduler;
 
  59     private final KeywordsHandler keywordsHandler;
 
  60     private final SSLAuthenticationHelper SSLAuthenticationHelper;
 
  63     public EventScheduler(Scheduler scheduler, KeywordsHandler keywordsHandler, SSLAuthenticationHelper SSLAuthenticationHelper) {
 
  64         this.scheduler = scheduler;
 
  65         this.keywordsHandler = keywordsHandler;
 
  66         this.SSLAuthenticationHelper = SSLAuthenticationHelper;
 
  69     public String scheduleEvent(String vesUrl, Integer repeatInterval, Integer repeatCount,
 
  70         String templateName, String eventId, JsonObject body)
 
  71             throws SchedulerException, IOException, GeneralSecurityException {
 
  73         JobDetail jobDetail = createJobDetail(vesUrl, templateName, eventId, body);
 
  74         SimpleTrigger trigger = createTrigger(repeatInterval, repeatCount);
 
  76         scheduler.scheduleJob(jobDetail, trigger);
 
  77         return jobDetail.getKey().getName();
 
  80     public boolean cancelAllEvents() throws SchedulerException {
 
  81         List<JobKey> jobKeys = getActiveJobsKeys();
 
  82         return scheduler.deleteJobs(jobKeys);
 
  85     public boolean cancelEvent(String jobName) throws SchedulerException {
 
  86         Optional<JobKey> activeJobKey = getActiveJobsKeys().stream().filter(e -> e.getName().equals(jobName)).findFirst();
 
  87         return activeJobKey.isPresent() && scheduler.deleteJob(activeJobKey.get());
 
  90     private SimpleTrigger createTrigger(int interval, int repeatCount) {
 
  91         return TriggerBuilder.newTrigger()
 
  92             .withSchedule(simpleSchedule()
 
  93                 .withIntervalInSeconds(interval)
 
  94                 .withRepeatCount(repeatCount - 1))
 
  98     private JobDetail createJobDetail(String vesUrl, String templateName, String eventId, JsonObject body) throws IOException, GeneralSecurityException {
 
  99         JobDataMap jobDataMap = new JobDataMap();
 
 100         jobDataMap.put(TEMPLATE_NAME, templateName);
 
 101         jobDataMap.put(VES_URL, vesUrl);
 
 102         jobDataMap.put(EVENT_ID, eventId);
 
 103         jobDataMap.put(KEYWORDS_HANDLER, keywordsHandler);
 
 104         jobDataMap.put(BODY, body);
 
 105         jobDataMap.put(CLIENT_ADAPTER, new HttpClientAdapterImpl(vesUrl, SSLAuthenticationHelper));
 
 108             .newJob(EventJob.class)
 
 109             .withDescription(templateName)
 
 110             .usingJobData(jobDataMap)
 
 114     private List<JobKey> getActiveJobsKeys() throws SchedulerException {
 
 115         return scheduler.getCurrentlyExecutingJobs()
 
 117             .map(JobExecutionContext::getJobDetail)
 
 118             .map(JobDetail::getKey)
 
 119             .collect(Collectors.toList());