Added support for using oauth token for Kafka
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / oran / datafile / utils / JsonMessage.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2018-2023 Nordix Foundation. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.oran.datafile.utils;
22
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 /**
28  * Utility class to produce correctly formatted fileReady event Json messages.
29  */
30 public class JsonMessage {
31     private String eventName;
32     private String changeIdentifier;
33     private String changeType;
34     private String notificationFieldsVersion;
35     private List<AdditionalField> arrayOfAdditionalFields;
36
37     public List<AdditionalField> getAdditionalFields() {
38         return arrayOfAdditionalFields;
39     }
40
41     @Override
42     public String toString() {
43         return "[" + getParsed() + "]";
44     }
45
46     /**
47      * Gets the message in parsed format.
48      *
49      * @return the massage in parsed format.
50      */
51     public String getParsed() {
52         StringBuffer additionalFieldsString = new StringBuffer();
53         if (arrayOfAdditionalFields.size() > 0) {
54             additionalFieldsString.append("\"arrayOfNamedHashMap\":[");
55             for (Iterator<AdditionalField> iterator = arrayOfAdditionalFields.iterator(); iterator.hasNext();) {
56                 AdditionalField additionalField = iterator.next();
57                 additionalFieldsString.append(additionalField.toString());
58                 if (iterator.hasNext()) {
59                     additionalFieldsString.append(",");
60                 }
61             }
62             additionalFieldsString.append("]");
63         }
64         return "{" //
65             + "\"event\":" //
66             + "{" //
67             + "\"commonEventHeader\":" //
68             + "{" //
69             + "\"domain\":\"notification\"," //
70             + "\"eventId\":\"<<SerialNumber>>-reg\"," //
71             + "\"eventName\":\"" + eventName + "\"," //
72             + "\"eventType\":\"fileReady\"," //
73             + "\"internalHeaderFields\":{}," //
74             + "\"lastEpochMicrosec\":1519837825682," //
75             + "\"nfNamingCode\":\"5GRAN\"," //
76             + "\"nfcNamingCode\":\"5DU\"," //
77             + "\"priority\":\"Normal\"," //
78             + "\"reportingEntityName\":\"5GRAN_DU\"," //
79             + "\"sequence\":0," //
80             + "\"sourceId\":\"<<SerialNumber>>\"," //
81             + "\"sourceName\":\"5GRAN_DU\"," //
82             + "\"timeZoneOffset\":\"UTC+05:00\"," //
83             + "\"startEpochMicrosec\":\"1519837825682\"," //
84             + "\"version\":3" //
85             + "}," //
86             + "\"notificationFields\":" //
87             + "{" //
88             + getAsStringIfParameterIsSet("changeIdentifier", changeIdentifier,
89                 changeType != null || notificationFieldsVersion != null || arrayOfAdditionalFields.size() > 0)
90             + getAsStringIfParameterIsSet("changeType", changeType,
91                 notificationFieldsVersion != null || arrayOfAdditionalFields.size() > 0)
92             + getAsStringIfParameterIsSet("notificationFieldsVersion", notificationFieldsVersion,
93                 arrayOfAdditionalFields.size() > 0)
94             + additionalFieldsString.toString() //
95             + "}" //
96             + "}" //
97             + "}";
98     }
99
100     private JsonMessage(final JsonMessageBuilder builder) {
101         this.eventName = builder.eventName;
102         this.changeIdentifier = builder.changeIdentifier;
103         this.changeType = builder.changeType;
104         this.notificationFieldsVersion = builder.notificationFieldsVersion;
105         this.arrayOfAdditionalFields = builder.arrayOfAdditionalFields;
106     }
107
108     public static class AdditionalField {
109         private String name;
110         private String location;
111         private String compression;
112         private String fileFormatType;
113         private String fileFormatVersion;
114
115         @Override
116         public String toString() {
117             return "{" //
118                 + getAsStringIfParameterIsSet("name", name, true) //
119                 + "\"hashMap\":" //
120                 + "{"
121                 + getAsStringIfParameterIsSet("location", location,
122                     compression != null || fileFormatType != null || fileFormatVersion != null)
123                 + getAsStringIfParameterIsSet("compression", compression,
124                     fileFormatType != null || fileFormatVersion != null)
125                 + getAsStringIfParameterIsSet("fileFormatType", fileFormatType, fileFormatVersion != null)
126                 + getAsStringIfParameterIsSet("fileFormatVersion", fileFormatVersion, false) //
127                 + "}" //
128                 + "}";
129         }
130
131         private AdditionalField(AdditionalFieldBuilder builder) {
132             this.name = builder.name;
133             this.location = builder.location;
134             this.compression = builder.compression;
135             this.fileFormatType = builder.fileFormatType;
136             this.fileFormatVersion = builder.fileFormatVersion;
137         }
138
139     }
140
141     public static class AdditionalFieldBuilder {
142         private String name;
143         private String location;
144         private String compression;
145         private String fileFormatType;
146         private String fileFormatVersion;
147
148         public AdditionalFieldBuilder name(String name) {
149             this.name = name;
150             return this;
151         }
152
153         public AdditionalFieldBuilder location(String location) {
154             this.location = location;
155             return this;
156         }
157
158         public AdditionalFieldBuilder compression(String compression) {
159             this.compression = compression;
160             return this;
161         }
162
163         public AdditionalFieldBuilder fileFormatType(String fileFormatType) {
164             this.fileFormatType = fileFormatType;
165             return this;
166         }
167
168         public AdditionalFieldBuilder fileFormatVersion(String fileFormatVersion) {
169             this.fileFormatVersion = fileFormatVersion;
170             return this;
171         }
172
173         public AdditionalField build() {
174             return new AdditionalField(this);
175         }
176     }
177
178     public static class JsonMessageBuilder {
179         private String eventName;
180         private String changeIdentifier;
181         private String changeType;
182         private String notificationFieldsVersion;
183         private List<AdditionalField> arrayOfAdditionalFields = new ArrayList<AdditionalField>();
184
185         public JsonMessageBuilder eventName(String eventName) {
186             this.eventName = eventName;
187             return this;
188         }
189
190         public JsonMessageBuilder changeIdentifier(String changeIdentifier) {
191             this.changeIdentifier = changeIdentifier;
192             return this;
193         }
194
195         public JsonMessageBuilder changeType(String changeType) {
196             this.changeType = changeType;
197             return this;
198         }
199
200         public JsonMessageBuilder notificationFieldsVersion(String notificationFieldsVersion) {
201             this.notificationFieldsVersion = notificationFieldsVersion;
202             return this;
203         }
204
205         public JsonMessageBuilder addAdditionalField(AdditionalField additionalField) {
206             this.arrayOfAdditionalFields.add(additionalField);
207             return this;
208         }
209
210         public JsonMessage build() {
211             return new JsonMessage(this);
212         }
213     }
214
215     private static String getAsStringIfParameterIsSet(String parameterName, String parameterValue,
216         boolean withSeparator) {
217         String result = "";
218         if (parameterValue != null) {
219             result = "\"" + parameterName + "\":\"" + parameterValue + "\"";
220
221             if (withSeparator) {
222                 result = result + ",";
223             }
224         }
225         return result;
226     }
227
228     /**
229      * Can be used to produce a correct test Json message. Tip! Check the formatting
230      * with
231      * <a href="https://jsonformatter.org/">Json formatter</a>
232      *
233      * @param args Not used
234      */
235     public static void main(String[] args) {
236         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder() //
237             .name("A20161224.1030-1045.bin.gz") //
238             .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz") //
239             .compression("gzip") //
240             .fileFormatType("org.3GPP.32.435#measCollec") //
241             .fileFormatVersion("V10") //
242             .build();
243         AdditionalField secondAdditionalField = new JsonMessage.AdditionalFieldBuilder() //
244             .name("A20161224.1030-1045.bin.gz") //
245             .location("sftp://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz") //
246             .compression("gzip") //
247             .fileFormatType("org.3GPP.32.435#measCollec") //
248             .fileFormatVersion("V10") //
249             .build();
250         JsonMessage message = new JsonMessage.JsonMessageBuilder() //
251             .eventName("Noti_NrRadio-Ericsson_FileReady") //
252             .changeIdentifier("PM_MEAS_FILES") //
253             .changeType("FileReady") //
254             .notificationFieldsVersion("2.0") //
255             .addAdditionalField(additionalField) //
256             .addAdditionalField(secondAdditionalField) //
257             .build();
258         System.out.println(message.toString());
259     }
260 }