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.HttpClientAdapter;
31 import org.onap.pnfsimulator.simulator.client.HttpClientAdapterImpl;
32 import org.onap.pnfsimulator.simulator.validation.JSONValidator;
33 import org.onap.pnfsimulator.simulator.validation.ValidationException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import org.slf4j.Marker;
38 import org.slf4j.MarkerFactory;
40 public class Simulator extends Thread {
42 private static final Logger LOGGER = LoggerFactory.getLogger(Simulator.class);
43 private final Marker EXIT = MarkerFactory.getMarker("EXIT");
44 private Map<String, String> contextMap = MDC.getCopyOfContextMap();
45 private boolean isEndless;
46 private String vesUrl;
47 private HttpClientAdapter httpClient;
48 private JSONObject messageBody;
49 private Duration duration;
50 private Duration interval;
51 private Instant endTime;
52 private JSONObject commonEventHeaderParams;
53 private Optional<JSONObject> pnfRegistrationParams;
54 private Optional<JSONObject> notificationParams;
55 private String xnfUrl;
56 private static final String DEFAULT_OUTPUT_SCHEMA_PATH = "json_schema/output_validator_ves_schema_30.0.1.json";
58 private Simulator() {}
60 public static Builder builder() {
66 setMdcContextMap(contextMap);
67 LOGGER.info("Simulation started - duration: {}, interval: {}s", getDuration(), interval.getSeconds());
68 endTime = Instant.now().plus(duration);
69 while (isEndless || runningTimeNotExceeded()) {
71 List<String> fileList = FileProvider.getFiles();
72 MessageProvider messageProvider = new MessageProvider();
73 JSONValidator validator = new JSONValidator();
75 messageBody = messageProvider.createMessage(this.commonEventHeaderParams, this.pnfRegistrationParams,
76 this.notificationParams, fileList, this.xnfUrl);
77 validator.validate(messageBody.toString(), DEFAULT_OUTPUT_SCHEMA_PATH);
79 LOGGER.info("Message to be sent:\n" + getMessage());
80 httpClient.send(messageBody.toString(), vesUrl);
81 Thread.sleep(interval.toMillis());
82 } catch (InterruptedException | ValidationException | ProcessingException | IOException e) {
83 LOGGER.info("Simulation stopped due to an exception");
87 LOGGER.info(EXIT, "Simulation finished");
91 private void setMdcContextMap(Map<String, String> mdcContextMap) {
92 if (mdcContextMap != null)
93 MDC.setContextMap(mdcContextMap);
96 private String getMessage() {
97 return messageBody.toString(4);
100 private String getDuration() {
101 return isEndless() ? "infinity" : duration.getSeconds() + "s";
104 private boolean runningTimeNotExceeded() {
105 return Instant.now().isBefore(endTime);
108 public boolean isEndless() {
112 public long getRemainingTime() {
113 return Duration.between(Instant.now(), endTime).getSeconds();
116 public static class Builder {
118 private String vesUrl;
119 private HttpClientAdapter httpClient;
120 //private JSONObject messageBody;
121 private Duration duration;
122 private Duration interval;
123 private Optional<JSONObject> notificationParams;
124 private Optional<JSONObject> pnfRegistrationParams;
125 private JSONObject commonEventHeaderParams;
126 private String xnfUrl;
130 this.httpClient = new HttpClientAdapterImpl();
131 //this.messageBody = new JSONObject();
132 this.duration = Duration.ZERO;
133 this.interval = Duration.ZERO;
134 this.commonEventHeaderParams = new JSONObject();
137 public Builder withVesUrl(String vesUrl) {
138 this.vesUrl = vesUrl;
142 public Builder withCustomHttpClientAdapter(HttpClientAdapter httpClient) {
143 this.httpClient = httpClient;
147 /*public Builder withMessageBody(JSONObject messageBody) {
148 this.messageBody = messageBody;
152 public Builder withDuration(Duration duration) {
153 this.duration = duration;
158 public Builder withInterval(Duration interval) {
159 this.interval = interval;
163 public Builder withCommonEventHeaderParams(JSONObject commonEventHeaderParams) {
164 this.commonEventHeaderParams = commonEventHeaderParams;
168 public Builder withNotificationParams(Optional<JSONObject> notificationParams) {
169 this.notificationParams = notificationParams;
173 public Builder withPnfRegistrationParams(Optional<JSONObject> pnfRegistrationParams) {
174 this.pnfRegistrationParams = pnfRegistrationParams;
178 public Builder withXnfUrl(String xnfUrl) {
179 this.xnfUrl = xnfUrl;
183 public Simulator build() {
184 Simulator simulator = new Simulator();
185 simulator.vesUrl = this.vesUrl;
186 simulator.httpClient = this.httpClient;
187 //simulator.messageBody = this.messageBody;
188 simulator.duration = this.duration;
189 simulator.interval = this.interval;
190 simulator.xnfUrl = this.xnfUrl;
191 simulator.commonEventHeaderParams = this.commonEventHeaderParams;
192 simulator.pnfRegistrationParams = this.pnfRegistrationParams;
193 simulator.notificationParams = this.notificationParams;
194 simulator.isEndless = duration.equals(Duration.ZERO);