[VOL-4709] Expose dhcpl2relay-allocations via REST API
Change-Id: I2c912b85d3bbdbfd5de4f9e49dc1631ca8bca514
Signed-off-by: Gustavo Silva <gsilva@furukawalatam.com>
diff --git a/README.md b/README.md
index 259705d..7add9b3 100644
--- a/README.md
+++ b/README.md
@@ -51,3 +51,34 @@
}
```
+# REST API
+
+Information about the DHCP allocations are available through REST API.
+
+You can query all DHCP allocations using the endpoint "/onos/dhcpl2relay/app/allocations", e.g:
+```sh
+$ curl -u karaf:karaf 'http://localhost:8181/onos/dhcpl2relay/app/allocations'
+```
+
+You can also filter by device-id "/onos/dhcpl2relay/app/allocations/{device-id}", e.g:
+```sh
+curl -u karaf:karaf 'http://localhost:8181/onos/dhcpl2relay/app/allocations/of%3A00000a0a0a0a0a0a'
+```
+
+These commands will output a JSON representation of the allocations, e.g:
+```sh
+{
+ "entries": [
+ {
+ "subscriberId": "BBSM000a0001-1",
+ "connectPoint": "of:00000a0a0a0a0a0a/256",
+ "state": "DHCPACK",
+ "macAddress": "2E:0A:00:01:00:00",
+ "vlanId": 900,
+ "circuitId": "BBSM000a0001-1",
+ "ipAllocated": "10.1.0.0",
+ "allocationTimestamp": "2022-05-25T20:09:28.672454Z"
+ }
+ ]
+}
+```
diff --git a/app/pom.xml b/app/pom.xml
index 3c57714..8f009c8 100644
--- a/app/pom.xml
+++ b/app/pom.xml
@@ -38,6 +38,13 @@
<onos.app.requires>
org.opencord.sadis
</onos.app.requires>
+
+ <web.context>/onos/dhcpl2relay</web.context>
+ <api.version>1.0.0</api.version>
+ <api.title>DhcpL2Relay REST API</api.title>
+ <api.description>REST API to query DhcpL2Relay allocation entries.</api.description>
+ <api.package>org.opencord.dhcpl2relay.rest</api.package>
+
</properties>
<dependencies>
@@ -132,6 +139,19 @@
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onlab-rest</artifactId>
+ <version>${onos.version}</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>javax.ws.rs</groupId>
+ <artifactId>javax.ws.rs-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
</dependencies>
<build>
@@ -146,6 +166,15 @@
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
+ <_wab>src/main/webapp/</_wab>
+ <Include-Resource>
+ WEB-INF/classes/apidoc/swagger.json=target/swagger.json,
+ {maven-resources}
+ </Include-Resource>
+ <Import-Package>
+ *,org.glassfish.jersey.servlet
+ </Import-Package>
+ <Web-ContextPath>${web.context}</Web-ContextPath>
<Karaf-Commands>org.opencord.dhcpl2relay.cli</Karaf-Commands>
</instructions>
</configuration>
diff --git a/app/src/main/java/org/opencord/dhcpl2relay/rest/DhcpL2RelayWebResource.java b/app/src/main/java/org/opencord/dhcpl2relay/rest/DhcpL2RelayWebResource.java
new file mode 100644
index 0000000..25e607c
--- /dev/null
+++ b/app/src/main/java/org/opencord/dhcpl2relay/rest/DhcpL2RelayWebResource.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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.opencord.dhcpl2relay.rest;
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import java.util.Objects;
+import java.util.stream.Collectors;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.rest.AbstractWebResource;
+
+import org.opencord.dhcpl2relay.DhcpAllocationInfo;
+import org.opencord.dhcpl2relay.DhcpL2RelayService;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+
+/**
+ * DhcpL2Relay web resource.
+ */
+@Path("app")
+public class DhcpL2RelayWebResource extends AbstractWebResource {
+ private final ObjectNode root = mapper().createObjectNode();
+ private final ArrayNode node = root.putArray("entries");
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ private static final String SUBSCRIBER_ID = "subscriberId";
+ private static final String CONNECT_POINT = "connectPoint";
+ private static final String MAC_ADDRESS = "macAddress";
+ private static final String STATE = "state";
+ private static final String VLAN_ID = "vlanId";
+ private static final String CIRCUIT_ID = "circuitId";
+ private static final String IP_ALLOCATED = "ipAllocated";
+ private static final String ALLOCATION_TIMESTAMP = "allocationTimestamp";
+
+ /**
+ *
+ * Shows all the successful DHCP allocations relayed by the dhcpl2relay.
+ *
+ * @return 200 OK
+ */
+ @GET
+ @Path("/allocations")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getAllocations() {
+ return getAllocations(null);
+ }
+
+ /**
+ * Shows the successful DHCP allocations relayed by the dhcpl2relay for a specific access device.
+ *
+ * @param deviceId Access device ID.
+ *
+ * @return 200 OK
+ */
+ @GET
+ @Path("/allocations/{deviceId}")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getAllocation(@PathParam("deviceId") String deviceId) {
+ return getAllocations(deviceId);
+ }
+
+ private Response getAllocations(String deviceId) {
+ DhcpL2RelayService service = get(DhcpL2RelayService.class);
+ Map<String, DhcpAllocationInfo> allocations = service.getAllocationInfo();
+
+ try {
+ buildAllocationsNodeObject(allocations, deviceId);
+ return ok(mapper().writeValueAsString(root)).build();
+ } catch (Exception e) {
+ log.error("Error while fetching DhcpL2Relay allocations information through REST API: {}", e.getMessage());
+ return Response.status(INTERNAL_SERVER_ERROR).build();
+ }
+ }
+
+ private void buildAllocationsNodeObject(Map<String, DhcpAllocationInfo> allocationMap, String strDeviceId) {
+ if (Objects.nonNull(strDeviceId)) {
+ DeviceId deviceId = DeviceId.deviceId(strDeviceId);
+ allocationMap = allocationMap.entrySet().stream()
+ .filter(a -> a.getValue().location().deviceId().equals(deviceId))
+ .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
+ }
+
+ allocationMap.forEach((key, value) -> {
+ node.add(encodeDhcpAllocationInfo(value));
+ });
+ }
+
+ private ObjectNode encodeDhcpAllocationInfo(DhcpAllocationInfo entry) {
+ return mapper().createObjectNode()
+ .put(SUBSCRIBER_ID, entry.subscriberId())
+ .put(CONNECT_POINT, entry.location().toString())
+ .put(STATE, entry.type().toString())
+ .put(MAC_ADDRESS, entry.macAddress().toString())
+ .put(VLAN_ID, entry.vlanId().toShort())
+ .put(CIRCUIT_ID, entry.circuitId())
+ .put(IP_ALLOCATED, entry.ipAddress().getIp4Address().toString())
+ .put(ALLOCATION_TIMESTAMP, entry.allocationTime().toString());
+ }
+}
diff --git a/app/src/main/java/org/opencord/dhcpl2relay/rest/package-info.java b/app/src/main/java/org/opencord/dhcpl2relay/rest/package-info.java
new file mode 100755
index 0000000..37de45e
--- /dev/null
+++ b/app/src/main/java/org/opencord/dhcpl2relay/rest/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/**
+ * DHCP-L2RELAY rest interface.
+ */
+package org.opencord.dhcpl2relay.rest;
diff --git a/app/src/main/webapp/WEB-INF/web.xml b/app/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..f91b795
--- /dev/null
+++ b/app/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2022-present Open Networking Foundation
+ ~
+ ~ 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.
+ -->
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ id="ONOS" version="2.5">
+ <display-name>DhcpL2Relay REST API v1.0</display-name>
+ <security-constraint>
+ <web-resource-collection>
+ <web-resource-name>Secured</web-resource-name>
+ <url-pattern>/*</url-pattern>
+ </web-resource-collection>
+ <auth-constraint>
+ <role-name>admin</role-name>
+ </auth-constraint>
+ </security-constraint>
+ <security-role>
+ <role-name>admin</role-name>
+ </security-role>
+ <login-config>
+ <auth-method>BASIC</auth-method>
+ <realm-name>karaf</realm-name>
+ </login-config>
+ <servlet>
+ <servlet-name>JAX-RS Service</servlet-name>
+ <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
+ <init-param>
+ <param-name>jersey.config.server.provider.classnames</param-name>
+ <param-value>
+ org.opencord.dhcpl2relay.rest.DhcpL2RelayWebResource
+ </param-value>
+ </init-param>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>JAX-RS Service</servlet-name>
+ <url-pattern>/*</url-pattern>
+ </servlet-mapping>
+</web-app>