d59e82968fc72e4a3cbfacec18d43b6f1a9d7505
[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.COMMON_EVENT_HEADER;
21 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN;
22 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_NOTIFICATION;
23 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_PNF_REGISTRATION;
24 import static org.onap.pnfsimulator.message.MessageConstants.EVENT;
25 import static org.onap.pnfsimulator.message.MessageConstants.EVENT_TYPE;
26 import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS;
27 import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Optional;
31 import org.json.JSONArray;
32 import org.json.JSONObject;
33
34 public class MessageProvider {
35
36     public JSONObject createMessage(JSONObject commonEventHeaderParams, Optional<JSONObject> pnfRegistrationParams,
37             Optional<JSONObject> notificationParams) {
38
39         if (!pnfRegistrationParams.isPresent() && !notificationParams.isPresent()) {
40             throw new IllegalArgumentException(
41                     "Both PNF registration and notification parameters objects are not present");
42         }
43         JSONObject event = new JSONObject();
44
45         JSONObject commonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
46         Map<String, Object> commonEventHeaderFields = commonEventHeaderParams.toMap();
47         commonEventHeaderFields.forEach((key, value) -> {
48             commonEventHeader.put(key, value);
49         });
50
51         JSONObject pnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields();
52         pnfRegistrationParams.ifPresent(jsonObject -> {
53             copyParametersToFields(jsonObject.toMap(), pnfRegistrationFields);
54             commonEventHeader.put(DOMAIN, DOMAIN_PNF_REGISTRATION);
55             commonEventHeader.put(EVENT_TYPE, DOMAIN_PNF_REGISTRATION);
56             event.put(PNF_REGISTRATION_FIELDS, pnfRegistrationFields);
57         });
58
59         JSONObject notificationFields = JSONObjectFactory.generateNotificationFields();
60         notificationParams.ifPresent(jsonObject -> {
61             copyParametersToFields(jsonObject.toMap(), notificationFields);
62             commonEventHeader.put(DOMAIN, DOMAIN_NOTIFICATION);
63             event.put(NOTIFICATION_FIELDS, notificationFields);
64         });
65
66         event.put(COMMON_EVENT_HEADER, commonEventHeader);
67         JSONObject root = new JSONObject();
68         root.put(EVENT, event);
69         return root;
70     }
71
72     private void copyParametersToFields(Map<String, Object> paramersMap, JSONObject fieldsJsonObject) {
73         paramersMap.forEach((key, value) -> {
74             fieldsJsonObject.put(key, value);
75         });
76     }
77
78     public JSONObject createOneVes(JSONObject commonEventHeaderParams, Optional<JSONObject> pnfRegistrationParams,
79             Optional<JSONObject> notificationParams, String url, String fileName) {
80
81
82         if (!pnfRegistrationParams.isPresent() && !notificationParams.isPresent()) {
83             throw new IllegalArgumentException(
84                     "Both PNF registration and notification parameters objects are not present");
85         }
86         JSONObject event = new JSONObject();
87
88         JSONObject commonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
89         Map<String, Object> commonEventHeaderFields = commonEventHeaderParams.toMap();
90         commonEventHeaderFields.forEach((key, value) -> {
91             commonEventHeader.put(key, value);
92         });
93
94         JSONObject pnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields();
95         pnfRegistrationParams.ifPresent(jsonObject -> {
96             copyParametersToFields(jsonObject.toMap(), pnfRegistrationFields);
97             commonEventHeader.put(DOMAIN, DOMAIN_PNF_REGISTRATION);
98             commonEventHeader.put(EVENT_TYPE, DOMAIN_PNF_REGISTRATION);
99             event.put(PNF_REGISTRATION_FIELDS, pnfRegistrationFields);
100         });
101
102         JSONObject notificationFields = JSONObjectFactory.generateNotificationFields();
103
104         Map hashMap = new HashMap();
105         hashMap.put("location", "LOCATION_DUMMY");
106         hashMap.put("fileFormatType", "org.3GPP.32.435#measCollec");
107         hashMap.put("fileFormatVersion", "V10");
108         hashMap.put("compression", "gzip");
109
110
111         JSONObject jsonHashMap = new JSONObject();
112         jsonHashMap.put("hashmap", jsonHashMap);
113
114         JSONArray jsonArrayOfNamedHashMap = new JSONArray();
115         jsonArrayOfNamedHashMap.put(jsonHashMap);
116
117
118
119         // // notification.put("name", "NAME_DUMMY");
120         // JSONObject notification = new JSONObject();
121         //
122         // notificationParams.ifPresent(jsonObject -> {
123         // copyParametersToFields(notification, notificationFields);
124         // commonEventHeader.put(DOMAIN, DOMAIN_NOTIFICATION);
125         // event.put(NOTIFICATION_FIELDS, notificationFields);
126         // });
127
128
129         // notificationParams.ifPresent(jsonObject -> {
130         // copyParametersToFields(jsonObject.toMap(), notificationFields);
131         // commonEventHeader.put(DOMAIN, DOMAIN_NOTIFICATION);
132         // event.put(NOTIFICATION_FIELDS, notificationFields);
133         // });
134
135         event.put(COMMON_EVENT_HEADER, commonEventHeader);
136         JSONObject root = new JSONObject();
137         root.put(EVENT, event);
138         return root;
139
140     }
141
142     public JSONObject createOneVesEvent(String xnfUrl, String fileName) {
143
144         String notificationFields;
145         JSONObject nof = new JSONObject();
146
147         nof.put("notificationFieldsVersion", "2.0");
148
149         nof.put("changeType", "FileReady");
150         nof.put("changeIdentifier", "PM_MEAS_FILES");
151
152         JSONObject hm = new JSONObject();
153         hm.put("location", xnfUrl.concat(fileName));
154         hm.put("fileFormatType", "org.3GPP.32.435#measCollec");
155         hm.put("fileFormatVersion", "V10");
156         hm.put("compression", "gzip");
157
158         JSONObject aonh = new JSONObject();
159         aonh.put("name", fileName);
160
161         aonh.put("hashMap", hm);
162
163         nof.put("arrayOfNamedHashMap", aonh);
164
165         String nofString = nof.toString();
166
167         JSONObject ceh = new JSONObject(); // commonEventHandler
168         ceh.put("startEpochMicrosec", "1551865758690");
169         ceh.put("sourceId", "val13");
170         ceh.put("eventId", "registration_51865758");
171         ceh.put("nfcNamingCode", "oam");
172         ceh.put("priority", "Normal");
173         ceh.put("version", "4.0.1");
174         ceh.put("reportingEntityName", "NOK6061ZW3");
175         ceh.put("sequence", "0");
176         ceh.put("domain", "notification");
177         ceh.put("lastEpochMicrosec", "1551865758690");
178         ceh.put("eventName", "pnfRegistration_Nokia_5gDu");
179         ceh.put("vesEventListenerVersion", "7.0.1");
180         ceh.put("sourceName", "NOK6061ZW3");
181         ceh.put("nfNamingCode", "gNB");
182
183         JSONObject ihf = new JSONObject(); // internalHeaderFields
184         ceh.put("internalHeaderFields", ihf);
185
186         JSONObject event = new JSONObject();
187         event.put("commonEventHeader", ceh);
188         event.put("notificationFields", nof);
189
190         System.out.println("event: ");
191         System.out.println(event.toString());
192
193         return event;
194
195      // @formatter:off
196         /*
197         {
198             "commonEventHeader": {                          <== "ceh"
199                 "startEpochMicrosec": "1551865758690",
200                 "sourceId": "val13",
201                 "eventId": "registration_51865758",
202                 "nfcNamingCode": "oam",
203                 "internalHeaderFields": {},                 <== "ihf"
204                 "priority": "Normal",
205                 "version": "4.0.1",
206                 "reportingEntityName": "NOK6061ZW3",
207                 "sequence": "0",
208                 "domain": "notification",
209                 "lastEpochMicrosec": "1551865758690",
210                 "eventName": "pnfRegistration_Nokia_5gDu",
211                 "vesEventListenerVersion": "7.0.1",
212                 "sourceName": "NOK6061ZW3",
213                 "nfNamingCode": "gNB"
214             },
215             "notificationFields": {                         <== "nof"
216                 "": "",
217                 "notificationFieldsVersion": "2.0",
218                 "changeType": "FileReady",
219                 "changeIdentifier": "PM_MEAS_FILES",
220                 "arrayOfNamedHashMap": {                    <== "aonh"
221                     "name": "A20161224.1030-1045.bin.gz",
222                     "hashMap": {                            <== "hm"
223                         "location": "ftpes://192.169.0.1:22/ftp/rop/A20161224.1030-1045.bin.gz",
224                         "fileFormatType": "org.3GPP.32.435#measCollec",
225                         "fileFormatVersion": "V10",
226                         "compression": "gzip"
227                     }
228                 }
229             }
230         }
231
232         */
233      // @formatter:on
234
235     }
236
237 }