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 java.time.Duration;
21 import java.time.Instant;
23 import org.json.JSONObject;
24 import org.onap.pnfsimulator.simulator.client.HttpClientAdapter;
25 import org.onap.pnfsimulator.simulator.client.HttpClientAdapterImpl;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import org.slf4j.Marker;
30 import org.slf4j.MarkerFactory;
32 public class Simulator extends Thread {
34 private static final Logger LOGGER = LoggerFactory.getLogger(Simulator.class);
35 private final Marker EXIT = MarkerFactory.getMarker("EXIT");
36 private Map<String, String> contextMap = MDC.getCopyOfContextMap();
37 private boolean isEndless;
38 private String vesUrl;
39 private HttpClientAdapter httpClient;
40 private JSONObject messageBody;
41 private Duration duration;
42 private Duration interval;
43 private Instant endTime;
45 private Simulator() {}
47 public static Builder builder() {
53 setMdcContextMap(contextMap);
54 LOGGER.info("Simulation started - duration: {}, interval: {}s", getDuration(), interval.getSeconds());
55 endTime = Instant.now().plus(duration);
56 while (isEndless || runningTimeNotExceeded()) {
58 LOGGER.info("Message to be sent:\n" + getMessage());
59 httpClient.send(messageBody.toString(), vesUrl);
60 Thread.sleep(interval.toMillis());
61 } catch (InterruptedException e) {
62 LOGGER.info("Simulation interrupted");
66 LOGGER.info(EXIT, "Simulation finished");
70 private void setMdcContextMap(Map<String, String> mdcContextMap) {
71 if (mdcContextMap != null)
72 MDC.setContextMap(mdcContextMap);
75 private String getMessage() {
76 return messageBody.toString(4);
79 private String getDuration() {
80 return isEndless() ? "infinity" : duration.getSeconds() + "s";
83 private boolean runningTimeNotExceeded() {
84 return Instant.now().isBefore(endTime);
87 public boolean isEndless() {
91 public long getRemainingTime() {
92 return Duration.between(Instant.now(), endTime).getSeconds();
95 public static class Builder {
97 private String vesUrl;
98 private HttpClientAdapter httpClient;
99 private JSONObject messageBody;
100 private Duration duration;
101 private Duration interval;
105 this.httpClient = new HttpClientAdapterImpl();
106 this.messageBody = new JSONObject();
107 this.duration = Duration.ZERO;
108 this.interval = Duration.ZERO;
111 public Builder withVesUrl(String vesUrl) {
112 this.vesUrl = vesUrl;
116 public Builder withCustomHttpClientAdapter(HttpClientAdapter httpClient) {
117 this.httpClient = httpClient;
121 public Builder withMessageBody(JSONObject messageBody) {
122 this.messageBody = messageBody;
126 public Builder withDuration(Duration duration) {
127 this.duration = duration;
132 public Builder withInterval(Duration interval) {
133 this.interval = interval;
137 public Simulator build() {
138 Simulator simulator = new Simulator();
139 simulator.vesUrl = this.vesUrl;
140 simulator.httpClient = this.httpClient;
141 simulator.messageBody = this.messageBody;
142 simulator.duration = this.duration;
143 simulator.interval = this.interval;
144 simulator.isEndless = duration.equals(Duration.ZERO);