Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Command; |
| 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.cluster.NodeId; |
| 25 | import org.onosproject.cluster.RoleInfo; |
| 26 | import org.onosproject.mastership.MastershipService; |
| 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.device.DeviceService; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 30 | |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 31 | import java.util.List; |
| 32 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import static org.onosproject.cli.net.DevicesListCommand.getSortedDevices; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Lists mastership roles of nodes for each device. |
| 37 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 38 | @Service |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 39 | @Command(scope = "onos", name = "roles", |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 40 | description = "Lists mastership roles of nodes for each device.") |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 41 | public class RolesCommand extends AbstractShellCommand { |
| 42 | |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 43 | private static final String FMT_HDR = "%s: master=%s, standbys=[ %s]"; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 44 | |
| 45 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 46 | protected void doExecute() { |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 47 | DeviceService deviceService = get(DeviceService.class); |
| 48 | MastershipService roleService = get(MastershipService.class); |
| 49 | |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 50 | if (outputJson()) { |
| 51 | print("%s", json(roleService, getSortedDevices(deviceService))); |
| 52 | } else { |
| 53 | for (Device d : getSortedDevices(deviceService)) { |
| 54 | DeviceId did = d.id(); |
| 55 | printRoles(roleService, did); |
| 56 | } |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 60 | // Produces JSON structure with role information for the given devices. |
| 61 | private JsonNode json(MastershipService service, List<Device> sortedDevices) { |
| 62 | ObjectMapper mapper = new ObjectMapper(); |
| 63 | ArrayNode results = mapper.createArrayNode(); |
| 64 | for (Device device : sortedDevices) { |
| 65 | results.add(json(service, mapper, device)); |
| 66 | } |
| 67 | return results; |
| 68 | } |
| 69 | |
| 70 | // Produces JSON structure with role information for the given device. |
| 71 | private JsonNode json(MastershipService service, ObjectMapper mapper, |
| 72 | Device device) { |
| 73 | NodeId master = service.getMasterFor(device.id()); |
| 74 | ObjectNode result = mapper.createObjectNode() |
| 75 | .put("id", device.id().toString()) |
| 76 | .put("master", master != null ? master.toString() : "none"); |
| 77 | RoleInfo nodes = service.getNodesFor(device.id()); |
| 78 | ArrayNode standbys = mapper.createArrayNode(); |
| 79 | for (NodeId nid : nodes.backups()) { |
| 80 | standbys.add(nid.toString()); |
| 81 | } |
| 82 | result.set("standbys", standbys); |
| 83 | return result; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Prints the role information for a device. |
| 88 | * |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 89 | * @param service mastership service |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 90 | * @param deviceId the ID of the device |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 91 | */ |
| 92 | protected void printRoles(MastershipService service, DeviceId deviceId) { |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 93 | RoleInfo nodes = service.getNodesFor(deviceId); |
| 94 | StringBuilder builder = new StringBuilder(); |
| 95 | for (NodeId nid : nodes.backups()) { |
| 96 | builder.append(nid).append(" "); |
| 97 | } |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 98 | |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 99 | print(FMT_HDR, deviceId, |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 100 | nodes.master() == null ? "NONE" : nodes.master(), |
| 101 | builder.toString()); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 102 | } |
| 103 | } |