diff options
author | Rafal Wrzesniak <r.wrzesniak@partner.samsung.com> | 2021-09-08 11:25:02 +0200 |
---|---|---|
committer | Rafal Wrzesniak <r.wrzesniak@partner.samsung.com> | 2021-09-08 11:32:39 +0200 |
commit | 1ec2a8d3be78a5aba4ba311e917b8d09cf57d7e9 (patch) | |
tree | a6d3c1accb8030cc81d4354458d45fb50d4fed3b | |
parent | c7f07d7ef8d70ea520a274914c42b3eeb68bdb3e (diff) |
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 <r.wrzesniak@partner.samsung.com>
Change-Id: Ibf41a7739e5df8254649b56a6bef64be0f1057cc
Signed-off-by: Rafal Wrzesniak <r.wrzesniak@partner.samsung.com>
Issue-ID: CCSDK-3455
7 files changed, 246 insertions, 176 deletions
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<String, String> 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<String, String> 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<String, String> 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<String> 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<String, String> 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<String, String> createFaultNotificationPayloadMap(String nodeId, String counter, String timestamp, + String objectId, String problem, String severity) { + HashMap<String, String> 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<String, String> 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<String, String> notificationPayloadMapMessage); + + protected String prepareMessageFromPayloadMap(Map<String, String> payloadMapMessage, String messagePayload, + List<String> 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<String, String> mapToValidate, List<String> 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<String, String> payloadMapMessage, String payload) { + for (Map.Entry<String, String> 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<String, String> 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<String> REQUIRED_FIELDS_SSH = List.of(PROTOCOL, DEVICE_NAME, DEVICE_IP, DEVICE_PORT, USERNAME, PASSWORD); + public static List<String> REQUIRED_FIELDS_TLS = List.of(PROTOCOL, DEVICE_NAME, DEVICE_IP, DEVICE_PORT, USERNAME, KEY_ID); + private static final String SSH_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n" - + " <node-id>@device-name@</node-id>\n" - + " <host xmlns=\"urn:opendaylight:netconf-node-topology\">@device-ip@</host>\n" - + " <port xmlns=\"urn:opendaylight:netconf-node-topology\">@device-port@</port>\n" - + " <username xmlns=\"urn:opendaylight:netconf-node-topology\">@username@</username>\n" - + " <password xmlns=\"urn:opendaylight:netconf-node-topology\">@password@</password>\n" + + " <node-id>" + DEVICE_NAME + "</node-id>\n" + + " <host xmlns=\"urn:opendaylight:netconf-node-topology\">" + DEVICE_IP + "</host>\n" + + " <port xmlns=\"urn:opendaylight:netconf-node-topology\">" + DEVICE_PORT + "</port>\n" + + " <username xmlns=\"urn:opendaylight:netconf-node-topology\">" + USERNAME + "</username>\n" + + " <password xmlns=\"urn:opendaylight:netconf-node-topology\">" + PASSWORD + "</password>\n" + " <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n" + " <!-- non-mandatory fields with default values, you can safely remove these if you do not wish to override any of these values-->\n" + " <reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema>\n" @@ -54,15 +55,14 @@ public class PNFMountPointClient extends BaseHTTPClient { + " <!-- keepalive-delay set to 0 turns off keepalives-->\n" + " <keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n" + "</node>"; - // @formatter:on - // @formatter:off + private static final String TLS_PAYLOAD = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">\n" - + " <node-id>@device-name@</node-id>\n" - + " <host xmlns=\"urn:opendaylight:netconf-node-topology\">@device-ip@</host>\n" - + " <port xmlns=\"urn:opendaylight:netconf-node-topology\">@device-port@</port>\n" + + " <node-id>" + DEVICE_NAME + "</node-id>\n" + + " <host xmlns=\"urn:opendaylight:netconf-node-topology\">" + DEVICE_IP + "</host>\n" + + " <port xmlns=\"urn:opendaylight:netconf-node-topology\">" + DEVICE_PORT + "</port>\n" + " <key-based xmlns=\"urn:opendaylight:netconf-node-topology\">\n" - + " <username xmlns=\"urn:opendaylight:netconf-node-topology\">@username@</username>\n" - + " <key-id>@key-id@</key-id>\n" + + " <username xmlns=\"urn:opendaylight:netconf-node-topology\">" + USERNAME + "</username>\n" + + " <key-id>" + KEY_ID + "</key-id>\n" + " </key-based>\n" + " <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>\n" + " <protocol xmlns=\"urn:opendaylight:netconf-node-topology\">\n" @@ -77,81 +77,48 @@ public class PNFMountPointClient extends BaseHTTPClient { + "<!-- keepalive-delay set to 0 turns off keepalives-->\n" + "<keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">120</keepalive-delay>\n" + "</node>"; - // @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<String, String> 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.*>)(.*)(<\\/key-id>)"; - } else { - REGEX = "(<password.*>)(.*)(<\\/password>)"; - } - Pattern p = Pattern.compile(REGEX, Pattern.MULTILINE); - Matcher matcher = p.matcher(message); - return matcher.replaceAll("$1*********$3"); + public static Map<String, String> createPNFNotificationPayloadMap(@NonNull String pnfName, @NonNull String ipAddress, + @NonNull String commPort, @NonNull String protocol, + String username, String password, String keyId) { + HashMap<String, String> 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<String, String> 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<String, String> 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 |