blob: bf4c3e9922ce1b87943e831faf61a150967040cc [file] [log] [blame]
Gustavo Silvab87da032022-05-18 17:51:59 -03001/*
Joey Armstrong4de0b452023-01-18 15:26:54 -05002 * Copyright 2022-2023 Open Networking Foundation (ONF) and the ONF Contributors
Gustavo Silvab87da032022-05-18 17:51:59 -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.aaa.rest;
17
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20
21import org.onlab.util.Tools;
22
23import org.onosproject.net.AnnotationKeys;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Port;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.rest.AbstractWebResource;
28import org.onosproject.utils.Comparators;
29
30import org.opencord.aaa.AuthenticationRecord;
31import org.opencord.aaa.AuthenticationService;
32import org.opencord.sadis.SadisService;
33import org.opencord.sadis.SubscriberAndDeviceInformation;
34
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import javax.ws.rs.GET;
39import javax.ws.rs.Path;
40import javax.ws.rs.PathParam;
41import javax.ws.rs.Produces;
42import javax.ws.rs.core.MediaType;
43import javax.ws.rs.core.Response;
44
45import java.util.Comparator;
46import java.util.List;
47import java.util.stream.Collectors;
48
49import static com.google.common.collect.Lists.newArrayList;
50import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
51
52/**
53 * AAA app web resource.
54 */
55@Path("app")
56public class AaaWebResource extends AbstractWebResource {
57 private final ObjectNode root = mapper().createObjectNode();
58 private final ArrayNode node = root.putArray("entries");
59 private final Logger log = LoggerFactory.getLogger(getClass());
60
61 private static final String UNKNOWN = "UNKNOWN";
62 private static final String CONNECT_POINT = "connectPoint";
63 private static final String STATE = "authState";
64 private static final String LAST_CHANGED = "lastChanged";
65 private static final String MAC_ADDRESS = "macAddress";
66 private static final String SUBSCRIBER_ID = "subscriberId";
67 private static final String USERNAME = "username";
68
69 /**
70 * Gets the AAA users.
71 *
72 * @return 200 OK
73 */
74 @GET
75 @Path("/users")
76 @Produces(MediaType.APPLICATION_JSON)
77 public Response getUsers() {
78 return getUsers(null);
79 }
80
81 /**
82 * Gets the AAA users by device access id.
83 *
84 * @param deviceId Access device ID.
85 *
86 * @return 200 OK
87 */
88 @GET
89 @Path("/users/{deviceId}")
90 @Produces(MediaType.APPLICATION_JSON)
91 public Response getUsersByDeviceId(@PathParam("deviceId") String deviceId) {
92 return getUsers(deviceId);
93 }
94
95 private Response getUsers(String strDeviceId) {
96 AuthenticationService authService = get(AuthenticationService.class);
97
98 try {
99 final Comparator<AuthenticationRecord> authenticationRecordComparator =
100 (a1, a2) -> Comparators.CONNECT_POINT_COMPARATOR.
101 compare(a1.supplicantConnectPoint(), a2.supplicantConnectPoint());
102 List<AuthenticationRecord> authentications = newArrayList(authService.getAuthenticationRecords());
103 authentications.sort(authenticationRecordComparator);
104
105 if (strDeviceId != null && !strDeviceId.isEmpty()) {
106 DeviceId deviceId = DeviceId.deviceId(strDeviceId);
107 authentications = authentications.stream()
108 .filter(a -> a.supplicantConnectPoint().deviceId().equals(deviceId))
109 .collect(Collectors.toList());
110 }
111
112 for (AuthenticationRecord auth : authentications) {
113 node.add(encodeAaaUser(auth));
114 }
115 return ok(mapper().writeValueAsString(root)).build();
116 } catch (Exception e) {
117 log.error("Error while fetching AAA users info through REST API: {}", e.getMessage());
118 return Response.status(INTERNAL_SERVER_ERROR).build();
119 }
120 }
121
122 private ObjectNode encodeAaaUser(AuthenticationRecord auth) {
123 SadisService sadisService = get(SadisService.class);
124 DeviceService devService = get(DeviceService.class);
125
126 String username = UNKNOWN;
127 if (auth.username() != null) {
128 username = new String(auth.username());
129 }
130 String mac = UNKNOWN;
131 if (auth.supplicantAddress() != null) {
132 mac = auth.supplicantAddress().toString();
133 }
134
135 Port port = devService.getPort(auth.supplicantConnectPoint());
136 String nasPortId = UNKNOWN;
137 if (port != null) {
138 nasPortId = devService.getPort(auth.supplicantConnectPoint()).
139 annotations().value(AnnotationKeys.PORT_NAME);
140 }
141
142 String subsId = UNKNOWN;
143 SubscriberAndDeviceInformation subscriber = sadisService.getSubscriberInfoService().get(nasPortId);
144 if (subscriber != null) {
145 subsId = subscriber.nasPortId();
146 }
147
148 return mapper().createObjectNode()
149 .put(CONNECT_POINT, auth.supplicantConnectPoint().toString())
150 .put(STATE, auth.state())
151 .put(LAST_CHANGED, Tools.timeAgo(auth.lastChanged()))
152 .put(MAC_ADDRESS, mac)
153 .put(SUBSCRIBER_ID, subsId)
154 .put(USERNAME, username);
155 }
156}