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
14 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
16 import java.io.IOException;
17 import java.util.Base64;
18 import java.util.HashMap;
19 import java.util.List;
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;
28 public abstract class MessageClient extends BaseHTTPClient {
30 private static final Logger LOG = LoggerFactory.getLogger(MessageClient.class);
31 protected final Map<String, String> headerMap;
32 private String notificationUri;
34 protected enum SendMethod {
38 protected enum MessageType {
42 public MessageClient(String baseUrl, String notificationUri) {
44 setNotificationUri(notificationUri);
45 headerMap = new HashMap<>();
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())));
54 public abstract String prepareMessageFromPayloadMap(Map<String, String> notificationPayloadMapMessage);
56 protected String prepareMessageFromPayloadMap(Map<String, String> payloadMapMessage, String messagePayload,
57 List<String> requiredFields) {
59 if (inputMapHasAllRequiredFields(payloadMapMessage, requiredFields)) {
60 message = insertValuesToPayload(payloadMapMessage, messagePayload);
62 LOG.warn("Input map is missing required fields.");
67 private boolean inputMapHasAllRequiredFields(Map<String, String> mapToValidate, List<String> requiredFields) {
68 if (mapToValidate == null || mapToValidate.isEmpty()) {
71 for (String requiredField : requiredFields) {
72 if (!mapToValidate.containsKey(requiredField)) {
73 LOG.warn("Missing required field {}", requiredField);
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");
88 public abstract boolean sendNotification(String message);
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;
96 response = sendRequest(notificationUri, method.toString(), message, headerMap);
97 } catch (IOException e) {
98 LOG.warn("Problem sending fault message: {}", e.getMessage());
101 LOG.debug("Finished with response code {}", response.code);
102 return response.isSuccess();
105 protected void setNotificationUri(String notificationUri) {
106 this.notificationUri = notificationUri;
109 private String redactMessage(String message) {
111 if (message.contains("<key-id")) {
112 REGEX = "(<key-id.*>)(.*)(<\\/key-id>)";
113 } else if (message.contains("<password")) {
114 REGEX = "(<password.*>)(.*)(<\\/password>)";
118 Pattern p = Pattern.compile(REGEX, Pattern.MULTILINE);
119 Matcher matcher = p.matcher(message);
120 return matcher.replaceAll("$1*********$3");