9b4725fd1ce434f3bf5fbc1f62297de9193b97f7
[integration.git] /
1 /*
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
16  */
17
18 package org.onap.pnfsimulator.simulator;
19
20 import java.time.Duration;
21 import java.time.Instant;
22 import java.util.Map;
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;
28 import org.slf4j.MDC;
29 import org.slf4j.Marker;
30 import org.slf4j.MarkerFactory;
31
32 public class Simulator extends Thread {
33
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;
44
45     private Simulator() {}
46
47     public static Builder builder() {
48         return new Builder();
49     }
50
51     @Override
52     public void run() {
53         setMdcContextMap(contextMap);
54         LOGGER.info("Simulation started - duration: {}, interval: {}s", getDuration(), interval.getSeconds());
55         endTime = Instant.now().plus(duration);
56         while (isEndless || runningTimeNotExceeded()) {
57             try {
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");
63                 return;
64             }
65         }
66         LOGGER.info(EXIT, "Simulation finished");
67         MDC.clear();
68     }
69
70     private void setMdcContextMap(Map<String, String> mdcContextMap) {
71         if (mdcContextMap != null)
72             MDC.setContextMap(mdcContextMap);
73     }
74
75     private String getMessage() {
76         return messageBody.toString(4);
77     }
78
79     private String getDuration() {
80         return isEndless() ? "infinity" : duration.getSeconds() + "s";
81     }
82
83     private boolean runningTimeNotExceeded() {
84         return Instant.now().isBefore(endTime);
85     }
86
87     public boolean isEndless() {
88         return isEndless;
89     }
90
91     public long getRemainingTime() {
92         return Duration.between(Instant.now(), endTime).getSeconds();
93     }
94
95     public static class Builder {
96
97         private String vesUrl;
98         private HttpClientAdapter httpClient;
99         private JSONObject messageBody;
100         private Duration duration;
101         private Duration interval;
102
103         private Builder() {
104             this.vesUrl = "";
105             this.httpClient = new HttpClientAdapterImpl();
106             this.messageBody = new JSONObject();
107             this.duration = Duration.ZERO;
108             this.interval = Duration.ZERO;
109         }
110
111         public Builder withVesUrl(String vesUrl) {
112             this.vesUrl = vesUrl;
113             return this;
114         }
115
116         public Builder withCustomHttpClientAdapter(HttpClientAdapter httpClient) {
117             this.httpClient = httpClient;
118             return this;
119         }
120
121         public Builder withMessageBody(JSONObject messageBody) {
122             this.messageBody = messageBody;
123             return this;
124         }
125
126         public Builder withDuration(Duration duration) {
127             this.duration = duration;
128             return this;
129         }
130
131
132         public Builder withInterval(Duration interval) {
133             this.interval = interval;
134             return this;
135         }
136
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);
145             return simulator;
146         }
147     }
148 }