From 1ec2a8d3be78a5aba4ba311e917b8d09cf57d7e9 Mon Sep 17 00:00:00 2001 From: Rafal Wrzesniak Date: Wed, 8 Sep 2021 11:25:02 +0200 Subject: Move common code from clients to super class Refactor existing client classes to make them more generic and ready for extensions Signed-off-by: Rafal Wrzesniak Change-Id: Ibf41a7739e5df8254649b56a6bef64be0f1057cc Signed-off-by: Rafal Wrzesniak Issue-ID: CCSDK-3455 --- .../impl/DMaaPFaultVESMsgConsumer.java | 12 +- .../impl/DMaaPPNFRegVESMsgConsumer.java | 36 +++-- .../impl/FaultNotificationClient.java | 94 +++++-------- .../wt/mountpointregistrar/impl/MessageClient.java | 108 +++++++++++++++ .../impl/PNFMountPointClient.java | 145 ++++++++------------- .../test/TestFaultNotificationClient.java | 13 +- .../test/TestPNFMountPointClient.java | 14 +- 7 files changed, 246 insertions(+), 176 deletions(-) create mode 100644 sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/MessageClient.java diff --git a/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/DMaaPFaultVESMsgConsumer.java b/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/DMaaPFaultVESMsgConsumer.java index 6daeb493d..e21903b72 100644 --- a/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/DMaaPFaultVESMsgConsumer.java +++ b/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/DMaaPFaultVESMsgConsumer.java @@ -3,6 +3,7 @@ * ONAP : ccsdk feature sdnr wt * ================================================================================================= * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved. * ================================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at @@ -23,6 +24,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.time.Instant; import java.time.ZoneId; +import java.util.Map; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -96,10 +98,14 @@ public class DMaaPFaultVESMsgConsumer extends DMaaPVESMsgConsumerImpl { String sdnrUser = getSDNRUser(); String sdnrPasswd = getSDNRPasswd(); - FaultNotificationClient faultClient = getFaultNotificationClient(baseUrl); + Map payloadMapMessage = FaultNotificationClient.createFaultNotificationPayloadMap(faultNodeId, + Integer.toString(faultSequence), faultOccurrenceTime, faultObjectId, faultReason, faultSeverity); + + FaultNotificationClient faultClient = new FaultNotificationClient(baseUrl); + LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd); faultClient.setAuthorization(sdnrUser, sdnrPasswd); - faultClient.sendFaultNotification(faultNodeId, Integer.toString(faultSequence), faultOccurrenceTime, - faultObjectId, faultReason, faultSeverity); + String message = faultClient.prepareMessageFromPayloadMap(payloadMapMessage); + faultClient.sendNotification(message); } catch (IOException e) { LOG.info("Cannot parse json object "); diff --git a/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/DMaaPPNFRegVESMsgConsumer.java b/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/DMaaPPNFRegVESMsgConsumer.java index 1d95ea522..d278a7317 100644 --- a/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/DMaaPPNFRegVESMsgConsumer.java +++ b/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/DMaaPPNFRegVESMsgConsumer.java @@ -3,6 +3,7 @@ * ONAP : ccsdk feature sdnr wt mountpoint-registrar * ================================================================================================= * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved. * ================================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at @@ -21,6 +22,7 @@ package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; +import java.util.Map; import org.eclipse.jdt.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -111,27 +113,35 @@ public class DMaaPPNFRegVESMsgConsumer extends DMaaPVESMsgConsumerImpl { String sdnrUser = getSDNRUser(); String sdnrPasswd = getSDNRPasswd(); - PNFMountPointClient mountpointClient = getPNFMountPointClient(baseUrl); + if (hasNullInRequiredField(pnfId, pnfIPAddress, pnfCommPort, pnfCommProtocol, pnfUsername)) { + LOG.warn("One of the mandatory fields has a null value - pnfId = {} : pnfIPAddress = {} : " + + "pnfCommProtocol = {} : pnfUsername {} : pnfCommPort {} - not invoking mountpoint creation", + pnfId, pnfIPAddress, pnfCommProtocol, pnfUsername, pnfCommPort); + return; + } + + Map payloadMap = PNFMountPointClient.createPNFNotificationPayloadMap(pnfId, pnfIPAddress, + pnfCommPort, pnfCommProtocol, pnfUsername, pnfPasswd, pnfKeyId); + + PNFMountPointClient mountPointClient = new PNFMountPointClient(baseUrl); LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd); - mountpointClient.setAuthorization(sdnrUser, sdnrPasswd); + mountPointClient.setAuthorization(sdnrUser, sdnrPasswd); + String message = mountPointClient.prepareMessageFromPayloadMap(payloadMap); + mountPointClient.sendNotification(message); - if ((null != pnfId) && null != pnfIPAddress && (null != pnfCommProtocol) && (null != pnfUsername) - && (null != pnfCommPort)) { - mountpointClient.pnfMountPointCreate(pnfId, pnfIPAddress, pnfCommProtocol, pnfKeyId, pnfUsername, - pnfPasswd, pnfCommPort); - } else { - LOG.warn( - "One of the mandatory fields has a null value - pnfId = {} : pnfIPAddress = {} : pnfCommProtocol = {} : pnfUsername {} : " - + "pnfCommPort {}", - pnfId, pnfIPAddress, pnfCommProtocol, pnfUsername, pnfCommPort, - "- not invoking mountpoint creation"); - } } catch (IOException e) { LOG.info("Cannot parse json object, ignoring the received PNF Registration VES Message. Reason: {}", e.getMessage()); } } + private boolean hasNullInRequiredField(String pnfId, String pnfIPAddress, String pnfCommPort, + String pnfCommProtocol, String pnfUsername) { + + return pnfId == null || pnfIPAddress == null || pnfCommProtocol == null || + pnfCommPort == null || pnfUsername == null; + } + private String getPNFIPAddress(JsonNode dmaapMessageRootNode) { String ipAddress = dmaapMessageRootNode.at("/event/pnfRegistrationFields/oamV6IpAddress").textValue(); if (ipAddress != null && ipAddress != "") diff --git a/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/FaultNotificationClient.java b/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/FaultNotificationClient.java index 11f46fff7..6851475e8 100644 --- a/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/FaultNotificationClient.java +++ b/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/FaultNotificationClient.java @@ -3,6 +3,7 @@ * ONAP : ccsdk feature sdnr wt * ================================================================================================= * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved. * ================================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at @@ -18,86 +19,57 @@ package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl; -import java.io.IOException; -import java.util.Base64; import java.util.HashMap; +import java.util.List; import java.util.Map; -import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient; -import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -public class FaultNotificationClient extends BaseHTTPClient { +import static org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.MessageClient.MessageType.*; +import static org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.MessageClient.SendMethod.*; + + +public class FaultNotificationClient extends MessageClient { - private static final Logger LOG = LoggerFactory.getLogger(FaultNotificationClient.class); private static final String FAULT_NOTIFICATION_URI = "restconf/operations/devicemanager:push-fault-notification"; - private final Map headerMap; + public static final String NODE_ID = "@node-id@", COUNTER = "@counter@", TIMESTAMP = "@timestamp@", + OBJECT_ID = "@object-id@", PROBLEM = "@problem@", SEVERITY = "@severity@"; + public static final List REQUIRED_FIELDS = List.of(NODE_ID, COUNTER, TIMESTAMP, OBJECT_ID, PROBLEM, SEVERITY); - // @formatter:off private static final String FAULT_PAYLOAD = "{\n" + " \"devicemanager:input\": {\n" - + " \"devicemanager:node-id\": \"@node-id@\",\n" - + " \"devicemanager:counter\": \"@counter@\",\n" - + " \"devicemanager:timestamp\": \"@timestamp@\",\n" - + " \"devicemanager:object-id\": \"@object-id@\",\n" - + " \"devicemanager:problem\": \"@problem@\",\n" - + " \"devicemanager:severity\": \"@severity@\"\n" + + " \"devicemanager:node-id\": \"" + NODE_ID + "\",\n" + + " \"devicemanager:counter\": \"" + COUNTER + "\",\n" + + " \"devicemanager:timestamp\": \"" + TIMESTAMP + "\",\n" + + " \"devicemanager:object-id\": \"" + OBJECT_ID + "\",\n" + + " \"devicemanager:problem\": \"" + PROBLEM + "\",\n" + + " \"devicemanager:severity\": \"" + SEVERITY + "\"\n" + " }\n" + "}"; - // @formatter:on public FaultNotificationClient(String baseUrl) { - super(baseUrl); - - this.headerMap = new HashMap<>(); - this.headerMap.put("Content-Type", "application/json"); - this.headerMap.put("Accept", "application/json"); + super(baseUrl, FAULT_NOTIFICATION_URI); } - public void setAuthorization(String username, String password) { - String credentials = username + ":" + password; - this.headerMap.put("Authorization", "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes()))); - + @Override + public String prepareMessageFromPayloadMap(Map notificationPayloadMap) { + return super.prepareMessageFromPayloadMap(notificationPayloadMap, FAULT_PAYLOAD, REQUIRED_FIELDS); } - public boolean sendFaultNotification(String faultNodeId, String faultCounter, String faultOccurrenceTime, - String faultObjectId, String faultReason, String faultSeverity) { - String message = ""; - - message = updateFaultPayload(faultNodeId, faultCounter, faultOccurrenceTime, faultObjectId, faultReason, - faultSeverity); - - LOG.debug("Payload after updating values is: {}",message); - - return sendFaultRequest("POST", message) == 200; - + @Override + public boolean sendNotification(String message) { + return super.sendNotification(message, POST, json); } - private static String updateFaultPayload(String faultNodeId, String faultCounter, String faultOccurrenceTime, - String faultObjectId, String faultReason, String faultSeverity) { - // @formatter:off - return FAULT_PAYLOAD.replace("@node-id@", faultNodeId) - .replace("@counter@", faultCounter) - .replace("@timestamp@", faultOccurrenceTime) - .replace("@object-id@", faultObjectId) - .replace("@problem@", faultReason) - .replace("@severity@", faultSeverity); - // @formatter:on + public static Map createFaultNotificationPayloadMap(String nodeId, String counter, String timestamp, + String objectId, String problem, String severity) { + HashMap map = new HashMap<>(); + map.put(NODE_ID, nodeId); + map.put(COUNTER, counter); + map.put(TIMESTAMP, timestamp); + map.put(OBJECT_ID, objectId); + map.put(PROBLEM, problem); + map.put(SEVERITY, severity); + return map; } - - private int sendFaultRequest(String method, String message) { - LOG.debug("In sendFaultRequest - {}-{}",method,message); - BaseHTTPResponse response; - try { - String uri = FAULT_NOTIFICATION_URI; - response = this.sendRequest(uri, method, message, headerMap); - LOG.debug("finished with responsecode {}", response.code); - return response.code; - } catch (IOException e) { - LOG.warn("problem sending fault message {}", e.getMessage()); - return -1; - } - } } diff --git a/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/MessageClient.java b/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/MessageClient.java new file mode 100644 index 000000000..534dbde1b --- /dev/null +++ b/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/MessageClient.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl; + +import java.io.IOException; +import java.util.Base64; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient; +import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class MessageClient extends BaseHTTPClient { + + private static final Logger LOG = LoggerFactory.getLogger(MessageClient.class); + protected final Map headerMap; + private String notificationUri; + + protected enum SendMethod { + PUT, POST + } + + protected enum MessageType { + xml, json + } + + public MessageClient(String baseUrl, String notificationUri) { + super(baseUrl); + setNotificationUri(notificationUri); + headerMap = new HashMap<>(); + } + + public void setAuthorization(String username, String password) { + String credentials = username + ":" + password; + headerMap.put("Authorization", "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes()))); + } + + + public abstract String prepareMessageFromPayloadMap(Map notificationPayloadMapMessage); + + protected String prepareMessageFromPayloadMap(Map payloadMapMessage, String messagePayload, + List requiredFields) { + String message = ""; + if (inputMapHasAllRequiredFields(payloadMapMessage, requiredFields)) { + message = insertValuesToPayload(payloadMapMessage, messagePayload); + } else { + LOG.warn("Input map is missing required fields."); + } + return message; + } + + private boolean inputMapHasAllRequiredFields(Map mapToValidate, List requiredFields) { + if (mapToValidate == null || mapToValidate.isEmpty()) { + return false; + } + for (String requiredField : requiredFields) { + if (!mapToValidate.containsKey(requiredField)) { + LOG.warn("Missing required field {}", requiredField); + return false; + } + } + return true; + } + + private String insertValuesToPayload(Map payloadMapMessage, String payload) { + for (Map.Entry entry : payloadMapMessage.entrySet()) { + payload = payload.replace(entry.getKey(), entry.getValue() != null ? entry.getValue() : "null"); + } + return payload; + } + + + public abstract boolean sendNotification(String message); + + protected boolean sendNotification(String message, SendMethod method, MessageType messageType) { + LOG.debug("In sendRequestNotification - {}-{}", method, message); + headerMap.put("Content-Type", "application/".concat(messageType.toString())); + headerMap.put("Accept", "application/".concat(messageType.toString())); + BaseHTTPResponse response; + try { + response = sendRequest(notificationUri, method.toString(), message, headerMap); + } catch (IOException e) { + LOG.warn("Problem sending fault message: {}", e.getMessage()); + return false; + } + LOG.debug("Finished with response code {}", response.code); + return response.isSuccess(); + } + + protected void setNotificationUri(String notificationUri) { + this.notificationUri = notificationUri; + } + + +} diff --git a/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/PNFMountPointClient.java b/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/PNFMountPointClient.java index b271e8a6d..961f7fef3 100644 --- a/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/PNFMountPointClient.java +++ b/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/impl/PNFMountPointClient.java @@ -3,6 +3,7 @@ * ONAP : ccsdk feature sdnr wt * ================================================================================================= * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved. * ================================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at @@ -18,32 +19,32 @@ package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl; -import java.io.IOException; -import java.util.Base64; -import java.util.HashMap; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.eclipse.jdt.annotation.NonNull; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.BaseRequest; -import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient; -import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -public class PNFMountPointClient extends BaseHTTPClient { +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.MessageClient.MessageType.*; +import static org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.MessageClient.SendMethod.PUT; + +public class PNFMountPointClient extends MessageClient { - private static final Logger LOG = LoggerFactory.getLogger(PNFMountPointClient.class); private static final String MOUNTPOINT_URI = "restconf/config/network-topology:network-topology/topology/topology-netconf/node/"; - private final Map headerMap; - // @formatter:off + public static final String DEVICE_NAME = "@device-name@", DEVICE_IP = "@device-ip@", DEVICE_PORT = "@device-port@", + USERNAME = "@username@", PASSWORD = "@password@", KEY_ID = "@key-id@"; + private static final String PROTOCOL = "protocol"; + public static List REQUIRED_FIELDS_SSH = List.of(PROTOCOL, DEVICE_NAME, DEVICE_IP, DEVICE_PORT, USERNAME, PASSWORD); + public static List REQUIRED_FIELDS_TLS = List.of(PROTOCOL, DEVICE_NAME, DEVICE_IP, DEVICE_PORT, USERNAME, KEY_ID); + private static final String SSH_PAYLOAD = "\n" - + " @device-name@\n" - + " @device-ip@\n" - + " @device-port@\n" - + " @username@\n" - + " @password@\n" + + " " + DEVICE_NAME + "\n" + + " " + DEVICE_IP + "\n" + + " " + DEVICE_PORT + "\n" + + " " + USERNAME + "\n" + + " " + PASSWORD + "\n" + " false\n" + " \n" + " false\n" @@ -54,15 +55,14 @@ public class PNFMountPointClient extends BaseHTTPClient { + " \n" + " 120\n" + ""; - // @formatter:on - // @formatter:off + private static final String TLS_PAYLOAD = "\n" - + " @device-name@\n" - + " @device-ip@\n" - + " @device-port@\n" + + " " + DEVICE_NAME + "\n" + + " " + DEVICE_IP + "\n" + + " " + DEVICE_PORT + "\n" + " \n" - + " @username@\n" - + " @key-id@\n" + + " " + USERNAME + "\n" + + " " + KEY_ID + "\n" + " \n" + " false\n" + " \n" @@ -77,81 +77,48 @@ public class PNFMountPointClient extends BaseHTTPClient { + "\n" + "120\n" + ""; - // @formatter:on - public PNFMountPointClient(String baseUrl) { - super(baseUrl); - - this.headerMap = new HashMap<>(); - this.headerMap.put("Content-Type", "application/xml"); - this.headerMap.put("Accept", "application/xml"); - } - - public void setAuthorization(String username, String password) { - String credentials = username + ":" + password; - this.headerMap.put("Authorization", "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes()))); + public PNFMountPointClient(String baseUrl) { + super(baseUrl, MOUNTPOINT_URI); } - public boolean pnfMountPointCreate(@NonNull String pnfName, @NonNull String ipAddress, @NonNull String protocol, String keyId, - String username, String password, @NonNull String commPort) { + @Override + public String prepareMessageFromPayloadMap(Map notificationPayloadMap) { + updateNotificationUriWithPnfName(notificationPayloadMap.get(DEVICE_NAME)); String message = ""; - if (protocol.equals("TLS")) { - message = updateTLSPayload(pnfName, ipAddress, username, keyId, commPort); - } else { //SSH - message = updatePayload(pnfName, ipAddress, username, password, commPort); + if(!notificationPayloadMap.containsKey(PROTOCOL)) { + return message; } - LOG.debug("Payload after updating values is: {}", redactMessage(message, protocol)); - return pnfRequest(pnfName, "PUT", message) == 200; - - } - - private static String updatePayload(String pnfName, String ipAddress, String username, String password, - String portNo) { - // @formatter:off - return SSH_PAYLOAD.replace("@device-name@", pnfName) - .replace("@device-ip@", ipAddress) - .replace("@device-port@", portNo) - .replace("@username@", username) - .replace("@password@", password); - // @formatter:on + if(notificationPayloadMap.get(PROTOCOL).equals("SSH")) { + message = super.prepareMessageFromPayloadMap(notificationPayloadMap, SSH_PAYLOAD, REQUIRED_FIELDS_SSH); + } else if(notificationPayloadMap.get(PROTOCOL).equals("TLS")) { + message = super.prepareMessageFromPayloadMap(notificationPayloadMap, TLS_PAYLOAD, REQUIRED_FIELDS_TLS); + } + return message; } - private static String updateTLSPayload(String pnfName, String ipAddress, String username, String keyId, - String portNo) { - // @formatter:off - return TLS_PAYLOAD.replace("@device-name@", pnfName) - .replace("@device-ip@", ipAddress) - .replace("@username@", username) - .replace("@key-id@", keyId) - .replace("@device-port@", portNo); - // @formatter:on + private void updateNotificationUriWithPnfName(String pnfName) { + setNotificationUri(MOUNTPOINT_URI + BaseRequest.urlEncodeValue(pnfName)); } - private int pnfRequest(String pnfName, String method, String message) { - LOG.info("In pnfRequest - {} : {} ", pnfName, method); - BaseHTTPResponse response; - try { - String uri = MOUNTPOINT_URI + BaseRequest.urlEncodeValue(pnfName); - response = this.sendRequest(uri, method, message, headerMap); - LOG.debug("finished with responsecode {}", response.code); - return response.code; - } catch (IOException e) { - LOG.warn("problem registering {} : {}", pnfName, e.getMessage()); - return -1; - } + @Override + public boolean sendNotification(String message) { + return super.sendNotification(message, PUT, xml); } - private String redactMessage(String message, String protocol) { - String REGEX = ""; - if (("TLS").equals(protocol)) { - REGEX = "()(.*)(<\\/key-id>)"; - } else { - REGEX = "()(.*)(<\\/password>)"; - } - Pattern p = Pattern.compile(REGEX, Pattern.MULTILINE); - Matcher matcher = p.matcher(message); - return matcher.replaceAll("$1*********$3"); + public static Map createPNFNotificationPayloadMap(@NonNull String pnfName, @NonNull String ipAddress, + @NonNull String commPort, @NonNull String protocol, + String username, String password, String keyId) { + HashMap map = new HashMap<>(); + map.put(DEVICE_NAME, pnfName); + map.put(DEVICE_IP, ipAddress); + map.put(DEVICE_PORT, commPort); + map.put(PROTOCOL, protocol); + map.put(USERNAME, username); + map.put(PASSWORD, password); + map.put(KEY_ID, keyId); + return map; } } diff --git a/sdnr/wt/mountpoint-registrar/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/test/TestFaultNotificationClient.java b/sdnr/wt/mountpoint-registrar/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/test/TestFaultNotificationClient.java index 9fb35e6ba..fa289aa4b 100644 --- a/sdnr/wt/mountpoint-registrar/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/test/TestFaultNotificationClient.java +++ b/sdnr/wt/mountpoint-registrar/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/test/TestFaultNotificationClient.java @@ -3,6 +3,7 @@ * ONAP : ccsdk feature sdnr wt * ================================================================================================= * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved. * ================================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at @@ -18,7 +19,7 @@ package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.Map; import javax.annotation.Nonnull; @@ -41,10 +42,12 @@ public class TestFaultNotificationClient extends FaultNotificationClient { public void testFaultNotificationClient() { testClient = new TestFaultNotificationClient(); testClient.setAuthorization("admin", "admin"); - assertEquals(true, testClient.sendFaultNotification("TEST_50001", "1", "2019-11-20T09:25:19.948Z", - "SEDNKSAHQ01M01nSky01", "lossOfSignal", "Critical")); - assertEquals(true, testClient.sendFaultNotification("TEST_50001", "1", "2019-11-20T09:25:19.948Z", - "SEDNKSAHQ01M01nSky01", "lossOfSignal", "Critical")); + Map payloadMap = FaultNotificationClient.createFaultNotificationPayloadMap( + "TEST_50001", "1", "2019-11-20T09:25:19.948Z", + "SEDNKSAHQ01M01nSky01", "lossOfSignal", "Critical"); + String msg = testClient.prepareMessageFromPayloadMap(payloadMap); + assertTrue(testClient.sendNotification(msg)); + assertTrue(testClient.sendNotification(msg)); } @Override diff --git a/sdnr/wt/mountpoint-registrar/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/test/TestPNFMountPointClient.java b/sdnr/wt/mountpoint-registrar/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/test/TestPNFMountPointClient.java index ad87c20d7..9db9071ef 100644 --- a/sdnr/wt/mountpoint-registrar/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/test/TestPNFMountPointClient.java +++ b/sdnr/wt/mountpoint-registrar/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/test/TestPNFMountPointClient.java @@ -3,6 +3,7 @@ * ONAP : ccsdk feature sdnr wt * ================================================================================================= * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved. * ================================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at @@ -18,7 +19,7 @@ package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.Map; import javax.annotation.Nonnull; @@ -41,11 +42,14 @@ public class TestPNFMountPointClient extends PNFMountPointClient { public void testPNFMountPointClient() { testClient = new TestPNFMountPointClient(); testClient.setAuthorization("admin", "admin"); - assertEquals(true, - testClient.pnfMountPointCreate("TEST 50001", "127.0.0.1", "TLS", "key_id", "admin", "admin", "17380")); + Map payloadMap = PNFMountPointClient.createPNFNotificationPayloadMap( + "TEST 50001", "127.0.0.1", "TLS", "key_id", + "admin", "admin", "17380"); + String msg = testClient.prepareMessageFromPayloadMap(payloadMap); + + assertTrue(testClient.sendNotification(msg)); + assertTrue(testClient.sendNotification(msg)); - assertEquals(true, - testClient.pnfMountPointCreate("TEST_50001", "127.0.0.1", "SSH", "key_id", "admin", "admin", "17380")); } @Override -- cgit 1.2.3-korg