Merge "PNF Simulator SFTP support"
[integration.git] / test / mocks / pnfsimulator / src / main / java / org / onap / pnfsimulator / message / MessageProvider.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.COMMON_EVENT_HEADER;
24 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN;
25 import static org.onap.pnfsimulator.message.MessageConstants.EVENT;
26 import static org.onap.pnfsimulator.message.MessageConstants.EVENT_TYPE;
27 import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS;
28 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_PNF_REGISTRATION;
29 import static org.onap.pnfsimulator.message.MessageConstants.DOMAIN_NOTIFICATION;
30 import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS;
31
32 import java.util.Map;
33 import java.util.Optional;
34 import org.json.JSONObject;
35
36 public class MessageProvider {
37
38     public JSONObject createMessage(JSONObject commonEventHeaderParams, Optional<JSONObject> pnfRegistrationParams,
39         Optional<JSONObject> notificationParams) {
40
41         if (!pnfRegistrationParams.isPresent() && !notificationParams.isPresent()) {
42             throw new IllegalArgumentException(
43                 "Both PNF registration and notification parameters objects are not present");
44         }
45         JSONObject event = new JSONObject();
46
47         JSONObject commonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader();
48         Map<String, Object> commonEventHeaderFields = commonEventHeaderParams.toMap();
49         commonEventHeaderFields.forEach((key, value) -> {
50             commonEventHeader.put(key, value);
51         });
52
53         JSONObject pnfRegistrationFields = JSONObjectFactory.generatePnfRegistrationFields();
54         pnfRegistrationParams.ifPresent(jsonObject -> {
55             copyParametersToFields(jsonObject.toMap(), pnfRegistrationFields);
56             commonEventHeader.put(DOMAIN, DOMAIN_PNF_REGISTRATION);
57             commonEventHeader.put(EVENT_TYPE, DOMAIN_PNF_REGISTRATION);
58             event.put(PNF_REGISTRATION_FIELDS, pnfRegistrationFields);
59         });
60
61         JSONObject notificationFields = JSONObjectFactory.generateNotificationFields();
62         notificationParams.ifPresent(jsonObject -> {
63             copyParametersToFields(jsonObject.toMap(), notificationFields);
64             commonEventHeader.put(DOMAIN, DOMAIN_NOTIFICATION);
65             event.put(NOTIFICATION_FIELDS, notificationFields);
66         });
67
68         event.put(COMMON_EVENT_HEADER, commonEventHeader);
69         JSONObject root = new JSONObject();
70         root.put(EVENT, event);
71         return root;
72     }
73
74     private void copyParametersToFields(Map<String, Object> paramersMap, JSONObject fieldsJsonObject) {
75         paramersMap.forEach((key, value) -> {
76             fieldsJsonObject.put(key, value);
77         });
78     }
79
80 }