blob: ebe5a4fa2654b82a38db902b8b521f683d16efc6 [file] [log] [blame]
Gustavo Silvade66bc72022-05-18 16:42:55 -03001/*
Joey Armstrong7e08d2a2022-12-30 12:25:42 -05002 * Copyright 2022-2023 Open Networking Foundation (ONF) and the ONF Contributors
Gustavo Silvade66bc72022-05-18 16:42:55 -03003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.opencord.dhcpl2relay.rest;
17
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20
21import java.util.Objects;
22import java.util.stream.Collectors;
23import java.util.Map;
24import java.util.Map.Entry;
25
26import javax.ws.rs.core.MediaType;
27import javax.ws.rs.core.Response;
28import javax.ws.rs.GET;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
32
33import org.onosproject.net.DeviceId;
34import org.onosproject.rest.AbstractWebResource;
35
36import org.opencord.dhcpl2relay.DhcpAllocationInfo;
37import org.opencord.dhcpl2relay.DhcpL2RelayService;
38
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
42import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
43
44/**
45 * DhcpL2Relay web resource.
46 */
47@Path("app")
48public class DhcpL2RelayWebResource extends AbstractWebResource {
49 private final ObjectNode root = mapper().createObjectNode();
50 private final ArrayNode node = root.putArray("entries");
51 private final Logger log = LoggerFactory.getLogger(getClass());
52
53 private static final String SUBSCRIBER_ID = "subscriberId";
54 private static final String CONNECT_POINT = "connectPoint";
55 private static final String MAC_ADDRESS = "macAddress";
56 private static final String STATE = "state";
57 private static final String VLAN_ID = "vlanId";
58 private static final String CIRCUIT_ID = "circuitId";
59 private static final String IP_ALLOCATED = "ipAllocated";
60 private static final String ALLOCATION_TIMESTAMP = "allocationTimestamp";
61
62 /**
63 *
64 * Shows all the successful DHCP allocations relayed by the dhcpl2relay.
65 *
66 * @return 200 OK
67 */
68 @GET
69 @Path("/allocations")
70 @Produces(MediaType.APPLICATION_JSON)
71 public Response getAllocations() {
72 return getAllocations(null);
73 }
74
75 /**
76 * Shows the successful DHCP allocations relayed by the dhcpl2relay for a specific access device.
77 *
78 * @param deviceId Access device ID.
79 *
80 * @return 200 OK
81 */
82 @GET
83 @Path("/allocations/{deviceId}")
84 @Produces(MediaType.APPLICATION_JSON)
85 public Response getAllocation(@PathParam("deviceId") String deviceId) {
86 return getAllocations(deviceId);
87 }
88
89 private Response getAllocations(String deviceId) {
90 DhcpL2RelayService service = get(DhcpL2RelayService.class);
91 Map<String, DhcpAllocationInfo> allocations = service.getAllocationInfo();
92
93 try {
94 buildAllocationsNodeObject(allocations, deviceId);
95 return ok(mapper().writeValueAsString(root)).build();
96 } catch (Exception e) {
97 log.error("Error while fetching DhcpL2Relay allocations information through REST API: {}", e.getMessage());
98 return Response.status(INTERNAL_SERVER_ERROR).build();
99 }
100 }
101
102 private void buildAllocationsNodeObject(Map<String, DhcpAllocationInfo> allocationMap, String strDeviceId) {
103 if (Objects.nonNull(strDeviceId)) {
104 DeviceId deviceId = DeviceId.deviceId(strDeviceId);
105 allocationMap = allocationMap.entrySet().stream()
106 .filter(a -> a.getValue().location().deviceId().equals(deviceId))
107 .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
108 }
109
110 allocationMap.forEach((key, value) -> {
111 node.add(encodeDhcpAllocationInfo(value));
112 });
113 }
114
115 private ObjectNode encodeDhcpAllocationInfo(DhcpAllocationInfo entry) {
116 return mapper().createObjectNode()
117 .put(SUBSCRIBER_ID, entry.subscriberId())
118 .put(CONNECT_POINT, entry.location().toString())
119 .put(STATE, entry.type().toString())
120 .put(MAC_ADDRESS, entry.macAddress().toString())
121 .put(VLAN_ID, entry.vlanId().toShort())
122 .put(CIRCUIT_ID, entry.circuitId())
123 .put(IP_ALLOCATED, entry.ipAddress().getIp4Address().toString())
124 .put(ALLOCATION_TIMESTAMP, entry.allocationTime().toString());
125 }
126}