aboutsummaryrefslogtreecommitdiffstats
path: root/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/utils/JsonMessage.java
blob: 7c2706d0d755b823ef2e0d3f90887257f10fc0fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*-
 * ============LICENSE_START=======================================================
 * 2018-2019 Nordix Foundation. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.dcaegen2.collectors.datafile.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Utility class to produce correctly formatted fileReady event Json messages.
 *
 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> on 7/25/18
 *
 */
public class JsonMessage {
    private String eventName;
    private String changeIdentifier;
    private String changeType;
    private String notificationFieldsVersion;
    private List<AdditionalField> arrayOfAdditionalFields;

    public List<AdditionalField> getAdditionalFields() {
        return arrayOfAdditionalFields;
    }

    @Override
    public String toString() {
        return "[" + getParsed() + "]";
    }

    /**
     * Gets the message in parsed format.
     *
     * @return the massage in parsed format.
     */
    public String getParsed() {
        StringBuffer additionalFieldsString = new StringBuffer();
        if (arrayOfAdditionalFields.size() > 0) {
            additionalFieldsString.append("\"arrayOfNamedHashMap\":[");
            for (Iterator<AdditionalField> iterator = arrayOfAdditionalFields.iterator(); iterator.hasNext();) {
                AdditionalField additionalField = iterator.next();
                additionalFieldsString.append(additionalField.toString());
                if (iterator.hasNext()) {
                    additionalFieldsString.append(",");
                }
            }
            additionalFieldsString.append("]");
        }
        return "{" //
            + "\"event\":" //
            + "{" //
            + "\"commonEventHeader\":" //
            + "{" //
            + "\"domain\":\"notification\"," //
            + "\"eventId\":\"<<SerialNumber>>-reg\"," //
            + "\"eventName\":\"" + eventName + "\"," //
            + "\"eventType\":\"fileReady\"," //
            + "\"internalHeaderFields\":{}," //
            + "\"lastEpochMicrosec\":1519837825682," //
            + "\"nfNamingCode\":\"5GRAN\"," //
            + "\"nfcNamingCode\":\"5DU\"," //
            + "\"priority\":\"Normal\"," //
            + "\"reportingEntityName\":\"5GRAN_DU\"," //
            + "\"sequence\":0," //
            + "\"sourceId\":\"<<SerialNumber>>\"," //
            + "\"sourceName\":\"5GRAN_DU\"," //
            + "\"timeZoneOffset\":\"UTC+05:00\"," //
            + "\"startEpochMicrosec\":\"1519837825682\"," //
            + "\"version\":3" //
            + "}," //
            + "\"notificationFields\":" //
            + "{" //
            + getAsStringIfParameterIsSet("changeIdentifier", changeIdentifier,
                changeType != null || notificationFieldsVersion != null || arrayOfAdditionalFields.size() > 0)
            + getAsStringIfParameterIsSet("changeType", changeType,
                notificationFieldsVersion != null || arrayOfAdditionalFields.size() > 0)
            + getAsStringIfParameterIsSet("notificationFieldsVersion", notificationFieldsVersion,
                arrayOfAdditionalFields.size() > 0)
            + additionalFieldsString.toString() //
            + "}" //
            + "}" //
            + "}";
    }

    private JsonMessage(final JsonMessageBuilder builder) {
        this.eventName = builder.eventName;
        this.changeIdentifier = builder.changeIdentifier;
        this.changeType = builder.changeType;
        this.notificationFieldsVersion = builder.notificationFieldsVersion;
        this.arrayOfAdditionalFields = builder.arrayOfAdditionalFields;
    }

    public static class AdditionalField {
        private String name;
        private String location;
        private String compression;
        private String fileFormatType;
        private String fileFormatVersion;

