2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================ Copyright (C)
5 * 2018 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================ Licensed under
7 * the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
15 * the License. ============LICENSE_END=========================================================
18 package org.onap.pnfsimulator.simulator;
20 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
21 import java.io.IOException;
22 import java.time.Duration;
23 import java.time.Instant;
24 import java.util.List;
26 import java.util.Optional;
27 import org.json.JSONObject;
28 import org.onap.pnfsimulator.FileProvider;
29 import org.onap.pnfsimulator.message.MessageProvider;
30 import org.onap.pnfsimulator.simulator.client.RestTemplateAdapter;
31 import org.onap.pnfsimulator.simulator.client.RestTemplateAdapterImpl;
32 import org.onap.pnfsimulator.simulator.validation.JSONValidator;
33 import org.onap.pnfsimulator.simulator.validation.NoRopFilesException;
34 import org.onap.pnfsimulator.simulator.validation.ValidationException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import org.slf4j.Marker;
39 import org.slf4j.MarkerFactory;
41 public class Simulator extends Thread {
43 private static final Logger LOGGER = LoggerFactory.getLogger(Simulator.class);
44 private final Marker EXIT = MarkerFactory.getMarker("EXIT");
45 private Map<String, String> contextMap = MDC.getCopyOfContextMap();
46 private boolean isEndless;
47 private String vesUrl;
48 private RestTemplateAdapter httpClient;
49 private JSONObject messageBody;
50 private Duration duration;
51 private Duration interval;
52 private Instant endTime;
53 private JSONObject commonEventHeaderParams;
54 private Optional<JSONObject> pnfRegistrationParams;
55 private Optional<JSONObject> notificationParams;
56 private String xnfUrl;
57 private static final String DEFAULT_OUTPUT_SCHEMA_PATH = "json_schema/output_validator_ves_schema_30.0.1.json";
58 private FileProvider fileProvider;
59 private Exception thrownException = null;
61 private Simulator() {}
63 public static Builder builder() {
69 setMdcContextMap(contextMap);
70 LOGGER.info("Simulation started - duration: {}, interval: {}s", getDuration(), interval.getSeconds());
71 endTime = Instant.now().plus(duration);
72 while (isEndless || runningTimeNotExceeded()) {
75 List<String> fileList = fileProvider.getFiles();
76 MessageProvider messageProvider = new MessageProvider();
77 JSONValidator validator = new JSONValidator();
78 messageBody = messageProvider.createMessage(this.commonEventHeaderParams, this.pnfRegistrationParams,
79 this.notificationParams, fileList, this.xnfUrl);
80 validator.validate(messageBody.toString(), DEFAULT_OUTPUT_SCHEMA_PATH);
82 LOGGER.info("Message to be sent:\n" + getMessage());
83 httpClient.send(messageBody.toString(), vesUrl);
84 Thread.sleep(interval.toMillis());
85 } catch (InterruptedException | ValidationException | ProcessingException | IOException | NoRopFilesException e) {
86 LOGGER.info("Simulation stopped due to an exception: " + e);
91 LOGGER.info(EXIT, "Simulation finished");
95 private void setMdcContextMap(Map<String, String> mdcContextMap) {
96 if (mdcContextMap != null)
97 MDC.setContextMap(mdcContextMap);
100 private String getMessage() {
101 return messageBody.toString(4);
104 private String getDuration() {
105 return isEndless() ? "infinity" : duration.getSeconds() + "s";
108 private boolean runningTimeNotExceeded() {
109 return Instant.now().isBefore(endTime);
112 public boolean isEndless() {
116 public Exception getThrownException() {
117 return thrownException;
120 public long getRemainingTime() {
121 return Duration.between(Instant.now(), endTime).getSeconds();
124 public static class Builder {
126 private String vesUrl;
127 private RestTemplateAdapter httpClient;
128 //private JSONObject messageBody;
129 private Duration duration;
130 private Duration interval;
131 private Optional<JSONObject> notificationParams;
132 private Optional<JSONObject> pnfRegistrationParams;
133 private JSONObject commonEventHeaderParams;
134 private String xnfUrl;
135 private FileProvider fileProvider;
139 this.httpClient = new RestTemplateAdapterImpl();
140 //this.messageBody = new JSONObject();
141 this.duration = Duration.ZERO;
142 this.interval = Duration.ZERO;
143 this.commonEventHeaderParams = new JSONObject();
146 public Builder withVesUrl(String vesUrl) {
147 this.vesUrl = vesUrl;
151 public Builder withCustomRestTemplateAdapter(RestTemplateAdapter httpClient) {
152 this.httpClient = httpClient;
156 /*public Builder withMessageBody(JSONObject messageBody) {
157 this.messageBody = messageBody;
161 public Builder withDuration(Duration duration) {
162 this.duration = duration;
167 public Builder withInterval(Duration interval) {
168 this.interval = interval;
172 public Builder withCommonEventHeaderParams(JSONObject commonEventHeaderParams) {
173 this.commonEventHeaderParams = commonEventHeaderParams;
177 public Builder withNotificationParams(Optional<JSONObject> notificationParams) {
178 this.notificationParams = notificationParams;
182 public Builder withPnfRegistrationParams(Optional<JSONObject> pnfRegistrationParams) {
183 this.pnfRegistrationParams = pnfRegistrationParams;
187 public Builder withXnfUrl(String xnfUrl) {
188 this.xnfUrl = xnfUrl;
192 public Builder withFileProvider(FileProvider fileProvider) {
193 this.fileProvider = fileProvider;
197 public Simulator build() {
198 Simulator simulator = new Simulator();
199 simulator.vesUrl = this.vesUrl;
200 simulator.httpClient = this.httpClient;
201 //simulator.messageBody = this.messageBody;
202 simulator.duration = this.duration;
203 simulator.interval = this.interval;
204 simulator.xnfUrl = this.xnfUrl;
205 simulator.fileProvider = this.fileProvider;
206 simulator.commonEventHeaderParams = this.commonEventHeaderParams;
207 simulator.pnfRegistrationParams = this.pnfRegistrationParams;
208 simulator.notificationParams = this.notificationParams;
209 simulator.isEndless = duration.equals(Duration.ZERO);