7049055845dcdc6515f9f805dacaaa30cd88c65b
[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
21 package org.onap.pnfsimulator.simulator;
22
23 import com.google.common.base.Strings;
24 import com.google.gson.JsonObject;
25 import java.io.IOException;
26 import java.net.MalformedURLException;
27 import java.util.Optional;
28 import org.onap.pnfsimulator.event.EventData;
29 import org.onap.pnfsimulator.event.EventDataService;
30 import org.onap.pnfsimulator.rest.model.FullEvent;
31 import org.onap.pnfsimulator.rest.model.SimulatorParams;
32 import org.onap.pnfsimulator.rest.model.SimulatorRequest;
33 import org.onap.pnfsimulator.simulator.client.HttpClientAdapter;
34 import org.onap.pnfsimulator.simulator.client.HttpClientAdapterImpl;
35 import org.onap.pnfsimulator.simulator.scheduler.EventScheduler;
36 import org.onap.pnfsimulator.simulatorconfig.SimulatorConfig;
37 import org.onap.pnfsimulator.simulatorconfig.SimulatorConfigService;
38 import org.quartz.SchedulerException;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Service;
41
42 @Service
43 public class SimulatorService {
44
45     private final TemplatePatcher templatePatcher;
46     private final TemplateReader templateReader;
47     private final EventDataService eventDataService;
48     private final EventScheduler eventScheduler;
49     private SimulatorConfigService simulatorConfigService;
50     private static final JsonObject EMPTY_JSON_OBJECT = new JsonObject();
51
52     @Autowired
53     public SimulatorService(TemplatePatcher templatePatcher, TemplateReader templateReader,
54                             EventScheduler eventScheduler, EventDataService eventDataService,
55                             SimulatorConfigService simulatorConfigService) {
56         this.templatePatcher = templatePatcher;
57         this.templateReader = templateReader;
58         this.eventDataService = eventDataService;
59         this.eventScheduler = eventScheduler;
60         this.simulatorConfigService = simulatorConfigService;
61     }
62
63     public String triggerEvent(SimulatorRequest simulatorRequest) throws IOException, SchedulerException {
64         String templateName = simulatorRequest.getTemplateName();
65         SimulatorParams simulatorParams = simulatorRequest.getSimulatorParams();
66         JsonObject template = templateReader.readTemplate(templateName);
67         JsonObject input = Optional.ofNullable(simulatorRequest.getPatch()).orElse(new JsonObject());
68         JsonObject patchedJson = templatePatcher
69                 .mergeTemplateWithPatch(template, input);
70         JsonObject keywords = new JsonObject();
71
72         EventData eventData = eventDataService.persistEventData(template, patchedJson, input, keywords);
73
74         String targetVesUrl = getDefaultUrlIfNotProvided(simulatorParams.getVesServerUrl());
75         return eventScheduler
76                 .scheduleEvent(targetVesUrl, Optional.ofNullable(simulatorParams.getRepeatInterval()).orElse(1),
77                         Optional.ofNullable(simulatorParams.getRepeatCount()).orElse(1), simulatorRequest.getTemplateName(),
78                         eventData.getId(),
79                 patchedJson);
80     }
81
82     public void triggerOneTimeEvent(FullEvent event) throws MalformedURLException {
83         KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), id -> 1);
84         JsonObject withKeywordsSubstituted = keywordsHandler.substituteKeywords(event.getEvent(), "").getAsJsonObject();
85
86         HttpClientAdapter client = createHttpClientAdapter(event.getVesServerUrl());
87         eventDataService.persistEventData(EMPTY_JSON_OBJECT, withKeywordsSubstituted, event.getEvent(), EMPTY_JSON_OBJECT);
88
89         client.send(withKeywordsSubstituted.toString());
90     }
91
92     public SimulatorConfig getConfiguration() {
93         return simulatorConfigService.getConfiguration();
94     }
95
96     public SimulatorConfig updateConfiguration(SimulatorConfig newConfig) {
97         return simulatorConfigService.updateConfiguration(newConfig);
98     }
99
100     public boolean cancelAllEvents() throws SchedulerException {
101         return eventScheduler.cancelAllEvents();
102     }
103
104     public boolean cancelEvent(String jobName) throws SchedulerException {
105         return eventScheduler.cancelEvent(jobName);
106     }
107
108     HttpClientAdapter createHttpClientAdapter(String vesServerUrl) throws MalformedURLException {
109         String targetVesUrl = getDefaultUrlIfNotProvided(vesServerUrl);
110         return new HttpClientAdapterImpl(targetVesUrl);
111     }
112
113     private String getDefaultUrlIfNotProvided(String vesUrlSimulatorParam) {
114         return Strings.isNullOrEmpty(vesUrlSimulatorParam)
115                 ? simulatorConfigService.getConfiguration().getVesServerUrl().toString() : vesUrlSimulatorParam;
116     }
117 }