584982a5baa037bca0381d1a000150e216faa042
[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 java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
24 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public abstract class MessageClient extends BaseHTTPClient {
29
30     private static final Logger LOG = LoggerFactory.getLogger(MessageClient.class);
31     protected final Map<String, String> headerMap;
32     private String notificationUri;
33
34     protected enum SendMethod {
35         PUT, POST
36     }
37
38     protected enum MessageType {
39         xml, json
40     }
41
42     public MessageClient(String baseUrl, String notificationUri) {
43         super(baseUrl);
44         setNotificationUri(notificationUri);
45         headerMap = new HashMap<>();
46     }
47
48     public void setAuthorization(String username, String password) {
49         String credentials = username + ":" + password;
50         headerMap.put("Authorization", "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
51     }
52
53
54     public abstract String prepareMessageFromPayloadMap(Map<String, String> notificationPayloadMapMessage);
55
56     protected String prepareMessageFromPayloadMap(Map<String, String> payloadMapMessage, String messagePayload,
57                                                   List<String> requiredFields) {
58         String message = "";
59         if (inputMapHasAllRequiredFields(payloadMapMessage, requiredFields)) {
60             message = insertValuesToPayload(payloadMapMessage, messagePayload);
61         } else {
62             LOG.warn("Input map is missing required fields.");
63         }
64         return message;
65     }
66
67     private boolean inputMapHasAllRequiredFields(Map<String, String> mapToValidate, List<String> requiredFields) {
68         if (mapToValidate == null || mapToValidate.isEmpty()) {
69             return false;
70         }
71         for (String requiredField : requiredFields) {
72             if (!mapToValidate.containsKey(requiredField)) {
73                 LOG.warn("Missing required field {}", requiredField);
74                 return false;
75             }
76         }
77         return true;
78     }
79
80     private String insertValuesToPayload(Map<String, String> payloadMapMessage, String payload) {
81         for (Map.Entry<String, String> entry : payloadMapMessage.entrySet()) {
82             payload = payload.replace(entry.getKey(), entry.getValue() != null ? entry.getValue() : "null");
83         }
84         return payload;
85     }
86
87
88     public abstract boolean sendNotification(String message);
89
90     protected boolean sendNotification(String message, SendMethod method, MessageType messageType) {
91         LOG.debug("In sendRequestNotification - {}-{}", method, redactMessage(message));
92         headerMap.put("Content-Type", "application/".concat(messageType.toString()));
93         headerMap.put("Accept", "application/".concat(messageType.toString()));
94         BaseHTTPResponse response;
95         try {
96             response = sendRequest(notificationUri, method.toString(), message, headerMap);
97         } catch (IOException e) {
98             LOG.warn("Problem sending fault message: {}", e.getMessage());
99             return false;
100         }
101         LOG.debug("Finished with response code {}", response.code);
102         return response.isSuccess();
103     }
104
105     protected void setNotificationUri(String notificationUri) {
106         this.notificationUri = notificationUri;
107     }
108
109     private String redactMessage(String message) {
110         String REGEX = "";
111         if (message.contains("<key-id")) {
112             REGEX = "(<key-id.*>)(.*)(<\\/key-id>)";
113         } else if (message.contains("<password")) {
114             REGEX = "(<password.*>)(.*)(<\\/password>)";
115         } else {
116             return message;
117         }
118         Pattern p = Pattern.compile(REGEX, Pattern.MULTILINE);
119         Matcher matcher = p.matcher(message);
120         return matcher.replaceAll("$1*********$3");
121     }
122
123 }