Merge "switch drools pdp image to new one"
[integration.git] / test / mocks / mass-pnf-sim / pnf-sim-lightweight / src / main / java / org / onap / pnfsimulator / message / JSONObjectFactory.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.message;
22
23 import static org.onap.pnfsimulator.message.MessageConstants.COMPRESSION;
24 import static org.onap.pnfsimulator.message.MessageConstants.COMPRESSION_VALUE;
25 import static org.onap.pnfsimulator.message.MessageConstants.EVENT_ID;
26 import static org.onap.pnfsimulator.message.MessageConstants.FILE_FORMAT_TYPE;
27 import static org.onap.pnfsimulator.message.MessageConstants.FILE_FORMAT_TYPE_VALUE;
28 import static org.onap.pnfsimulator.message.MessageConstants.FILE_FORMAT_VERSION;
29 import static org.onap.pnfsimulator.message.MessageConstants.FILE_FORMAT_VERSION_VALUE;
30 import static org.onap.pnfsimulator.message.MessageConstants.HASH_MAP;
31 import static org.onap.pnfsimulator.message.MessageConstants.INTERNAL_HEADER_FIELDS;
32 import static org.onap.pnfsimulator.message.MessageConstants.LAST_EPOCH_MICROSEC;
33 import static org.onap.pnfsimulator.message.MessageConstants.LOCATION;
34 import static org.onap.pnfsimulator.message.MessageConstants.NAME;
35 import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS_VERSION;
36 import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS_VERSION_VALUE;
37 import static org.onap.pnfsimulator.message.MessageConstants.PNF_LAST_SERVICE_DATE;
38 import static org.onap.pnfsimulator.message.MessageConstants.PNF_MANUFACTURE_DATE;
39 import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS_VERSION;
40 import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS_VERSION_VALUE;
41 import static org.onap.pnfsimulator.message.MessageConstants.PRIORITY;
42 import static org.onap.pnfsimulator.message.MessageConstants.PRIORITY_NORMAL;
43 import static org.onap.pnfsimulator.message.MessageConstants.REPORTING_ENTITY_NAME;
44 import static org.onap.pnfsimulator.message.MessageConstants.SEQUENCE;
45 import static org.onap.pnfsimulator.message.MessageConstants.SEQUENCE_NUMBER;
46 import static org.onap.pnfsimulator.message.MessageConstants.SOURCE_NAME;
47 import static org.onap.pnfsimulator.message.MessageConstants.START_EPOCH_MICROSEC;
48 import static org.onap.pnfsimulator.message.MessageConstants.TIME_ZONE_OFFSET;
49 import static org.onap.pnfsimulator.message.MessageConstants.VERSION;
50 import static org.onap.pnfsimulator.message.MessageConstants.VERSION_NUMBER;
51 import static org.onap.pnfsimulator.message.MessageConstants.VES_EVENT_LISTENER_VERSION;
52 import static org.onap.pnfsimulator.message.MessageConstants.VES_EVENT_LISTENER_VERSION_NUMBER;
53 import java.io.File;
54 import java.util.List;
55 import java.util.TimeZone;
56 import org.json.JSONArray;
57 import org.json.JSONObject;
58
59 final class JSONObjectFactory {
60
61     static JSONObject generateConstantCommonEventHeader() {
62         JSONObject commonEventHeader = new JSONObject();
63         long timestamp = System.currentTimeMillis();
64         commonEventHeader.put(EVENT_ID, generateEventId());
65         commonEventHeader.put(TIME_ZONE_OFFSET, generateTimeZone(timestamp));
66         commonEventHeader.put(LAST_EPOCH_MICROSEC, timestamp);
67         commonEventHeader.put(PRIORITY, PRIORITY_NORMAL);
68         commonEventHeader.put(SEQUENCE, SEQUENCE_NUMBER);
69         commonEventHeader.put(START_EPOCH_MICROSEC, timestamp);
70         commonEventHeader.put(INTERNAL_HEADER_FIELDS, new JSONObject());
71         commonEventHeader.put(VERSION, VERSION_NUMBER);
72         commonEventHeader.put(VES_EVENT_LISTENER_VERSION, VES_EVENT_LISTENER_VERSION_NUMBER);
73         String absPath = System.getProperty("user.dir");
74         String nodeName = absPath.substring(absPath.lastIndexOf(File.separator)+1);
75         commonEventHeader.put(SOURCE_NAME, nodeName);
76         commonEventHeader.put(REPORTING_ENTITY_NAME, nodeName);
77         return commonEventHeader;
78     }
79
80     static JSONObject generatePnfRegistrationFields() {
81         JSONObject pnfRegistrationFields = new JSONObject();
82         pnfRegistrationFields.put(PNF_REGISTRATION_FIELDS_VERSION, PNF_REGISTRATION_FIELDS_VERSION_VALUE);
83         pnfRegistrationFields.put(PNF_LAST_SERVICE_DATE, String.valueOf(System.currentTimeMillis()));
84         pnfRegistrationFields.put(PNF_MANUFACTURE_DATE, String.valueOf(System.currentTimeMillis()));
85         return pnfRegistrationFields;
86     }
87
88     static JSONObject generateNotificationFields() {
89         JSONObject notificationFields = new JSONObject();
90         notificationFields.put(NOTIFICATION_FIELDS_VERSION, NOTIFICATION_FIELDS_VERSION_VALUE);
91         return notificationFields;
92     }
93
94     static JSONArray generateArrayOfNamedHashMap(List<String> fileList, String xnfUrl) {
95         JSONArray arrayOfNamedHashMap = new JSONArray();
96
97         for (String fileName : fileList) {
98             JSONObject namedHashMap = new JSONObject();
99             namedHashMap.put(NAME, fileName);
100
101             JSONObject hashMap = new JSONObject();
102             hashMap.put(FILE_FORMAT_TYPE, FILE_FORMAT_TYPE_VALUE);
103             hashMap.put(LOCATION, xnfUrl.concat(fileName));
104             hashMap.put(FILE_FORMAT_VERSION, FILE_FORMAT_VERSION_VALUE);
105             hashMap.put(COMPRESSION, COMPRESSION_VALUE);
106             namedHashMap.put(HASH_MAP, hashMap);
107
108             arrayOfNamedHashMap.put(namedHashMap);
109         }
110
111
112         return arrayOfNamedHashMap;
113     }
114
115
116     static String generateEventId() {
117         String timeAsString = String.valueOf(System.currentTimeMillis());
118         return String.format("FileReady_%s", timeAsString);
119     }
120
121     static String generateTimeZone(long timestamp) {
122         TimeZone timeZone = TimeZone.getDefault();
123         int offsetInMillis = timeZone.getOffset(timestamp);
124         String offsetHHMM = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000),
125                 Math.abs((offsetInMillis / 60000) % 60));
126         return ("UTC" + (offsetInMillis >= 0 ? "+" : "-") + offsetHHMM);
127     }
128
129     private JSONObjectFactory() {
130
131     }
132
133 }