2 * ============LICENSE_START=======================================================
3 * 2018-2019 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.onap.dcaegen2.collectors.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 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> on 7/25/18
33 public class JsonMessage {
34 private String eventName;
35 private String changeIdentifier;
36 private String changeType;
37 private String notificationFieldsVersion;
38 private List<AdditionalField> arrayOfAdditionalFields;
40 public List<AdditionalField> getAdditionalFields() {
41 return arrayOfAdditionalFields;
45 public String toString() {
46 return "[" + getParsed() + "]";
50 * Gets the message in parsed format.
52 * @return the massage in parsed format.
54 public String getParsed() {
55 StringBuffer additionalFieldsString = new StringBuffer();
56 if (arrayOfAdditionalFields.size() > 0) {
57 additionalFieldsString.append("\"arrayOfNamedHashMap\":[");
58 for (Iterator<AdditionalField> iterator = arrayOfAdditionalFields.iterator(); iterator.hasNext();) {
59 AdditionalField additionalField = iterator.next();
60 additionalFieldsString.append(additionalField.toString());
61 if (iterator.hasNext()) {
62 additionalFieldsString.append(",");
65 additionalFieldsString.append("]");
70 + "\"commonEventHeader\":" //
72 + "\"domain\":\"notification\"," //
73 + "\"eventId\":\"<<SerialNumber>>-reg\"," //
74 + "\"eventName\":\"" + eventName + "\"," //
75 + "\"eventType\":\"fileReady\"," //
76 + "\"internalHeaderFields\":{}," //
77 + "\"lastEpochMicrosec\":1519837825682," //
78 + "\"nfNamingCode\":\"5GRAN\"," //
79 + "\"nfcNamingCode\":\"5DU\"," //
80 + "\"priority\":\"Normal\"," //
81 + "\"reportingEntityName\":\"5GRAN_DU\"," //
82 + "\"sequence\":0," //
83 + "\"sourceId\":\"<<SerialNumber>>\"," //
84 + "\"sourceName\":\"5GRAN_DU\"," //
85 + "\"timeZoneOffset\":\"UTC+05:00\"," //
86 + "\"startEpochMicrosec\":\"1519837825682\"," //
89 + "\"notificationFields\":" //
91 + getAsStringIfParameterIsSet("changeIdentifier", changeIdentifier,
92 changeType != null || notificationFieldsVersion != null || arrayOfAdditionalFields.size() > 0)
93 + getAsStringIfParameterIsSet("changeType", changeType,
94 notificationFieldsVersion != null || arrayOfAdditionalFields.size() > 0)
95 + getAsStringIfParameterIsSet("notificationFieldsVersion", notificationFieldsVersion,
96 arrayOfAdditionalFields.size() > 0)
97 + additionalFieldsString.toString() //
103 private JsonMessage(final JsonMessageBuilder builder) {
104 this.eventName = builder.eventName;
105 this.changeIdentifier = builder.changeIdentifier;
106 this.changeType = builder.changeType;
107 this.notificationFieldsVersion = builder.notificationFieldsVersion;
108 this.arrayOfAdditionalFields = builder.arrayOfAdditionalFields;
111 public static class AdditionalField {
113 private String location;
114 private String compression;
115 private String fileFormatType;
116 private String fileFormatVersion;
119 public String toString() {
121 + getAsStringIfParameterIsSet("name", name, true) //
124 + getAsStringIfParameterIsSet("location", location,
125 compression != null || fileFormatType != null || fileFormatVersion != null)
126 + getAsStringIfParameterIsSet("compression", compression,
127 fileFormatType != null || fileFormatVersion != null)
128 + getAsStringIfParameterIsSet("fileFormatType", fileFormatType, fileFormatVersion != null)
129 + getAsStringIfParameterIsSet("fileFormatVersion", fileFormatVersion, false) //
134 private AdditionalField(AdditionalFieldBuilder builder) {
135 this.name = builder.name;
136 this.location = builder.location;
137 this.compression = builder.compression;
138 this.fileFormatType = builder.fileFormatType;
139 this.fileFormatVersion = builder.fileFormatVersion;
144 public static class AdditionalFieldBuilder {
146 private String location;
147 private String compression;
148 private String fileFormatType;
149 private String fileFormatVersion;
151 public AdditionalFieldBuilder name(String name) {
156 public AdditionalFieldBuilder location(String location) {
157 this.location = location;
161 public AdditionalFieldBuilder compression(String compression) {
162 this.compression = compression;
166 public AdditionalFieldBuilder fileFormatType(String fileFormatType) {
167 this.fileFormatType = fileFormatType;
171 public AdditionalFieldBuilder fileFormatVersion(String fileFormatVersion) {
172 this.fileFormatVersion = fileFormatVersion;
176 public AdditionalField build() {
177 return new AdditionalField(this);
181 public static class JsonMessageBuilder {
182 private String eventName;
183 private String changeIdentifier;
184 private String changeType;
185 private String notificationFieldsVersion;
186 private List<AdditionalField> arrayOfAdditionalFields = new ArrayList<AdditionalField>();
188 public JsonMessageBuilder eventName(String eventName) {
189 this.eventName = eventName;
193 public JsonMessageBuilder changeIdentifier(String changeIdentifier) {
194 this.changeIdentifier = changeIdentifier;
198 public JsonMessageBuilder changeType(String changeType) {
199 this.changeType = changeType;
203 public JsonMessageBuilder notificationFieldsVersion(String notificationFieldsVersion) {
204 this.notificationFieldsVersion = notificationFieldsVersion;
208 public JsonMessageBuilder addAdditionalField(AdditionalField additionalField) {
209 this.arrayOfAdditionalFields.add(additionalField);
213 public JsonMessage build() {
214 return new JsonMessage(this);
218 private static String getAsStringIfParameterIsSet(String parameterName, String parameterValue,
219 boolean withSeparator) {
221 if (parameterValue != null) {
222 result = "\"" + parameterName + "\":\"" + parameterValue + "\"";
225 result = result + ",";
232 * Can be used to produce a correct test Json message. Tip! Check the formatting with
233 * <a href="https://jsonformatter.org/">Json formatter</a>
235 * @param args Not used
237 public static void main(String[] args) {
238 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder() //
239 .name("A20161224.1030-1045.bin.gz") //
240 .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz") //
241 .compression("gzip") //
242 .fileFormatType("org.3GPP.32.435#measCollec") //
243 .fileFormatVersion("V10") //
245 AdditionalField secondAdditionalField = new JsonMessage.AdditionalFieldBuilder() //
246 .name("A20161224.1030-1045.bin.gz") //
247 .location("sftp://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz") //
248 .compression("gzip") //
249 .fileFormatType("org.3GPP.32.435#measCollec") //
250 .fileFormatVersion("V10") //
252 JsonMessage message = new JsonMessage.JsonMessageBuilder() //
253 .eventName("Noti_NrRadio-Ericsson_FileReady") //
254 .changeIdentifier("PM_MEAS_FILES") //
255 .changeType("FileReady") //
256 .notificationFieldsVersion("2.0") //
257 .addAdditionalField(additionalField) //
258 .addAdditionalField(secondAdditionalField) //
260 System.out.println(message.toString());