aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/pnfsimulator/simulator/SimulatorService.java
blob: 218dbc8fe0991704dc08f69cf693ddc72f6e9c0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * ============LICENSE_START=======================================================
 * PNF-REGISTRATION-HANDLER
 * ================================================================================
 * Copyright (C) 2018 Nokia. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.pnfsimulator.simulator;

import com.google.common.base.Strings;
import com.google.gson.JsonObject;
import org.onap.pnfsimulator.event.EventData;
import org.onap.pnfsimulator.event.EventDataService;
import org.onap.pnfsimulator.rest.model.FullEvent;
import org.onap.pnfsimulator.rest.model.SimulatorParams;
import org.onap.pnfsimulator.rest.model.SimulatorRequest;
import org.onap.pnfsimulator.simulator.client.HttpClientAdapter;
import org.onap.pnfsimulator.simulator.client.HttpClientAdapterImpl;
import org.onap.pnfsimulator.simulator.client.HttpResponseAdapter;
import org.onap.pnfsimulator.simulator.client.utils.ssl.SslAuthenticationHelper;
import org.onap.pnfsimulator.simulator.scheduler.EventScheduler;
import org.onap.pnfsimulator.simulatorconfig.SimulatorConfig;
import org.onap.pnfsimulator.simulatorconfig.SimulatorConfigService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Optional;

@Service
public class SimulatorService {

    private final TemplatePatcher templatePatcher;
    private final TemplateVariablesReplacer templateVariablesReplacer;
    private final TemplateReader templateReader;
    private final EventDataService eventDataService;
    private final EventScheduler eventScheduler;
    private final SslAuthenticationHelper sslAuthenticationHelper;
    private SimulatorConfigService simulatorConfigService;
    private static final JsonObject EMPTY_JSON_OBJECT = new JsonObject();

    @Autowired
    public SimulatorService(
        TemplatePatcher templatePatcher,
        TemplateReader templateReader,
        EventScheduler eventScheduler,
        EventDataService eventDataService,
        SimulatorConfigService simulatorConfigService,
        TemplateVariablesReplacer templateVariablesReplacer,
        SslAuthenticationHelper sslAuthenticationHelper) {
        this.templatePatcher = templatePatcher;
        this.templateReader = templateReader;
        this.eventDataService = eventDataService;
        this.eventScheduler = eventScheduler;
        this.simulatorConfigService = simulatorConfigService;
        this.templateVariablesReplacer = templateVariablesReplacer;
        this.sslAuthenticationHelper = sslAuthenticationHelper;
    }

    public String triggerEvent(SimulatorRequest simulatorRequest) throws IOException, SchedulerException, GeneralSecurityException {
        String templateName = simulatorRequest.getTemplateName();
        SimulatorParams simulatorParams = simulatorRequest.getSimulatorParams();
        JsonObject template = templateReader.readTemplate(templateName);
        JsonObject input = Optional.ofNullable(simulatorRequest.getPatch()).orElse(new JsonObject());
        JsonObject patchedJson = templatePatcher
                .mergeTemplateWithPatch(template, input);
        JsonObject variables = Optional.ofNullable(simulatorRequest.getVariables()).orElse(new JsonObject());
        JsonObject patchedJsonWithVariablesSubstituted = templateVariablesReplacer.substituteVariables(patchedJson, variables);

        JsonObject keywords = new JsonObject();

        EventData eventData = eventDataService.persistEventData(template, patchedJsonWithVariablesSubstituted, input, keywords);

        String targetVesUrl = getDefaultUrlIfNotProvided(simulatorParams.getVesServerUrl());
        return eventScheduler
                .scheduleEvent(targetVesUrl, Optional.ofNullable(simulatorParams.getRepeatInterval()).orElse(1),
                        Optional.ofNullable(simulatorParams.getRepeatCount()).orElse(1), simulatorRequest.getTemplateName(),
                        eventData.getId(),
                    patchedJsonWithVariablesSubstituted);
    }

    public HttpResponseAdapter triggerOneTimeEvent(FullEvent event) throws IOException, GeneralSecurityException {
        KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), id -> 1);
        JsonObject withKeywordsSubstituted = keywordsHandler.substituteKeywords(event.getEvent(), "").getAsJsonObject();

        HttpClientAdapter client = createHttpClientAdapter(event.getVesServerUrl());
        eventDataService.persistEventData(EMPTY_JSON_OBJECT, withKeywordsSubstituted, event.getEvent(), EMPTY_JSON_OBJECT);

        return client.send(withKeywordsSubstituted.toString());
    }

    public SimulatorConfig getConfiguration() {
        return simulatorConfigService.getConfiguration();
    }

    public SimulatorConfig updateConfiguration(SimulatorConfig newConfig) {
        return simulatorConfigService.updateConfiguration(newConfig);
    }

    public boolean cancelAllEvents() throws SchedulerException {
        return eventScheduler.cancelAllEvents();
    }

    public boolean cancelEvent(String jobName) throws SchedulerException {
        return eventScheduler.cancelEvent(jobName);
    }

    HttpClientAdapter createHttpClientAdapter(String vesServerUrl) throws IOException, GeneralSecurityException {
        String targetVesUrl = getDefaultUrlIfNotProvided(vesServerUrl);
        return new HttpClientAdapterImpl(targetVesUrl, sslAuthenticationHelper);
    }

    private String getDefaultUrlIfNotProvided(String vesUrlSimulatorParam) {
        return Strings.isNullOrEmpty(vesUrlSimulatorParam)
                ? simulatorConfigService.getConfiguration().getVesServerUrl().toString() : vesUrlSimulatorParam;
    }
}