Merge "Extracting configs and schema for notifications"
[integration.git] / test / mocks / pnfsimulator / src / main / java / org / onap / pnfsimulator / message / MessageProvider.java
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.pnfsimulator.message;
22
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;
31
32 import java.util.Map;
33 import java.util.Optional;
34 import javax.annotation.Nonnull;
35 import org.json.JSONObject;
36
37 public class MessageProvider {
38
39     public JSONObject createMessage(@Nonnull  JSONObject commonEventHeaderParams,@Nonnull Optional<JSONObject> pnfRegistrationParams,
40         @Nonnull Optional<JSONObject> notificationParams) {
41
42         if (!pnfRegistrationParams.isPresent() && !notificationParams.isPresent()) {
43             throw new IllegalArgumentException(
44                 "Both PNF registration and notification parameters objects are not present");
45         }
46         JSONObject event = new JSONObject();
47
48         JSONObject commonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
49         Map<String, Object> commonEventHeaderFields = commonEventHeaderParams.toMap();
50         commonEventHeaderFields.forEach((key, value) -> {
51             commonEventHeader.put(key, value);
52         });
53
54         JSONObject pnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields();
55         pnfRegistrationParams.ifPresent(jsonObject -> {
56             copyParametersToFields(jsonObject.toMap(), pnfRegistrationFields);
57             commonEventHeader.put(DOMAIN, DOMAIN_PNF_REGISTRATION);
58             commonEventHeader.put(EVENT_TYPE, DOMAIN_PNF_REGISTRATION);
59             event.put(PNF_REGISTRATION_FIELDS, pnfRegistrationFields);
60         });
61
62         JSONObject notificationFields = JSONObjectFactory.generateNotificationFields();
63         notificationParams.ifPresent(jsonObject -> {
64             copyParametersToFields(jsonObject.toMap(), notificationFields);
65             commonEventHeader.put(DOMAIN, DOMAIN_NOTIFICATION);
66             event.put(NOTIFICATION_FIELDS, notificationFields);
67         });
68
69         event.put(COMMON_EVENT_HEADER, commonEventHeader);
70         JSONObject root = new JSONObject();
71         root.put(EVENT, event);
72         return root;
73     }
74
75     private void copyParametersToFields(Map<String, Object> paramersMap, JSONObject fieldsJsonObject) {
76         paramersMap.forEach((key, value) -> {
77             fieldsJsonObject.put(key, value);
78         });
79     }
80
81 }