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