Mass-pnf-simulator publishing ports
[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         event.put(COMMON_EVENT_HEADER, commonEventHeader);
118         JSONObject root = new JSONObject();
119         root.put(EVENT, event);
120         return root;
121
122     }
123
124     public JSONObject createOneVesEvent(String xnfUrl, String fileName) {
125
126         JSONObject nof = new JSONObject();
127
128         nof.put("notificationFieldsVersion", "2.0");
129
130         nof.put("changeType", "FileReady");
131         nof.put("changeIdentifier", "PM_MEAS_FILES");
132
133         JSONObject hm = new JSONObject();
134         hm.put("location", "ftpes://".concat(xnfUrl).concat(fileName));
135         hm.put("fileFormatType", "org.3GPP.32.435#measCollec");
136         hm.put("fileFormatVersion", "V10");
137         hm.put("compression", "gzip");
138
139         JSONObject aonhElement = new JSONObject();
140         aonhElement.put("name", fileName);
141         aonhElement.put("hashMap", hm);
142
143         JSONArray aonh = new JSONArray();
144         aonh.put(aonhElement);
145
146         nof.put("arrayOfNamedHashMap", aonh);
147
148         JSONObject ceh = new JSONObject(); // commonEventHandler
149         ceh.put("startEpochMicrosec", "1551865758690");
150         ceh.put("sourceId", "val13");
151         ceh.put("eventId", "registration_51865758");
152         ceh.put("nfcNamingCode", "oam");
153         ceh.put("priority", "Normal");
154         ceh.put("version", "4.0.1");
155         ceh.put("reportingEntityName", "NOK6061ZW3");
156         ceh.put("sequence", "0");
157         ceh.put("domain", "notification");
158         ceh.put("lastEpochMicrosec", "1551865758690");
159         ceh.put("eventName", "pnfRegistration_Nokia_5gDu");
160         ceh.put("vesEventListenerVersion", "7.0.1");
161         ceh.put("sourceName", "NOK6061ZW3");
162         ceh.put("nfNamingCode", "gNB");
163
164         JSONObject ihf = new JSONObject(); // internalHeaderFields
165         ceh.put("internalHeaderFields", ihf);
166
167         JSONObject eventContent = new JSONObject();
168         eventContent.put("commonEventHeader", ceh);
169         eventContent.put("notificationFields", nof);
170
171
172         JSONObject event = new JSONObject();
173         event.put("event", eventContent);
174
175         System.out.println("VES messages to be sent: ");
176         System.out.println(event.toString());
177
178         return event;
179
180      // @formatter:off
181         /*
182     {
183         "event": {
184             "commonEventHeader": {                          <== "ceh"
185                 "startEpochMicrosec": "1551865758690",
186                 "sourceId": "val13",
187                 "eventId": "registration_51865758",
188                 "nfcNamingCode": "oam",
189                 "internalHeaderFields": {},                 <== "ihf"
190                 "priority": "Normal",
191                 "version": "4.0.1",
192                 "reportingEntityName": "NOK6061ZW3",
193                 "sequence": "0",
194                 "domain": "notification",
195                 "lastEpochMicrosec": "1551865758690",
196                 "eventName": "pnfRegistration_Nokia_5gDu",
197                 "vesEventListenerVersion": "7.0.1",
198                 "sourceName": "NOK6061ZW3",
199                 "nfNamingCode": "gNB"
200             },
201             "notificationFields": {                         <== "nof"
202                 "": "",
203                 "notificationFieldsVersion": "2.0",
204                 "changeType": "FileReady",
205                 "changeIdentifier": "PM_MEAS_FILES",
206                 "arrayOfNamedHashMap": [                    <== "aonh"
207                     {                                       <== "aonhElement"
208                         "name": "A20161224.1030-1045.bin.gz",
209                         "hashMap": {                            <== "hm"
210                             "location": "ftpes://192.169.0.1:22/ftp/rop/A20161224.1030-1045.bin.gz",
211                             "fileFormatType": "org.3GPP.32.435#measCollec",
212                             "fileFormatVersion": "V10",
213                             "compression": "gzip"
214                          }
215                     }
216                 ]
217             }
218         }
219     }
220     */
221      // @formatter:on
222     }
223 }