2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.pnfsimulator.message;
23 import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER;
24 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN;
25 import static org.onap.pnfsimulator.message.MessageConstants.EVENT;
26 import static org.onap.pnfsimulator.message.MessageConstants.EVENT_TYPE;
27 import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS;
28 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_PNF_REGISTRATION;
29 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_NOTIFICATION;
30 import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS;
33 import java.util.Optional;
34 import org.json.JSONObject;
36 public class MessageProvider {
38 public JSONObject createMessage(JSONObject commonEventHeaderParams, Optional<JSONObject> pnfRegistrationParams,
39 Optional<JSONObject> notificationParams) {
41 if (!pnfRegistrationParams.isPresent() && !notificationParams.isPresent()) {
42 throw new IllegalArgumentException(
43 "Both PNF registration and notification parameters objects are not present");
45 JSONObject event = new JSONObject();
47 JSONObject commonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
48 Map<String, Object> commonEventHeaderFields = commonEventHeaderParams.toMap();
49 commonEventHeaderFields.forEach((key, value) -> {
50 commonEventHeader.put(key, value);
53 JSONObject pnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields();
54 pnfRegistrationParams.ifPresent(jsonObject -> {
55 copyParametersToFields(jsonObject.toMap(), pnfRegistrationFields);
56 commonEventHeader.put(DOMAIN, DOMAIN_PNF_REGISTRATION);
57 commonEventHeader.put(EVENT_TYPE, DOMAIN_PNF_REGISTRATION);
58 event.put(PNF_REGISTRATION_FIELDS, pnfRegistrationFields);
61 JSONObject notificationFields = JSONObjectFactory.generateNotificationFields();
62 notificationParams.ifPresent(jsonObject -> {
63 copyParametersToFields(jsonObject.toMap(), notificationFields);
64 commonEventHeader.put(DOMAIN, DOMAIN_NOTIFICATION);
65 event.put(NOTIFICATION_FIELDS, notificationFields);
68 event.put(COMMON_EVENT_HEADER, commonEventHeader);
69 JSONObject root = new JSONObject();
70 root.put(EVENT, event);
74 private void copyParametersToFields(Map<String, Object> paramersMap, JSONObject fieldsJsonObject) {
75 paramersMap.forEach((key, value) -> {
76 fieldsJsonObject.put(key, value);