Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 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 | */ |
| 16 | package org.onosproject.rest.resources; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.cluster.NodeId; |
| 22 | import org.onosproject.cluster.RoleInfo; |
| 23 | import org.onosproject.mastership.MastershipAdminService; |
| 24 | import org.onosproject.mastership.MastershipService; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.MastershipRole; |
| 27 | import org.onosproject.rest.AbstractWebResource; |
| 28 | |
| 29 | import javax.ws.rs.Consumes; |
| 30 | import javax.ws.rs.GET; |
| 31 | import javax.ws.rs.PUT; |
| 32 | import javax.ws.rs.Path; |
| 33 | import javax.ws.rs.PathParam; |
| 34 | import javax.ws.rs.Produces; |
| 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
| 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
| 39 | import java.util.Set; |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 40 | |
| 41 | import static org.onlab.util.Tools.nullIsNotFound; |
| 42 | |
| 43 | /** |
| 44 | * Manage the mastership of ONOS instances. |
| 45 | */ |
| 46 | @Path("mastership") |
| 47 | public final class MastershipWebResource extends AbstractWebResource { |
| 48 | |
Jian Li | 0409b70 | 2016-05-03 18:21:37 -0700 | [diff] [blame] | 49 | private static final String DEVICE_IDS = "deviceIds"; |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 50 | private static final String DEVICE_ID = "deviceId"; |
| 51 | private static final String NODE_ID = "nodeId"; |
| 52 | |
| 53 | private static final String DEVICE_ID_INVALID = "Invalid deviceId for setting role"; |
| 54 | private static final String NODE_ID_INVALID = "Invalid nodeId for setting role"; |
| 55 | |
| 56 | private static final String NODE_ID_NOT_FOUND = "Node id is not found"; |
| 57 | private static final String ROLE_INFO_NOT_FOUND = "Role info is not found"; |
| 58 | private static final String MASTERSHIP_ROLE_NOT_FOUND = "Mastership role is not found"; |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 59 | |
| 60 | private final MastershipService mastershipService = get(MastershipService.class); |
| 61 | private final MastershipAdminService mastershipAdminService = |
| 62 | get(MastershipAdminService.class); |
| 63 | |
| 64 | /** |
| 65 | * Returns the role of the local node for the specified device. |
| 66 | * |
| 67 | * @param deviceId device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 68 | * @return 200 OK with role of the current node |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 69 | * @onos.rsModel MastershipRole |
| 70 | */ |
| 71 | @GET |
| 72 | @Produces(MediaType.APPLICATION_JSON) |
| 73 | @Path("{deviceId}/local") |
| 74 | public Response getLocalRole(@PathParam("deviceId") String deviceId) { |
| 75 | MastershipRole role = mastershipService.getLocalRole(DeviceId.deviceId(deviceId)); |
| 76 | ObjectNode root = codec(MastershipRole.class).encode(role, this); |
| 77 | return ok(root).build(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the current master for a given device. |
| 82 | * |
| 83 | * @param deviceId device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 84 | * @return 200 OK with the identifier of the master controller for the device |
Jian Li | 0409b70 | 2016-05-03 18:21:37 -0700 | [diff] [blame] | 85 | * @onos.rsModel NodeId |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 86 | */ |
| 87 | @GET |
| 88 | @Produces(MediaType.APPLICATION_JSON) |
| 89 | @Path("{deviceId}/master") |
| 90 | public Response getMasterFor(@PathParam("deviceId") String deviceId) { |
| 91 | NodeId id = nullIsNotFound(mastershipService.getMasterFor( |
| 92 | DeviceId.deviceId(deviceId)), NODE_ID_NOT_FOUND); |
| 93 | |
| 94 | ObjectNode root = mapper().createObjectNode(); |
Jian Li | 0409b70 | 2016-05-03 18:21:37 -0700 | [diff] [blame] | 95 | root.put(NODE_ID, id.id()); |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 96 | return ok(root).build(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns controllers connected to a given device, in order of |
| 101 | * preference. The first entry in the list is the current master. |
| 102 | * |
| 103 | * @param deviceId device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 104 | * @return 200 OK with a list of controller identifiers |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 105 | * @onos.rsModel RoleInfo |
| 106 | */ |
| 107 | @GET |
| 108 | @Produces(MediaType.APPLICATION_JSON) |
| 109 | @Path("{deviceId}/role") |
| 110 | public Response getNodesFor(@PathParam("deviceId") String deviceId) { |
| 111 | RoleInfo info = nullIsNotFound(mastershipService.getNodesFor( |
| 112 | DeviceId.deviceId(deviceId)), ROLE_INFO_NOT_FOUND); |
| 113 | ObjectNode root = codec(RoleInfo.class).encode(info, this); |
| 114 | return ok(root).build(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the devices for which a controller is master. |
| 119 | * |
| 120 | * @param nodeId controller identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 121 | * @return 200 OK with a set of device identifiers |
Jian Li | 0409b70 | 2016-05-03 18:21:37 -0700 | [diff] [blame] | 122 | * @onos.rsModel DeviceIds |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 123 | */ |
| 124 | @GET |
| 125 | @Produces(MediaType.APPLICATION_JSON) |
| 126 | @Path("{nodeId}/device") |
| 127 | public Response getDeviceOf(@PathParam("nodeId") String nodeId) { |
| 128 | ObjectNode root = mapper().createObjectNode(); |
Jian Li | 0409b70 | 2016-05-03 18:21:37 -0700 | [diff] [blame] | 129 | ArrayNode devicesNode = root.putArray(DEVICE_IDS); |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 130 | |
| 131 | Set<DeviceId> devices = mastershipService.getDevicesOf(NodeId.nodeId(nodeId)); |
| 132 | if (devices != null) { |
| 133 | devices.forEach(id -> devicesNode.add(id.toString())); |
| 134 | } |
| 135 | |
| 136 | return ok(root).build(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns the mastership status of the local controller for a given |
| 141 | * device forcing master selection if necessary. |
| 142 | * |
| 143 | * @param deviceId device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 144 | * @return 200 OK with the role of this controller instance |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 145 | * @onos.rsModel MastershipRole |
| 146 | */ |
| 147 | @GET |
| 148 | @Produces(MediaType.APPLICATION_JSON) |
| 149 | @Path("{deviceId}/request") |
| 150 | public Response requestRoleFor(@PathParam("deviceId") String deviceId) { |
Jian Li | 0409b70 | 2016-05-03 18:21:37 -0700 | [diff] [blame] | 151 | MastershipRole role = nullIsNotFound(mastershipService.requestRoleForSync( |
| 152 | DeviceId.deviceId(deviceId)), MASTERSHIP_ROLE_NOT_FOUND); |
| 153 | ObjectNode root = codec(MastershipRole.class).encode(role, this); |
| 154 | return ok(root).build(); |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Abandons mastership of the specified device on the local node thus |
| 159 | * forcing selection of a new master. If the local node is not a master |
| 160 | * for this device, no master selection will occur. |
| 161 | * |
| 162 | * @param deviceId device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 163 | * @return status of the request - CREATED if the JSON is correct |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 164 | */ |
| 165 | @GET |
| 166 | @Produces(MediaType.APPLICATION_JSON) |
| 167 | @Path("{deviceId}/relinquish") |
| 168 | public Response relinquishMastership(@PathParam("deviceId") String deviceId) { |
| 169 | DeviceId id = DeviceId.deviceId(deviceId); |
Jian Li | 0409b70 | 2016-05-03 18:21:37 -0700 | [diff] [blame] | 170 | mastershipService.relinquishMastershipSync(id); |
| 171 | return Response.created(id.uri()).build(); |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Applies the current mastership role for the specified device. |
| 176 | * |
| 177 | * @param stream JSON representation of device, node, mastership info |
| 178 | * @return status of the request - CREATED if the JSON is correct, |
| 179 | * BAD_REQUEST if the JSON is invalid |
| 180 | * @onos.rsModel MastershipPut |
| 181 | */ |
| 182 | @PUT |
| 183 | @Consumes(MediaType.APPLICATION_JSON) |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 184 | public Response setRole(InputStream stream) { |
| 185 | |
| 186 | try { |
| 187 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 188 | JsonNode deviceIdJson = jsonTree.get(DEVICE_ID); |
| 189 | JsonNode nodeIdJson = jsonTree.get(NODE_ID); |
| 190 | MastershipRole role = codec(MastershipRole.class).decode(jsonTree, this); |
| 191 | |
| 192 | if (deviceIdJson == null) { |
| 193 | throw new IllegalArgumentException(DEVICE_ID_INVALID); |
| 194 | } |
| 195 | |
| 196 | if (nodeIdJson == null) { |
| 197 | throw new IllegalArgumentException(NODE_ID_INVALID); |
| 198 | } |
| 199 | |
Jian Li | 0409b70 | 2016-05-03 18:21:37 -0700 | [diff] [blame] | 200 | mastershipAdminService.setRoleSync(NodeId.nodeId(nodeIdJson.asText()), |
| 201 | DeviceId.deviceId(deviceIdJson.asText()), role); |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 202 | |
| 203 | return Response.ok().build(); |
Jian Li | 0409b70 | 2016-05-03 18:21:37 -0700 | [diff] [blame] | 204 | } catch (IOException e) { |
Jian Li | f96d41f | 2016-05-03 09:49:12 -0700 | [diff] [blame] | 205 | throw new IllegalArgumentException(e); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Balances the mastership to be shared as evenly as possibly by all |
| 211 | * online instances. |
| 212 | * |
| 213 | * @return status of the request - OK if the request is successfully processed |
| 214 | */ |
| 215 | @GET |
| 216 | @Produces(MediaType.APPLICATION_JSON) |
| 217 | public Response balanceRoles() { |
| 218 | mastershipAdminService.balanceRoles(); |
| 219 | return Response.ok().build(); |
| 220 | } |
| 221 | } |