Remove outdated doc for A1 Adaptor
[integration.git] / test / mocks / masspnfsim / 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.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;
37 import org.slf4j.MDC;
38 import org.slf4j.Marker;
39 import org.slf4j.MarkerFactory;
40
41 public class Simulator extends Thread {
42
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;
60
61     private Simulator() {}
62
63     public static Builder builder() {
64         return new Builder();
65     }
66
67     @Override
68     public void run() {
69         setMdcContextMap(contextMap);
70         LOGGER.info("Simulation started - duration: {}, interval: {}s", getDuration(), interval.getSeconds());
71         endTime = Instant.now().plus(duration);
72         while (isEndless || runningTimeNotExceeded()) {
73             try {
74
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);
81
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);
87                 thrownException = e;
88                 return;
89             }
90         }
91         LOGGER.info(EXIT, "Simulation finished");
92         MDC.clear();
93     }
94
95     private void setMdcContextMap(Map<String, String> mdcContextMap) {
96         if (mdcContextMap != null)
97             MDC.setContextMap(mdcContextMap);
98     }
99
100     private String getMessage() {
101         return messageBody.toString(4);
102     }
103
104     private String getDuration() {
105         return isEndless() ? "infinity" : duration.getSeconds() + "s";
106     }
107
108     private boolean runningTimeNotExceeded() {
109         return Instant.now().isBefore(endTime);
110     }
111
112     public boolean isEndless() {
113         return isEndless;
114     }
115
116     public Exception getThrownException() {
117         return thrownException;
118     }
119
120     public long getRemainingTime() {
121         return Duration.between(Instant.now(), endTime).getSeconds();
122     }
123
124     public static class Builder {
125
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;
136
137         private Builder() {
138             this.vesUrl = "";
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();
144         }
145
146         public Builder withVesUrl(String vesUrl) {
147             this.vesUrl = vesUrl;
148             return this;
149         }
150
151         public Builder withCustomRestTemplateAdapter(RestTemplateAdapter httpClient) {
152             this.httpClient = httpClient;
153             return this;
154         }
155
156         /*public Builder withMessageBody(JSONObject messageBody) {
157             this.messageBody = messageBody;
158             return this;
159         }*/
160
161         public Builder withDuration(Duration duration) {
162             this.duration = duration;
163             return this;
164         }
165
166
167         public Builder withInterval(Duration interval) {
168             this.interval = interval;
169             return this;
170         }
171
172         public Builder withCommonEventHeaderParams(JSONObject commonEventHeaderParams) {
173             this.commonEventHeaderParams = commonEventHeaderParams;
174             return this;
175         }
176
177         public Builder withNotificationParams(Optional<JSONObject> notificationParams) {
178             this.notificationParams = notificationParams;
179             return this;
180         }
181
182         public Builder withPnfRegistrationParams(Optional<JSONObject> pnfRegistrationParams) {
183             this.pnfRegistrationParams = pnfRegistrationParams;
184             return this;
185         }
186
187         public Builder withXnfUrl(String xnfUrl) {
188             this.xnfUrl = xnfUrl;
189             return this;
190         }
191
192         public Builder withFileProvider(FileProvider fileProvider) {
193             this.fileProvider = fileProvider;
194             return this;
195         }
196
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);
210             return simulator;
211         }
212     }
213 }