Merge "switch drools pdp image to new one"
[integration.git] / test / mocks / mass-pnf-sim / pnf-sim-lightweight / src / main / java / org / onap / pnfsimulator / simulator / Simulator.java
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 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;
25 import java.util.Map;
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;
36 import org.slf4j.MDC;
37 import org.slf4j.Marker;
38 import org.slf4j.MarkerFactory;
39
40 public class Simulator extends Thread {
41
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";
57
58     private Simulator() {}
59
60     public static Builder builder() {
61         return new Builder();
62     }
63
64     @Override
65     public void run() {
66         setMdcContextMap(contextMap);
67         LOGGER.info("Simulation started - duration: {}, interval: {}s", getDuration(), interval.getSeconds());
68         endTime = Instant.now().plus(duration);
69         while (isEndless || runningTimeNotExceeded()) {
70             try {
71                 List<String> fileList = FileProvider.getFiles();
72                 MessageProvider messageProvider = new MessageProvider();
73                 JSONValidator validator = new JSONValidator();
74
75                 messageBody = messageProvider.createMessage(this.commonEventHeaderParams, this.pnfRegistrationParams,
76                         this.notificationParams, fileList, this.xnfUrl);
77                 validator.validate(messageBody.toString(), DEFAULT_OUTPUT_SCHEMA_PATH);
78
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");
84                 return;
85             }
86         }
87         LOGGER.info(EXIT, "Simulation finished");
88         MDC.clear();
89     }
90
91     private void setMdcContextMap(Map<String, String> mdcContextMap) {
92         if (mdcContextMap != null)
93             MDC.setContextMap(mdcContextMap);
94     }
95
96     private String getMessage() {
97         return messageBody.toString(4);
98     }
99
100     private String getDuration() {
101         return isEndless() ? "infinity" : duration.getSeconds() + "s";
102     }
103
104     private boolean runningTimeNotExceeded() {
105         return Instant.now().isBefore(endTime);
106     }
107
108     public boolean isEndless() {
109         return isEndless;
110     }
111
112     public long getRemainingTime() {
113         return Duration.between(Instant.now(), endTime).getSeconds();
114     }
115
116     public static class Builder {
117
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;
127
128         private Builder() {
129             this.vesUrl = "";
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();
135         }
136
137         public Builder withVesUrl(String vesUrl) {
138             this.vesUrl = vesUrl;
139             return this;
140         }
141
142         public Builder withCustomHttpClientAdapter(HttpClientAdapter httpClient) {
143             this.httpClient = httpClient;
144             return this;
145         }
146
147         /*public Builder withMessageBody(JSONObject messageBody) {
148             this.messageBody = messageBody;
149             return this;
150         }*/
151
152         public Builder withDuration(Duration duration) {
153             this.duration = duration;
154             return this;
155         }
156
157
158         public Builder withInterval(Duration interval) {
159             this.interval = interval;
160             return this;
161         }
162
163         public Builder withCommonEventHeaderParams(JSONObject commonEventHeaderParams) {
164             this.commonEventHeaderParams = commonEventHeaderParams;
165             return this;
166         }
167
168         public Builder withNotificationParams(Optional<JSONObject> notificationParams) {
169             this.notificationParams = notificationParams;
170             return this;
171         }
172
173         public Builder withPnfRegistrationParams(Optional<JSONObject> pnfRegistrationParams) {
174             this.pnfRegistrationParams = pnfRegistrationParams;
175             return this;
176         }
177
178         public Builder withXnfUrl(String xnfUrl) {
179             this.xnfUrl = xnfUrl;
180             return this;
181         }
182
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);
195             return simulator;
196         }
197     }
198 }