2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.message;
23 import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER;
24 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN;
25 import static org.onap.pnfsimulator.message.MessageConstants.EVENT_ID;
26 import static org.onap.pnfsimulator.message.MessageConstants.EVENT_TYPE;
27 import static org.onap.pnfsimulator.message.MessageConstants.INTERNAL_HEADER_FIELDS;
28 import static org.onap.pnfsimulator.message.MessageConstants.LAST_EPOCH_MICROSEC;
29 import static org.onap.pnfsimulator.message.MessageConstants.OTHER_FIELDS;
30 import static org.onap.pnfsimulator.message.MessageConstants.OTHER_FIELDS_VERSION;
31 import static org.onap.pnfsimulator.message.MessageConstants.PNF_LAST_SERVICE_DATE;
32 import static org.onap.pnfsimulator.message.MessageConstants.PNF_MANUFACTURE_DATE;
33 import static org.onap.pnfsimulator.message.MessageConstants.PNF_PREFIX;
34 import static org.onap.pnfsimulator.message.MessageConstants.PRIORITY;
35 import static org.onap.pnfsimulator.message.MessageConstants.SEQUENCE;
36 import static org.onap.pnfsimulator.message.MessageConstants.START_EPOCH_MICROSEC;
37 import static org.onap.pnfsimulator.message.MessageConstants.VERSION;
38 import static org.onap.pnfsimulator.message.MessageConstants.EVENT;
41 import java.util.UUID;
43 import com.google.common.base.Preconditions;
44 import org.json.JSONObject;
46 public class MessageProvider {
48 private static MessageProvider instance;
50 public static MessageProvider getInstance() {
51 if (instance == null) {
52 instance = new MessageProvider();
57 public JSONObject createMessage(JSONObject params) {
59 Preconditions.checkArgument(params != null, "Params object cannot be null");
60 Map<String, Object> paramsMap = params.toMap();
61 JSONObject root = new JSONObject();
62 JSONObject commonEventHeader = generateConstantCommonEventHeader();
63 JSONObject otherFields = generateConstantOtherFields();
65 paramsMap.forEach((key, value) -> {
67 if (key.startsWith(PNF_PREFIX)) {
68 otherFields.put(key, value);
70 commonEventHeader.put(key, value);
74 JSONObject event = new JSONObject();
75 event.put(COMMON_EVENT_HEADER, commonEventHeader);
76 event.put(OTHER_FIELDS, otherFields);
78 root.put(EVENT, event);
82 private JSONObject generateConstantCommonEventHeader() {
84 JSONObject commonEventHeader = new JSONObject();
85 long timestamp = System.currentTimeMillis();
87 commonEventHeader.put(DOMAIN, "other");
88 commonEventHeader.put(EVENT_ID, UUID.randomUUID() + "-reg");
89 commonEventHeader.put(EVENT_TYPE, "pnfRegistration");
90 commonEventHeader.put(LAST_EPOCH_MICROSEC, timestamp);
91 commonEventHeader.put(PRIORITY, "Normal");
92 commonEventHeader.put(SEQUENCE, 0);
93 commonEventHeader.put(START_EPOCH_MICROSEC, timestamp);
94 commonEventHeader.put(INTERNAL_HEADER_FIELDS, new JSONObject());
95 commonEventHeader.put(VERSION, 3);
96 commonEventHeader.put("functionalRole", "test_rola");
98 return commonEventHeader;
101 private JSONObject generateConstantOtherFields() {
103 JSONObject otherFields = new JSONObject();
105 otherFields.put(OTHER_FIELDS_VERSION, 1);
106 otherFields.put(PNF_LAST_SERVICE_DATE, System.currentTimeMillis());
107 otherFields.put(PNF_MANUFACTURE_DATE, System.currentTimeMillis());