534dbde1b33c36afe912d18512ea95da5acc0a90
[ccsdk/features.git] /
1 /*
2  * Copyright (C) 2021 Samsung Electronics
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *      http://www.apache.org/licenses/LICENSE-2.0
7  * Unless required by applicable law or agreed to in writing, software
8  * distributed under the License is distributed on an "AS IS" BASIS,
9  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10  * See the License for the specific language governing permissions and
11  * limitations under the License
12  */
13
14 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
15
16 import java.io.IOException;
17 import java.util.Base64;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
22 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public abstract class MessageClient extends BaseHTTPClient {
27
28     private static final Logger LOG = LoggerFactory.getLogger(MessageClient.class);
29     protected final Map<String, String> headerMap;
30     private String notificationUri;
31
32     protected enum SendMethod {
33         PUT, POST
34     }
35
36     protected enum MessageType {
37         xml, json
38     }
39
40     public MessageClient(String baseUrl, String notificationUri) {
41         super(baseUrl);
42         setNotificationUri(notificationUri);
43         headerMap = new HashMap<>();
44     }
45
46     public void setAuthorization(String username, String password) {
47         String credentials = username + ":" + password;
48         headerMap.put("Authorization", "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
49     }
50
51
52     public abstract String prepareMessageFromPayloadMap(Map<String, String> notificationPayloadMapMessage);
53
54     protected String prepareMessageFromPayloadMap(Map<String, String> payloadMapMessage, String messagePayload,
55                                                   List<String> requiredFields) {
56         String message = "";
57         if (inputMapHasAllRequiredFields(payloadMapMessage, requiredFields)) {
58             message = insertValuesToPayload(payloadMapMessage, messagePayload);
59         } else {
60             LOG.warn("Input map is missing required fields.");
61         }
62         return message;
63     }
64
65     private boolean inputMapHasAllRequiredFields(Map<String, String> mapToValidate, List<String> requiredFields) {
66         if (mapToValidate == null || mapToValidate.isEmpty()) {
67             return false;
68         }
69         for (String requiredField : requiredFields) {
70             if (!mapToValidate.containsKey(requiredField)) {
71                 LOG.warn("Missing required field {}", requiredField);
72                 return false;
73             }
74         }
75         return true;
76     }
77
78     private String insertValuesToPayload(Map<String, String> payloadMapMessage, String payload) {
79         for (Map.Entry<String, String> entry : payloadMapMessage.entrySet()) {
80             payload = payload.replace(entry.getKey(), entry.getValue() != null ? entry.getValue() : "null");
81         }
82         return payload;
83     }
84
85
86     public abstract boolean sendNotification(String message);
87
88     protected boolean sendNotification(String message, SendMethod method, MessageType messageType) {
89         LOG.debug("In sendRequestNotification - {}-{}", method, message);
90         headerMap.put("Content-Type", "application/".concat(messageType.toString()));
91         headerMap.put("Accept", "application/".concat(messageType.toString()));
92         BaseHTTPResponse response;
93         try {
94             response = sendRequest(notificationUri, method.toString(), message, headerMap);
95         } catch (IOException e) {
96             LOG.warn("Problem sending fault message: {}", e.getMessage());
97             return false;
98         }
99         LOG.debug("Finished with response code {}", response.code);
100         return response.isSuccess();
101     }
102
103     protected void setNotificationUri(String notificationUri) {
104         this.notificationUri = notificationUri;
105     }
106
107
108 }