Merge "switch drools pdp image to new one"
[integration.git] / test / mocks / mass-pnf-sim / pnf-sim-lightweight / src / main / java / org / onap / pnfsimulator / message / MessageProvider.java
1 /*
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
16  */
17
18 package org.onap.pnfsimulator.message;
19
20 import static org.onap.pnfsimulator.message.MessageConstants.ARRAY_OF_NAMED_HASH_MAP;
21 import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER;
22 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN;
23 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_NOTIFICATION;
24 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_PNF_REGISTRATION;
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.PNF_REGISTRATION_FIELDS;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Optional;
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35
36 public class MessageProvider {
37
38     public JSONObject createMessage(JSONObject commonEventHeaderParams, Optional<JSONObject> pnfRegistrationParams,
39             Optional<JSONObject> notificationParams) {
40         List<String> emptyList = new ArrayList<>();
41         String emptyString = "";
42         return createMessage(commonEventHeaderParams, pnfRegistrationParams, notificationParams, emptyList, emptyString);
43     }
44
45     public JSONObject createMessage(JSONObject commonEventHeaderParams, Optional<JSONObject> pnfRegistrationParams,
46             Optional<JSONObject> notificationParams, List<String> fileList, String xnfUrl) {
47
48         if (!pnfRegistrationParams.isPresent() && !notificationParams.isPresent()) {
49             throw new IllegalArgumentException(
50                     "Both PNF registration and notification parameters objects are not present");
51         }
52         JSONObject event = new JSONObject();
53
54         JSONObject commonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
55         Map<String, Object> commonEventHeaderFields = commonEventHeaderParams.toMap();
56         commonEventHeaderFields.forEach((key, value) -> {
57             commonEventHeader.put(key, value);
58         });
59
60         JSONObject pnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields();
61         pnfRegistrationParams.ifPresent(jsonObject -> {
62             copyParametersToFields(jsonObject.toMap(), pnfRegistrationFields);
63             commonEventHeader.put(DOMAIN, DOMAIN_PNF_REGISTRATION);
64             commonEventHeader.put(EVENT_TYPE, DOMAIN_PNF_REGISTRATION);
65             event.put(PNF_REGISTRATION_FIELDS, pnfRegistrationFields);
66         });
67
68         JSONObject notificationFields = JSONObjectFactory.generateNotificationFields();
69         notificationParams.ifPresent(jsonObject -> {
70             copyParametersToFields(jsonObject.toMap(), notificationFields);
71             JSONArray arrayOfNamedHashMap = JSONObjectFactory.generateArrayOfNamedHashMap(fileList, xnfUrl);
72             notificationFields.put(ARRAY_OF_NAMED_HASH_MAP, arrayOfNamedHashMap);
73             commonEventHeader.put(DOMAIN, DOMAIN_NOTIFICATION);
74             event.put(NOTIFICATION_FIELDS, notificationFields);
75         });
76
77         event.put(COMMON_EVENT_HEADER, commonEventHeader);
78         JSONObject root = new JSONObject();
79         root.put(EVENT, event);
80         return root;
81     }
82
83     private void copyParametersToFields(Map<String, Object> paramersMap, JSONObject fieldsJsonObject) {
84         paramersMap.forEach((key, value) -> {
85             fieldsJsonObject.put(key, value);
86         });
87     }
88 }