7c2706d0d755b823ef2e0d3f90887257f10fc0fd
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / onap / dcaegen2 / collectors / datafile / utils / JsonMessage.java
1 /*-
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
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.onap.dcaegen2.collectors.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  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> on 7/25/18
31  *
32  */
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;
39
40     public List<AdditionalField> getAdditionalFields() {
41         return arrayOfAdditionalFields;
42     }
43
44     @Override
45     public String toString() {
46         return "[" + getParsed() + "]";
47     }
48
49     /**
50      * Gets the message in parsed format.
51      *
52      * @return the massage in parsed format.
53      */
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(",");
63                 }
64             }
65             additionalFieldsString.append("]");
66         }
67         return "{" //
68             + "\"event\":" //
69             + "{" //
70             + "\"commonEventHeader\":" //
71             + "{" //
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\"," //
87             + "\"version\":3" //
88             + "}," //
89             + "\"notificationFields\":" //
90             + "{" //
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() //
98             + "}" //
99             + "}" //
100             + "}";
101     }
102
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;
109     }
110
111     public static class AdditionalField {
112         private String name;
113         private String location;
114         private String compression;
115         private String fileFormatType;
116         private String fileFormatVersion;
117
118         @Override
119         public String toString() {
120             return "{" //
121                 + getAsStringIfParameterIsSet("name", name, true) //
122                 + "\"hashMap\":" //
123                 + "{"
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) //
130                 + "}" //
131                 + "}";
132         }
133
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;
140         }
141
142     }
143
144     public static class AdditionalFieldBuilder {
145         private String name;
146         private String location;
147         private String compression;
148         private String fileFormatType;
149         private String fileFormatVersion;
150
151         public AdditionalFieldBuilder name(String name) {
152             this.name = name;
153             return this;
154         }
155
156         public AdditionalFieldBuilder location(String location) {
157             this.location = location;
158             return this;
159         }
160
161         public AdditionalFieldBuilder compression(String compression) {
162             this.compression = compression;
163             return this;
164         }
165
166         public AdditionalFieldBuilder fileFormatType(String fileFormatType) {
167             this.fileFormatType = fileFormatType;
168             return this;
169         }
170
171         public AdditionalFieldBuilder fileFormatVersion(String fileFormatVersion) {
172             this.fileFormatVersion = fileFormatVersion;
173             return this;
174         }
175
176         public AdditionalField build() {
177             return new AdditionalField(this);
178         }
179     }
180
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>();
187
188         public JsonMessageBuilder eventName(String eventName) {
189             this.eventName = eventName;
190             return this;
191         }
192
193         public JsonMessageBuilder changeIdentifier(String changeIdentifier) {
194             this.changeIdentifier = changeIdentifier;
195             return this;
196         }
197
198         public JsonMessageBuilder changeType(String changeType) {
199             this.changeType = changeType;
200             return this;
201         }
202
203         public JsonMessageBuilder notificationFieldsVersion(String notificationFieldsVersion) {
204             this.notificationFieldsVersion = notificationFieldsVersion;
205             return this;
206         }
207
208         public JsonMessageBuilder addAdditionalField(AdditionalField additionalField) {
209             this.arrayOfAdditionalFields.add(additionalField);
210             return this;
211         }
212
213         public JsonMessage build() {
214             return new JsonMessage(this);
215         }
216     }
217
218     private static String getAsStringIfParameterIsSet(String parameterName, String parameterValue,
219         boolean withSeparator) {
220         String result = "";
221         if (parameterValue != null) {
222             result = "\"" + parameterName + "\":\"" + parameterValue + "\"";
223
224             if (withSeparator) {
225                 result = result + ",";
226             }
227         }
228         return result;
229     }
230
231     /**
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>
234      *
235      * @param args Not used
236      */
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") //
244             .build();
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") //
251             .build();
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) //
259             .build();
260         System.out.println(message.toString());
261     }
262 }