diff options
Diffstat (limited to 'actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools')
6 files changed, 545 insertions, 0 deletions
diff --git a/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/DefaultPncInstance.java b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/DefaultPncInstance.java new file mode 100644 index 0000000..4bb8b66 --- /dev/null +++ b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/DefaultPncInstance.java @@ -0,0 +1,127 @@ +/* + * ============LICENSE_START======================================================= + * Actn Interface Tools + * ================================================================================ + * Copyright (C) 2023 Huawei Canada Limited. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.integration.actninterfacetools.protocol.restconf; + +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; +import org.apache.commons.lang3.StringUtils; +import java.net.InetAddress; +import java.util.Objects; +import java.util.UUID; + +/** + * Default implementation for Rest devices. + */ +public class DefaultPncInstance implements PncInstance { + private static final String REST = "rest"; + private static final String COLON = ":"; + + private final UUID pncId; + private final InetAddress ip; + private final int port; + private final String username; + private final String password; + private String protocol; + private String url; + + public DefaultPncInstance(InetAddress ip, int port, String name, String password, + String protocol, String url) { + this.pncId = UUID.randomUUID(); + Preconditions.checkNotNull(ip, "IP address cannot be null"); + Preconditions.checkArgument(port > 0, "Port address cannot be negative"); + Preconditions.checkNotNull(protocol, "protocol address cannot be null"); + this.ip = ip; + this.port = port; + this.username = name; + this.password = StringUtils.isEmpty(password) ? null : password; + this.protocol = protocol; + this.url = StringUtils.isEmpty(url) ? null : url; + } + + @Override + public InetAddress ip() { + return this.ip; + } + + @Override + public int port() { + return this.port; + } + + @Override + public String username() { + return this.username; + } + + @Override + public String password() { + return this.password; + } + + @Override + public UUID pncId() { + return this.pncId; + } + + @Override + public String protocol() { + return protocol; + } + + @Override + public String url() { + return url; + } + + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .omitNullValues() + .add("url", url) + .add("protocol", protocol) + .add("username", username) + .add("port", port) + .add("ip", ip) + .toString(); + + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof PncInstance)) { + return false; + } + PncInstance pncInstance = (PncInstance) obj; + return this.username.equals(pncInstance.username()) && this.ip.equals(pncInstance.ip()) && + this.port == pncInstance.port(); + + } + + @Override + public int hashCode() { + return Objects.hash(ip, port); + } + +} diff --git a/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/PncInstance.java b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/PncInstance.java new file mode 100644 index 0000000..a6726e8 --- /dev/null +++ b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/PncInstance.java @@ -0,0 +1,81 @@ +/* + * ============LICENSE_START======================================================= + * Actn Interface Tools + * ================================================================================ + * Copyright (C) 2023 Huawei Canada Limited. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.integration.actninterfacetools.protocol.restconf; + +import java.net.InetAddress; +import java.util.Optional; +import java.util.UUID; + +/** + * Represents an abstraction of a Pnc. + */ +public interface PncInstance { + /** + * Returns the ip of the Pnc Service. + * + * @return ip + */ + InetAddress ip(); + + /** + * Returns the port of the Pnc Service. + * + * @return port + */ + int port(); + + /** + * Returns the username of the Pnc Service. + * + * @return username + */ + String username(); + + /** + * Returns the password of the Pnc Service. + * + * @return password + */ + String password(); + + /** + * Returns the pncId. + * + * @return pncId + */ + UUID pncId(); + + + /** + * Returns the protocol for the REST request, usually HTTP o HTTPS. + * + * @return protocol + */ + String protocol(); + + /** + * Returns the url for the REST requests, to be used instead of IP and PORT. + * + * @return url + */ + String url(); + +} diff --git a/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/RestConfSBController.java b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/RestConfSBController.java new file mode 100644 index 0000000..86cfbc6 --- /dev/null +++ b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/RestConfSBController.java @@ -0,0 +1,216 @@ +/* + * ============LICENSE_START======================================================= + * Actn Interface Tools + * ================================================================================ + * Copyright (C) 2023 Huawei Canada Limited. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.integration.actninterfacetools.protocol.restconf; + +import com.fasterxml.jackson.databind.node.ObjectNode; + +import javax.ws.rs.core.MediaType; +import java.net.InetAddress; +import java.util.Map; +import java.util.UUID; + +/** + * Abstraction of a RESTCONF controller. Serves as a one-stop shop for obtaining + * RESTCONF southbound devices and (un)register listeners. + */ +public interface RestConfSBController { + /** + * Returns all the devices known to this controller. + * + * @return map of devices + */ + Map<UUID, PncInstance> getPncInstances(); + + /** + * Returns a device by node identifier. + * + * @param deviceInfo node identifier + * @return RestSBDevice rest device + */ + PncInstance getPncInstance(UUID deviceInfo); + + /** + * Returns a device by Ip and Port. + * + * @param ip device ip + * @param port device port + * @return RestSBDevice rest device + */ + PncInstance getPncInstance(InetAddress ip, int port); + + /** + * Adds a device to the device map. + * + * @param pncInstance to be added + */ + void addPncInstance(PncInstance pncInstance); + + /** + * Removes the device from the devices map. + * + * @param pncId to be removed + */ + void removeDevice(UUID pncId); + /** + * This method is to be called by whoever is interested to receive + * Notifications from a specific device. It does a REST GET request + * with specified parameters to the device, and calls the provided + * callBackListener upon receiving notifications to notify the requester + * about notifications. + * + * @param pncId pnc to make the request to + * @param request url of the request + * @param mediaType format to retrieve the content in + * @param callBackListener method to call when notifications arrives + */ + void enableNotifications(UUID pncId, String request, String mediaType, + RestconfNotificationEventListener callBackListener); + + /** + * Registers a listener for notification events that occur to restconf + * devices. + * + * @param pncId identifier of the pnc to which the listener is attached + * @param listener the listener to notify + */ + void addNotificationListener(UUID pncId, + RestconfNotificationEventListener listener); + + /** + * Unregisters the listener for the device. + * + * @param pncId identifier of the pnc for which the listener + * is to be removed + * @param listener listener to be removed + */ + void removeNotificationListener(UUID pncId, + RestconfNotificationEventListener listener); + + /** + * Returns true if a listener has been installed to listen to RESTCONF + * notifications sent from a particular device. + * + * @param pncId identifier of the pnc from which the notifications + * are generated + * @return true if listener is installed; false otherwise + */ + boolean isNotificationEnabled(UUID pncId); + + /** + * HTTP POST request with specified parameters to the pnc. + * + * @param pncId pnc to make the request to + * @param request url of the request + * @param payload payload of the request as an ObjectNode + * @param mediaType type of content in the payload i.e. application/json + * @return status Commonly used status codes defined by HTTP + */ + int post(UUID pncId, String request, ObjectNode payload, MediaType mediaType); + /** + * HTTP POST request with specified parameters to the pnc. + * + * @param pncId pnc to make the request to + * @param request url of the request + * @param payload payload of the request as an ObjectNode + * @param mediaType type of content in the payload in the form of string i.e. "application/json" + * @return true if status defined by HTTP is OK, CREATED, ACCEPTED or PARTIAL-CONTEND, otherwise return false + */ + boolean post(UUID pncId, String request, ObjectNode payload, String mediaType); + + <T> T post(UUID pncId, String request, ObjectNode payload, String mediaType, Class<T> responseClass); + + <T> T post(UUID pncId, String request, ObjectNode payload, MediaType mediaType, Class<T> responseClass); + + /** + * HTTP PUT request with specified parameters to the device. + * + * @param pncId pnc to make the request to + * @param request resource path of the request + * @param payload payload of the request as an ObjectNode + * @param mediaType type of content in the payload i.e. application/json + * @return status Commonly used status codes defined by HTTP + */ + int put(UUID pncId, String request, ObjectNode payload, MediaType mediaType); + + /** + * HTTP PUT request with specified parameters to the pnc. + * + * @param pncId pnc to make the request to + * @param request url of the request + * @param payload payload of the request as an ObjectNode + * @param mediaType type of content in the payload in the form of string i.e. "application/json" + * @return true if status defined by HTTP is OK, CREATED, ACCEPTED or PARTIAL-CONTEND, otherwise return false + */ + boolean put(UUID pncId, String request, ObjectNode payload, String mediaType); + + /** + * HTTP PATCH request with specified parameters to the device. + * + * @param pncId pnc to make the request to + * @param request url of the request + * @param payload payload of the request as an ObjectNode + * @param mediaType format to retrieve the content in + * @return status Commonly used status codes defined by HTTP + */ + int patch(UUID pncId, String request, ObjectNode payload, MediaType mediaType); + + /** + * HTTP PATCH request with specified parameters to the pnc. + * + * @param pncId pnc to make the request to + * @param request url of the request + * @param payload payload of the request as an ObjectNode + * @param mediaType type of content in the payload i.e. application/json + * @return status Commonly used status codes defined by HTTP + */ + boolean patch(UUID pncId, String request, ObjectNode payload, String mediaType); + + /** + * HTTP DELETE request with specified parameters to the device. + * + * @param pncId pnc to make the request to + * @param request url of the request + * @param mediaType type of content in the payload i.e. application/json + * @return status Commonly used status codes defined by HTTP + */ + int delete(UUID pncId, String request, MediaType mediaType); + + /** + * HTTP DELETE request with specified parameters to the pnc. + * + * @param pncId pnc to make the request to + * @param request url of the request + * @param mediaType type of content in the payload i.e. application/json + * @return status Commonly used status codes defined by HTTP + */ + boolean delete(UUID pncId, String request, String mediaType); + + /** + * + * HTTP GET request with specified parameters to the device. + * + * @param pncId pnc to make the request to + * @param request url of the request + * @param mediaType format to retrieve the content in + * @return an ObjectNode of data from the reply. + */ + ObjectNode get(UUID pncId, String request, String mediaType); + +} diff --git a/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/RestconfNotificationEventListener.java b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/RestconfNotificationEventListener.java new file mode 100644 index 0000000..70eb2d6 --- /dev/null +++ b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/RestconfNotificationEventListener.java @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START======================================================= + * Actn Interface Tools + * ================================================================================ + * Copyright (C) 2023 Huawei Canada Limited. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.integration.actninterfacetools.protocol.restconf; + +import java.util.UUID; + +/** + * Notifies providers about incoming RESTCONF notification events. + */ +public interface RestconfNotificationEventListener<T> { + + /** + * Handles the notification event. + * + * @param pncId restconf device identifier + * @param event event payload + */ + void handleNotificationEvent(UUID pncId, T event); +} diff --git a/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/WebClientUtil.java b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/WebClientUtil.java new file mode 100644 index 0000000..066d4bd --- /dev/null +++ b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/WebClientUtil.java @@ -0,0 +1,61 @@ +/* + * ============LICENSE_START======================================================= + * Actn Interface Tools + * ================================================================================ + * Copyright (C) 2023 Huawei Canada Limited. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.integration.actninterfacetools.protocol.restconf; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class WebClientUtil { + + protected static final Logger LOGGER = LoggerFactory.getLogger(WebClientUtil.class); + + private WebClientUtil() { + } + + public static String resetContentRange(String range) { + String rangeResult = ""; + if (range != null) { + final Pattern pattern = Pattern.compile("\\d+\\-\\d+/\\S+"); + final Matcher matcher = pattern.matcher(range); + if (matcher.find()) { + final String[] result = matcher.group(0).split("/"); + if (result.length >= 2) { + final String[] ranges = result[0].split("\\D"); + // 如果返回截止长度大于等于总长度-1,则认为结束 + if (isContentEnd(ranges[1], result[1])) { + return ""; + } + rangeResult = "bytes=" + (Integer.parseInt(ranges[1]) + 1) + "-" + + (result[1].equals("*") ? "" : result[1]); + } + } + } + return rangeResult; + } + + private static boolean isContentEnd(String end, String total) { + return total.matches("[0-9]*") && Integer.parseInt(end) >= Integer.parseInt(total) - 1; + } + +} diff --git a/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/package-info.java b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/package-info.java new file mode 100644 index 0000000..6551498 --- /dev/null +++ b/actn-interface-tools/restconf-client/api/src/main/java/org/onap/integration/actninterfacetools/protocol/restconf/package-info.java @@ -0,0 +1,24 @@ +/* + * ============LICENSE_START======================================================= + * Actn Interface Tools + * ================================================================================ + * Copyright (C) 2023 Huawei Canada Limited. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +/** + * RESTCONF southbound protocols libraries. + */ +package org.onap.integration.actninterfacetools.protocol.restconf; |