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.OTHER_FIELDS;
-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 org.json.JSONObject;
public class MessageProvider {
- public JSONObject createMessage(JSONObject params) {
+ public JSONObject createMessage(JSONObject commonEventHeaderParams, Optional<JSONObject> pnfRegistrationParams,
+ 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 otherFields = JSONObjectFactory.generateConstantOtherFields();
+ 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)) {
- otherFields.put(key, 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(OTHER_FIELDS, otherFields);
+ 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);
+ });
+ }
+
}