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 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;
26 public abstract class MessageClient extends BaseHTTPClient {
28 private static final Logger LOG = LoggerFactory.getLogger(MessageClient.class);
29 protected final Map<String, String> headerMap;
30 private String notificationUri;
32 protected enum SendMethod {
36 protected enum MessageType {
40 public MessageClient(String baseUrl, String notificationUri) {
42 setNotificationUri(notificationUri);
43 headerMap = new HashMap<>();
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())));
52 public abstract String prepareMessageFromPayloadMap(Map<String, String> notificationPayloadMapMessage);
54 protected String prepareMessageFromPayloadMap(Map<String, String> payloadMapMessage, String messagePayload,
55 List<String> requiredFields) {
57 if (inputMapHasAllRequiredFields(payloadMapMessage, requiredFields)) {
58 message = insertValuesToPayload(payloadMapMessage, messagePayload);
60 LOG.warn("Input map is missing required fields.");
65 private boolean inputMapHasAllRequiredFields(Map<String, String> mapToValidate, List<String> requiredFields) {
66 if (mapToValidate == null || mapToValidate.isEmpty()) {
69 for (String requiredField : requiredFields) {
70 if (!mapToValidate.containsKey(requiredField)) {
71 LOG.warn("Missing required field {}", requiredField);
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");
86 public abstract boolean sendNotification(String message);
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;
94 response = sendRequest(notificationUri, method.toString(), message, headerMap);
95 } catch (IOException e) {
96 LOG.warn("Problem sending fault message: {}", e.getMessage());
99 LOG.debug("Finished with response code {}", response.code);
100 return response.isSuccess();
103 protected void setNotificationUri(String notificationUri) {
104 this.notificationUri = notificationUri;