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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.oran.datafile.utils;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
28 * Utility class to produce correctly formatted fileReady event Json messages.
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;
37 public List<AdditionalField> getAdditionalFields() {
38 return arrayOfAdditionalFields;
42 public String toString() {
43 return "[" + getParsed() + "]";
47 * Gets the message in parsed format.
49 * @return the massage in parsed format.
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(",");
62 additionalFieldsString.append("]");
67 + "\"commonEventHeader\":" //
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\"," //
86 + "\"notificationFields\":" //
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() //
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;
108 public static class AdditionalField {
110 private String location;
111 private String compression;
112 private String fileFormatType;
113 private String fileFormatVersion;
116 public String toString() {
118 + getAsStringIfParameterIsSet("name", name, true) //
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) //
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;
141 public static class AdditionalFieldBuilder {
143 private String location;
144 private String compression;
145 private String fileFormatType;
146 private String fileFormatVersion;
148 public AdditionalFieldBuilder name(String name) {
153 public AdditionalFieldBuilder location(String location) {
154 this.location = location;
158 public AdditionalFieldBuilder compression(String compression) {
159 this.compression = compression;
163 public AdditionalFieldBuilder fileFormatType(String fileFormatType) {
164 this.fileFormatType = fileFormatType;
168 public AdditionalFieldBuilder fileFormatVersion(String fileFormatVersion) {
169 this.fileFormatVersion = fileFormatVersion;
173 public AdditionalField build() {
174 return new AdditionalField(this);
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>();
185 public JsonMessageBuilder eventName(String eventName) {
186 this.eventName = eventName;
190 public JsonMessageBuilder changeIdentifier(String changeIdentifier) {
191 this.changeIdentifier = changeIdentifier;
195 public JsonMessageBuilder changeType(String changeType) {
196 this.changeType = changeType;
200 public JsonMessageBuilder notificationFieldsVersion(String notificationFieldsVersion) {
201 this.notificationFieldsVersion = notificationFieldsVersion;
205 public JsonMessageBuilder addAdditionalField(AdditionalField additionalField) {
206 this.arrayOfAdditionalFields.add(additionalField);
210 public JsonMessage build() {
211 return new JsonMessage(this);
215 private static String getAsStringIfParameterIsSet(String parameterName, String parameterValue,
216 boolean withSeparator) {
218 if (parameterValue != null) {
219 result = "\"" + parameterName + "\":\"" + parameterValue + "\"";
222 result = result + ",";
229 * Can be used to produce a correct test Json message. Tip! Check the formatting
231 * <a href="https://jsonformatter.org/">Json formatter</a>
233 * @param args Not used
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") //
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") //
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) //
258 System.out.println(message.toString());