diff options
author | Marcin Migdal <marcin.migdal@nokia.com> | 2018-09-07 12:59:00 +0200 |
---|---|---|
committer | Marcin Migdal <marcin.migdal@nokia.com> | 2018-09-13 16:12:18 +0200 |
commit | 577247a99292445a59e86e7bfe565a58be9a538f (patch) | |
tree | 6b0403306c9a33d5e74dbfe3d9bb2f1997db58d2 /test/mocks/pnfsimulator/src/main | |
parent | 1c760c54176f529dad26b5ab1b7875231ec2e3b3 (diff) |
Extracting configs and schema for notifications
Change-Id: Id004fdaabced8efbe6aff354db608d3d1b28a63e
Issue-ID: INT-607
Signed-off-by: Marcin Migdal <marcin.migdal@nokia.com>
Diffstat (limited to 'test/mocks/pnfsimulator/src/main')
5 files changed, 117 insertions, 55 deletions
diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/JSONObjectFactory.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/JSONObjectFactory.java index fd2b95af1..3ebf5674a 100644 --- a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/JSONObjectFactory.java +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/JSONObjectFactory.java @@ -20,7 +20,24 @@ package org.onap.pnfsimulator.message; -import static org.onap.pnfsimulator.message.MessageConstants.*; +import static org.onap.pnfsimulator.message.MessageConstants.EVENT_ID; +import static org.onap.pnfsimulator.message.MessageConstants.INTERNAL_HEADER_FIELDS; +import static org.onap.pnfsimulator.message.MessageConstants.LAST_EPOCH_MICROSEC; +import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS_VERSION; +import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS_VERSION_VALUE; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_LAST_SERVICE_DATE; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_MANUFACTURE_DATE; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS_VERSION; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS_VERSION_VALUE; +import static org.onap.pnfsimulator.message.MessageConstants.PRIORITY; +import static org.onap.pnfsimulator.message.MessageConstants.PRIORITY_NORMAL; +import static org.onap.pnfsimulator.message.MessageConstants.SEQUENCE; +import static org.onap.pnfsimulator.message.MessageConstants.SEQUENCE_NUMBER; +import static org.onap.pnfsimulator.message.MessageConstants.START_EPOCH_MICROSEC; +import static org.onap.pnfsimulator.message.MessageConstants.VERSION; +import static org.onap.pnfsimulator.message.MessageConstants.VERSION_NUMBER; +import static org.onap.pnfsimulator.message.MessageConstants.VES_EVENT_LISTENER_VERSION; +import static org.onap.pnfsimulator.message.MessageConstants.VES_EVENT_LISTENER_VERSION_NUMBER; import org.json.JSONObject; @@ -29,9 +46,7 @@ final class JSONObjectFactory { static JSONObject generateConstantCommonEventHeader() { JSONObject commonEventHeader = new JSONObject(); long timestamp = System.currentTimeMillis(); - commonEventHeader.put(DOMAIN, PNF_REGISTRATION); commonEventHeader.put(EVENT_ID, generateEventId()); - commonEventHeader.put(EVENT_TYPE, PNF_REGISTRATION); commonEventHeader.put(LAST_EPOCH_MICROSEC, timestamp); commonEventHeader.put(PRIORITY, PRIORITY_NORMAL); commonEventHeader.put(SEQUENCE, SEQUENCE_NUMBER); @@ -50,13 +65,20 @@ final class JSONObjectFactory { return pnfRegistrationFields; } + static JSONObject generateNotificationFields() { + JSONObject notificationFields = new JSONObject(); + notificationFields.put(NOTIFICATION_FIELDS_VERSION, NOTIFICATION_FIELDS_VERSION_VALUE); + return notificationFields; + } + + static String generateEventId() { String timeAsString = String.valueOf(System.currentTimeMillis()); return String.format("registration_%s", timeAsString.substring(timeAsString.length() - 11, timeAsString.length() - 3)); } - private JSONObjectFactory(){ + private JSONObjectFactory() { } diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageConstants.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageConstants.java index 7861d5f9c..95e8f69f3 100644 --- a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageConstants.java +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageConstants.java @@ -22,9 +22,25 @@ package org.onap.pnfsimulator.message; public final class MessageConstants { - public static final String SIMULATOR_PARAMS_CONTAINER = "simulatorParams"; - public static final String MESSAGE_PARAMS_CONTAINER = "messageParams"; + public static final String SIMULATOR_PARAMS = "simulatorParams"; + public static final String COMMON_EVENT_HEADER_PARAMS = "commonEventHeaderParams"; + public static final String PNF_REGISTRATION_PARAMS = "pnfRegistrationParams"; + public static final String NOTIFICATION_PARAMS = "notificationParams"; + + static final String COMMON_EVENT_HEADER = "commonEventHeader"; + static final String PNF_REGISTRATION_FIELDS = "pnfRegistrationFields"; + static final String NOTIFICATION_FIELDS = "notificationFields"; static final String EVENT = "event"; + + //============================================================================================= + //Simulation parameters + public static final String VES_SERVER_URL = "vesServerUrl"; + public static final String TEST_DURATION = "testDuration"; + public static final String MESSAGE_INTERVAL = "messageInterval"; + + //============================================================================================= + //commonEventHeader + //parameters static final String DOMAIN = "domain"; static final String EVENT_ID = "eventId"; static final String EVENT_TYPE = "eventType"; @@ -34,32 +50,30 @@ public final class MessageConstants { static final String START_EPOCH_MICROSEC = "startEpochMicrosec"; static final String INTERNAL_HEADER_FIELDS = "internalHeaderFields"; static final String VERSION = "version"; - static final String PNF_REGISTRATION_FIELDS_VERSION = "pnfRegistrationFieldsVersion"; - static final String PNF_LAST_SERVICE_DATE = "lastServiceDate"; - static final String PNF_MANUFACTURE_DATE = "manufactureDate"; static final String VES_EVENT_LISTENER_VERSION = "vesEventListenerVersion"; - - // mandatory used in json file, but not in java logic - //public static final String PNF_OAM_IPV4_ADDRESS = "pnfOamIpv4Address"; - //public static final String PNF_OAM_IPV6_ADDRESS = "pnfOamIpv6Address"; - //public static final String PNF_SERIAL_NUMBER = "pnfSerialNumber"; - //public static final String PNF_VENDOR_NAME = "pnfVendorName"; - public static final String VES_SERVER_URL = "vesServerUrl"; - public static final String TEST_DURATION = "testDuration"; - public static final String MESSAGE_INTERVAL = "messageInterval"; - static final String PNF_PREFIX = "pnf_"; - static final String COMMON_EVENT_HEADER = "commonEventHeader"; - static final String PNF_REGISTRATION_FIELDS = "pnfRegistrationFields"; - - - //=============================================================== //constant values - static final String PNF_REGISTRATION ="pnfRegistration"; - static final String PRIORITY_NORMAL = "Normal"; + static final int SEQUENCE_NUMBER = 0; static final String VERSION_NUMBER = "4.0.1"; static final String VES_EVENT_LISTENER_VERSION_NUMBER = "7.0.1"; + static final String PRIORITY_NORMAL = "Normal"; + + //============================================================================================= + //PNF registration + //parameters + static final String PNF_REGISTRATION_FIELDS_VERSION = "pnfRegistrationFieldsVersion"; + static final String PNF_LAST_SERVICE_DATE = "lastServiceDate"; + static final String PNF_MANUFACTURE_DATE = "manufactureDate"; + //constant values static final String PNF_REGISTRATION_FIELDS_VERSION_VALUE = "2.0"; - static final int SEQUENCE_NUMBER = 0; + static final String DOMAIN_PNF_REGISTRATION ="pnfRegistration"; + + //============================================================================================= + // Notifications + //parameters + static final String NOTIFICATION_FIELDS_VERSION = "notificationFieldsVersion"; + //constant values + static final String NOTIFICATION_FIELDS_VERSION_VALUE = "2.0"; + static final String DOMAIN_NOTIFICATION ="notification"; private MessageConstants() { } diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java index 7c3bf9ef8..4931c3b91 100644 --- a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java @@ -21,40 +21,61 @@ package org.onap.pnfsimulator.message; import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER; +import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN; import static org.onap.pnfsimulator.message.MessageConstants.EVENT; -import static org.onap.pnfsimulator.message.MessageConstants.PNF_PREFIX; +import static org.onap.pnfsimulator.message.MessageConstants.EVENT_TYPE; +import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS; +import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_PNF_REGISTRATION; +import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_NOTIFICATION; import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS; import java.util.Map; +import java.util.Optional; +import javax.annotation.Nonnull; import org.json.JSONObject; public class MessageProvider { - public JSONObject createMessage(JSONObject params) { + public JSONObject createMessage(@Nonnull JSONObject commonEventHeaderParams,@Nonnull Optional<JSONObject> pnfRegistrationParams, + @Nonnull Optional<JSONObject> notificationParams) { - if (params == null) { - throw new IllegalArgumentException("Params object cannot be null"); + if (!pnfRegistrationParams.isPresent() && !notificationParams.isPresent()) { + throw new IllegalArgumentException( + "Both PNF registration and notification parameters objects are not present"); } + JSONObject event = new JSONObject(); - Map<String, Object> paramsMap = params.toMap(); - JSONObject root = new JSONObject(); JSONObject commonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader(); - JSONObject pnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields(); + Map<String, Object> commonEventHeaderFields = commonEventHeaderParams.toMap(); + commonEventHeaderFields.forEach((key, value) -> { + commonEventHeader.put(key, value); + }); - paramsMap.forEach((key, value) -> { + JSONObject pnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields(); + pnfRegistrationParams.ifPresent(jsonObject -> { + copyParametersToFields(jsonObject.toMap(), pnfRegistrationFields); + commonEventHeader.put(DOMAIN, DOMAIN_PNF_REGISTRATION); + commonEventHeader.put(EVENT_TYPE, DOMAIN_PNF_REGISTRATION); + event.put(PNF_REGISTRATION_FIELDS, pnfRegistrationFields); + }); - if (key.startsWith(PNF_PREFIX)) { - pnfRegistrationFields.put(key.substring(PNF_PREFIX.length()), value); - } else { - commonEventHeader.put(key, value); - } + JSONObject notificationFields = JSONObjectFactory.generateNotificationFields(); + notificationParams.ifPresent(jsonObject -> { + copyParametersToFields(jsonObject.toMap(), notificationFields); + commonEventHeader.put(DOMAIN, DOMAIN_NOTIFICATION); + event.put(NOTIFICATION_FIELDS, notificationFields); }); - JSONObject event = new JSONObject(); event.put(COMMON_EVENT_HEADER, commonEventHeader); - event.put(PNF_REGISTRATION_FIELDS, pnfRegistrationFields); + JSONObject root = new JSONObject(); root.put(EVENT, event); return root; } + private void copyParametersToFields(Map<String, Object> paramersMap, JSONObject fieldsJsonObject) { + paramersMap.forEach((key, value) -> { + fieldsJsonObject.put(key, value); + }); + } + } diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/SimulatorController.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/SimulatorController.java index 3b275a66f..506d21b6c 100644 --- a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/SimulatorController.java +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/SimulatorController.java @@ -27,7 +27,8 @@ import static org.onap.pnfsimulator.logging.MDCVariables.RESPONSE_CODE; import static org.onap.pnfsimulator.logging.MDCVariables.SERVICE_NAME; import static org.onap.pnfsimulator.logging.MDCVariables.X_INVOCATION_ID; import static org.onap.pnfsimulator.logging.MDCVariables.X_ONAP_REQUEST_ID; -import static org.onap.pnfsimulator.message.MessageConstants.SIMULATOR_PARAMS_CONTAINER; +import static org.onap.pnfsimulator.message.MessageConstants.SIMULATOR_PARAMS; +import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER_PARAMS; import static org.onap.pnfsimulator.rest.util.ResponseBuilder.MESSAGE; import static org.onap.pnfsimulator.rest.util.ResponseBuilder.REMAINING_TIME; import static org.onap.pnfsimulator.rest.util.ResponseBuilder.SIMULATOR_STATUS; @@ -40,6 +41,7 @@ import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import com.github.fge.jsonschema.core.exceptions.ProcessingException; +import java.util.Optional; import java.util.UUID; import org.json.JSONException; import org.json.JSONObject; @@ -65,7 +67,6 @@ import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; - @RestController @RequestMapping("/simulator") public class SimulatorController { @@ -74,7 +75,6 @@ public class SimulatorController { private static final DateFormat RESPONSE_DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss,SSS"); private final Marker ENTRY = MarkerFactory.getMarker("ENTRY"); private Simulator simulator; - private JSONValidator validator; private SimulatorFactory factory; @@ -90,7 +90,7 @@ public class SimulatorController { MDC.put(INVOCATION_ID, headers.getFirst(X_INVOCATION_ID)); MDC.put(INSTANCE_UUID, UUID.randomUUID().toString()); MDC.put(SERVICE_NAME, "/simulator/start"); - LOGGER.info(ENTRY,"Simulator starting"); + LOGGER.info(ENTRY, "Simulator starting"); if (isSimulatorRunning()) { MDC.put(RESPONSE_CODE, BAD_REQUEST.toString()); @@ -103,12 +103,15 @@ public class SimulatorController { try { validator.validate(message, "json_schema/input_validator.json"); - JSONObject root = new JSONObject(message); - JSONObject simulatorParams = root.getJSONObject(SIMULATOR_PARAMS_CONTAINER); - JSONObject messageParams = root.getJSONObject(MessageConstants.MESSAGE_PARAMS_CONTAINER); - - simulator = factory.create(simulatorParams, messageParams); + JSONObject simulatorParams = root.getJSONObject(SIMULATOR_PARAMS); + JSONObject commonEventHeaderParams = root.getJSONObject(COMMON_EVENT_HEADER_PARAMS); + Optional<JSONObject> pnfRegistrationFields = root.has(MessageConstants.PNF_REGISTRATION_PARAMS) ? Optional + .of(root.getJSONObject(MessageConstants.PNF_REGISTRATION_PARAMS)) : Optional.empty(); + Optional<JSONObject> notificationFields = root.has(MessageConstants.NOTIFICATION_PARAMS) ? Optional + .of(root.getJSONObject(MessageConstants.NOTIFICATION_PARAMS)) : Optional.empty(); + simulator = factory + .create(simulatorParams, commonEventHeaderParams, pnfRegistrationFields, notificationFields); simulator.start(); MDC.put(RESPONSE_CODE, OK.toString()); @@ -145,8 +148,7 @@ public class SimulatorController { .put(TIMESTAMP, DateUtil.getTimestamp(RESPONSE_DATE_FORMAT)) .put(MESSAGE, "Unexpected exception: " + e.getMessage()) .build(); - } - finally { + } finally { MDC.clear(); } } diff --git a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java index 394864431..046c97cad 100644 --- a/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java +++ b/test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java @@ -28,6 +28,7 @@ import static org.onap.pnfsimulator.message.MessageConstants.VES_SERVER_URL; import com.github.fge.jsonschema.core.exceptions.ProcessingException; import java.io.IOException; import java.time.Duration; +import java.util.Optional; import org.json.JSONObject; import org.onap.pnfsimulator.message.MessageProvider; import org.onap.pnfsimulator.simulator.validation.JSONValidator; @@ -49,13 +50,15 @@ public class SimulatorFactory { this.validator = validator; } - public Simulator create(JSONObject simulatorParams, JSONObject messageParams) + public Simulator create(JSONObject simulatorParams, JSONObject commonEventHeaderParams, + Optional<JSONObject> pnfRegistrationParams, Optional<JSONObject> notificationParams) throws ProcessingException, IOException, ValidationException { Duration duration = Duration.ofSeconds(parseInt(simulatorParams.getString(TEST_DURATION))); Duration interval = Duration.ofSeconds(parseInt(simulatorParams.getString(MESSAGE_INTERVAL))); String vesUrl = simulatorParams.getString(VES_SERVER_URL); - JSONObject messageBody = messageProvider.createMessage(messageParams); + JSONObject messageBody = messageProvider + .createMessage(commonEventHeaderParams, pnfRegistrationParams, notificationParams); validator.validate(messageBody.toString(), DEFAULT_OUTPUT_SCHEMA_PATH); return Simulator.builder() |