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.rest;
20 import static org.onap.pnfsimulator.logging.MDCVariables.INSTANCE_UUID;
21 import static org.onap.pnfsimulator.logging.MDCVariables.INVOCATION_ID;
22 import static org.onap.pnfsimulator.logging.MDCVariables.REQUEST_ID;
23 import static org.onap.pnfsimulator.logging.MDCVariables.RESPONSE_CODE;
24 import static org.onap.pnfsimulator.logging.MDCVariables.SERVICE_NAME;
25 import static org.onap.pnfsimulator.logging.MDCVariables.X_INVOCATION_ID;
26 import static org.onap.pnfsimulator.logging.MDCVariables.X_ONAP_REQUEST_ID;
27 import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER_PARAMS;
28 import static org.onap.pnfsimulator.message.MessageConstants.SIMULATOR_PARAMS;
29 import static org.onap.pnfsimulator.rest.util.ResponseBuilder.MESSAGE;
30 import static org.onap.pnfsimulator.rest.util.ResponseBuilder.REMAINING_TIME;
31 import static org.onap.pnfsimulator.rest.util.ResponseBuilder.SIMULATOR_STATUS;
32 import static org.onap.pnfsimulator.rest.util.ResponseBuilder.TIMESTAMP;
33 import static org.springframework.http.HttpStatus.BAD_REQUEST;
34 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
35 import static org.springframework.http.HttpStatus.OK;
36 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
37 import java.io.IOException;
38 import java.text.DateFormat;
39 import java.text.SimpleDateFormat;
40 import java.util.Optional;
41 import java.util.UUID;
42 import org.json.JSONException;
43 import org.json.JSONObject;
44 import org.onap.pnfsimulator.message.MessageConstants;
45 import org.onap.pnfsimulator.rest.util.DateUtil;
46 import org.onap.pnfsimulator.rest.util.ResponseBuilder;
47 import org.onap.pnfsimulator.simulator.Simulator;
48 import org.onap.pnfsimulator.simulator.SimulatorFactory;
49 import org.onap.pnfsimulator.simulator.validation.JSONValidator;
50 import org.onap.pnfsimulator.simulator.validation.ValidationException;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import org.slf4j.Marker;
55 import org.slf4j.MarkerFactory;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.http.HttpHeaders;
58 import org.springframework.http.ResponseEntity;
59 import org.springframework.web.bind.annotation.GetMapping;
60 import org.springframework.web.bind.annotation.PostMapping;
61 import org.springframework.web.bind.annotation.RequestBody;
62 import org.springframework.web.bind.annotation.RequestHeader;
63 import org.springframework.web.bind.annotation.RequestMapping;
64 import org.springframework.web.bind.annotation.RestController;
67 @RequestMapping("/simulator")
68 public class SimulatorController {
70 private static final Logger LOGGER = LoggerFactory.getLogger(Simulator.class);
71 private static final DateFormat RESPONSE_DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss,SSS");
72 private final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
73 private Simulator simulator;
74 private JSONValidator validator;
75 private SimulatorFactory factory;
78 public SimulatorController(JSONValidator validator, SimulatorFactory factory) {
79 this.validator = validator;
80 this.factory = factory;
84 public ResponseEntity start(@RequestHeader HttpHeaders headers, @RequestBody String message) {
85 MDC.put(REQUEST_ID, headers.getFirst(X_ONAP_REQUEST_ID));
86 MDC.put(INVOCATION_ID, headers.getFirst(X_INVOCATION_ID));
87 MDC.put(INSTANCE_UUID, UUID.randomUUID().toString());
88 MDC.put(SERVICE_NAME, "/simulator/start");
89 LOGGER.info(ENTRY, "Simulator starting");
91 if (isSimulatorRunning()) {
92 MDC.put(RESPONSE_CODE, BAD_REQUEST.toString());
93 return ResponseBuilder.status(BAD_REQUEST).put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
94 .put(MESSAGE, "Cannot start simulator since it's already running").build();
98 validator.validate(message, "json_schema/input_validator.json");
99 JSONObject root = new JSONObject(message);
100 JSONObject simulatorParams = root.getJSONObject(SIMULATOR_PARAMS);
101 JSONObject commonEventHeaderParams = root.getJSONObject(COMMON_EVENT_HEADER_PARAMS);
102 Optional<JSONObject> pnfRegistrationFields = root.has(MessageConstants.PNF_REGISTRATION_PARAMS)
103 ? Optional.of(root.getJSONObject(MessageConstants.PNF_REGISTRATION_PARAMS))
105 Optional<JSONObject> notificationFields = root.has(MessageConstants.NOTIFICATION_PARAMS)
106 ? Optional.of(root.getJSONObject(MessageConstants.NOTIFICATION_PARAMS))
109 factory.create(simulatorParams, commonEventHeaderParams, pnfRegistrationFields, notificationFields);
112 MDC.put(RESPONSE_CODE, OK.toString());
113 return ResponseBuilder.status(OK).put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
114 .put(MESSAGE, "Simulator started").build();
116 } catch (JSONException e) {
117 MDC.put(RESPONSE_CODE, BAD_REQUEST.toString());
118 LOGGER.warn("Cannot start simulator, invalid json format: {}", e.getMessage());
119 LOGGER.debug("Received json has invalid format", e);
120 return ResponseBuilder.status(BAD_REQUEST).put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
121 .put(MESSAGE, "Cannot start simulator, invalid json format").build();
123 } catch (ProcessingException | ValidationException | IOException e) {
124 MDC.put(RESPONSE_CODE, BAD_REQUEST.toString());
125 LOGGER.warn("Json validation failed: {}", e.getMessage());
126 return ResponseBuilder.status(BAD_REQUEST).put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
127 .put(MESSAGE, "Cannot start simulator - Json format is not compatible with schema definitions")
130 } catch (Exception e) {
131 MDC.put(RESPONSE_CODE, INTERNAL_SERVER_ERROR.toString());
132 LOGGER.error("Cannot start simulator - unexpected exception", e);
133 return ResponseBuilder.status(INTERNAL_SERVER_ERROR)
134 .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
135 .put(MESSAGE, "Unexpected exception: " + e.getMessage()).build();
141 @PostMapping("startmassmode")
142 public ResponseEntity startmassmode(@RequestHeader HttpHeaders headers, @RequestBody String message) {
143 MDC.put(REQUEST_ID, headers.getFirst(X_ONAP_REQUEST_ID));
144 MDC.put(INVOCATION_ID, headers.getFirst(X_INVOCATION_ID));
145 MDC.put(INSTANCE_UUID, UUID.randomUUID().toString());
146 MDC.put(SERVICE_NAME, "/simulator/start");
147 LOGGER.info(ENTRY, "Simulator starting");
149 if (isSimulatorRunning()) {
150 MDC.put(RESPONSE_CODE, BAD_REQUEST.toString());
151 return ResponseBuilder.status(BAD_REQUEST).put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
152 .put(MESSAGE, "Cannot start simulator since it's already running").build();
156 validator.validate(message, "json_schema/input_validator.json");
157 JSONObject root = new JSONObject(message);
158 JSONObject simulatorParams = root.getJSONObject(SIMULATOR_PARAMS);
159 JSONObject commonEventHeaderParams = root.getJSONObject(COMMON_EVENT_HEADER_PARAMS);
160 Optional<JSONObject> pnfRegistrationFields = root.has(MessageConstants.PNF_REGISTRATION_PARAMS)
161 ? Optional.of(root.getJSONObject(MessageConstants.PNF_REGISTRATION_PARAMS))
163 Optional<JSONObject> notificationFields = root.has(MessageConstants.NOTIFICATION_PARAMS)
164 ? Optional.of(root.getJSONObject(MessageConstants.NOTIFICATION_PARAMS))
167 factory.create(simulatorParams, commonEventHeaderParams, pnfRegistrationFields, notificationFields);
170 MDC.put(RESPONSE_CODE, OK.toString());
171 return ResponseBuilder.status(OK).put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
172 .put(MESSAGE, "Simulator started").build();
174 } catch (JSONException e) {
175 MDC.put(RESPONSE_CODE, BAD_REQUEST.toString());
176 LOGGER.warn("Cannot start simulator, invalid json format: {}", e.getMessage());
177 LOGGER.debug("Received json has invalid format", e);
178 return ResponseBuilder.status(BAD_REQUEST).put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
179 .put(MESSAGE, "Cannot start simulator, invalid json format").build();
181 } catch (ProcessingException | ValidationException | IOException e) {
182 MDC.put(RESPONSE_CODE, BAD_REQUEST.toString());
183 LOGGER.warn("Json validation failed: {}", e.getMessage());
184 return ResponseBuilder.status(BAD_REQUEST).put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
185 .put(MESSAGE, "Cannot start simulator - Json format is not compatible with schema definitions")
188 } catch (Exception e) {
189 MDC.put(RESPONSE_CODE, INTERNAL_SERVER_ERROR.toString());
190 LOGGER.error("Cannot start simulator - unexpected exception", e);
191 return ResponseBuilder.status(INTERNAL_SERVER_ERROR)
192 .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
193 .put(MESSAGE, "Unexpected exception: " + e.getMessage()).build();
201 @GetMapping("status")
202 public ResponseEntity status() {
203 if (isSimulatorRunning()) {
204 ResponseBuilder responseBuilder = ResponseBuilder.status(OK)
205 .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)).put(SIMULATOR_STATUS, "RUNNING");
207 return !simulator.isEndless() ? responseBuilder.put(REMAINING_TIME, simulator.getRemainingTime()).build()
208 : responseBuilder.build();
210 return ResponseBuilder.status(OK).put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
211 .put(SIMULATOR_STATUS, "NOT RUNNING").build();
216 public ResponseEntity stop() {
217 if (isSimulatorRunning()) {
218 simulator.interrupt();
220 return ResponseBuilder.status(OK).put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
221 .put(MESSAGE, "Simulator successfully stopped").build();
223 return ResponseBuilder.status(BAD_REQUEST).put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT))
224 .put(MESSAGE, "Cannot stop simulator, because it's not running").build();
228 private boolean isSimulatorRunning() {
229 return simulator != null && simulator.isAlive();