        @Override
        public String toString() {
            return "{" //
                + getAsStringIfParameterIsSet("name", name, true) //
                + "\"hashMap\":" //
                + "{"
                + getAsStringIfParameterIsSet("location", location,
                    compression != null || fileFormatType != null || fileFormatVersion != null)
                + getAsStringIfParameterIsSet("compression", compression,
                    fileFormatType != null || fileFormatVersion != null)
                + getAsStringIfParameterIsSet("fileFormatType", fileFormatType, fileFormatVersion != null)
                + getAsStringIfParameterIsSet("fileFormatVersion", fileFormatVersion, false) //
                + "}" //
                + "}";
        }

        private AdditionalField(AdditionalFieldBuilder builder) {
            this.name = builder.name;
            this.location = builder.location;
            this.compression = builder.compression;
            this.fileFormatType = builder.fileFormatType;
            this.fileFormatVersion = builder.fileFormatVersion;
        }

    }

    public static class AdditionalFieldBuilder {
        private String name;
        private String location;
        private String compression;
        private String fileFormatType;
        private String fileFormatVersion;

        public AdditionalFieldBuilder name(String name) {
            this.name = name;
            return this;
        }

        public AdditionalFieldBuilder location(String location) {
            this.location = location;
            return this;
        }

        public AdditionalFieldBuilder compression(String compression) {
            this.compression = compression;
            return this;
        }

        public AdditionalFieldBuilder fileFormatType(String fileFormatType) {
            this.fileFormatType = fileFormatType;
            return this;
        }

        public AdditionalFieldBuilder fileFormatVersion(String fileFormatVersion) {
            this.fileFormatVersion = fileFormatVersion;
            return this;
        }

        public AdditionalField build() {
            return new AdditionalField(this);
        }
    }

    public static class JsonMessageBuilder {
        private String eventName;
        private String changeIdentifier;
        private String changeType;
        private String notificationFieldsVersion;
        private List<AdditionalField> arrayOfAdditionalFields = new ArrayList<AdditionalField>();

        public JsonMessageBuilder eventName(String eventName) {
            this.eventName = eventName;
            return this;
        }

        public JsonMessageBuilder changeIdentifier(String changeIdentifier) {
            this.changeIdentifier = changeIdentifier;
            return this;
        }

        public JsonMessageBuilder changeType(String changeType) {
            this.changeType = changeType;
            return this;
        }

        public JsonMessageBuilder notificationFieldsVersion(String notificationFieldsVersion) {
            this.notificationFieldsVersion = notificationFieldsVersion;
            return this;
        }

        public JsonMessageBuilder addAdditionalField(AdditionalField additionalField) {
            this.arrayOfAdditionalFields.add(additionalField);
            return this;
        }

        public JsonMessage build() {
            return new JsonMessage(this);
        }
    }

    private static String getAsStringIfParameterIsSet(String parameterName, String parameterValue,
        boolean withSeparator) {
        String result = "";
        if (parameterValue != null) {
            result = "\"" + parameterName + "\":\"" + parameterValue + "\"";

            if (withSeparator) {
                result = result + ",";
            }
        }
        return result;
    }

    /**
     * Can be used to produce a correct test Json message. Tip! Check the formatting with
     * <a href="https://jsonformatter.org/">Json formatter</a>
     *
     * @param args Not used
     */
    public static void main(String[] args) {
        AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder() //
            .name("A20161224.1030-1045.bin.gz") //
            .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz") //
            .compression("gzip") //
            .fileFormatType("org.3GPP.32.435#measCollec") //
            .fileFormatVersion("V10") //
            .build();
        AdditionalField secondAdditionalField = new JsonMessage.AdditionalFieldBuilder() //
            .name("A20161224.1030-1045.bin.gz") //
            .location("sftp://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz") //
            .compression("gzip") //
            .fileFormatType("org.3GPP.32.435#measCollec") //
            .fileFormatVersion("V10") //
            .build();
        JsonMessage message = new JsonMessage.JsonMessageBuilder() //
            .eventName("Noti_NrRadio-Ericsson_FileReady") //
            .changeIdentifier("PM_MEAS_FILES") //
            .changeType("FileReady") //
            .notificationFieldsVersion("2.0") //
            .addAdditionalField(additionalField) //
            .addAdditionalField(secondAdditionalField) //
            .build();
        System.out.println(message.toString());
    }
}