blob: 75878f7cd2d314f9cadc94ff3d28631222129db0 [file] [log] [blame]
Andreas Papazois25a98e72016-07-28 10:57:15 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Andreas Papazois25a98e72016-07-28 10:57:15 +05303 *
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.onosproject.cli.net;
17
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Andreas Papazois25a98e72016-07-28 10:57:15 +053021import org.onlab.packet.VlanId;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Port;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.behaviour.VlanQuery;
29import org.onosproject.net.device.DeviceService;
30import org.onosproject.net.driver.DriverHandler;
31import org.onosproject.net.driver.DriverService;
32
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.Set;
36
37/**
38 * Command to show the list of unused vlan-ids.
39 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
Andreas Papazois25a98e72016-07-28 10:57:15 +053041@Command(scope = "onos", name = "port-query-vlans",
42 description = "Lists all unused VLAN-IDs on port")
43public class PortQueryVlansCommand extends AbstractShellCommand {
44
45 private static final String AVAIL_VLANS = "VLAN-ID: %s";
46 private static final String VLAN_NOT_AVAILABLE = "No unused VLAN-ID";
47 private static final String FMT = "port=%s, state=%s, type=%s, speed=%s%s";
48 private static final String NO_SUPPORT = "Device not supporting VLAN-ID retrieval";
49 private static final String FAILURE = "Failed to retrieve VLAN information: ";
50
51 @Argument(index = 0, name = "port",
52 description = "Port Description",
53 required = true, multiValued = true)
54 private String[] ports;
55
56
57 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058 protected void doExecute() {
Andreas Papazois25a98e72016-07-28 10:57:15 +053059 DeviceService service = get(DeviceService.class);
60 for (String portStr : ports) {
61 ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(portStr);
62 Port port = service.getPort(connectPoint.deviceId(), connectPoint.port());
63 printPort(port);
64 printVlans(port);
65 }
66 }
67
68 private void printPort(Port port) {
69 String portName = portName(port.number());
70 Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled";
71 String portType = port.type().toString().toLowerCase();
72 String annotations = annotations(port.annotations());
73 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
74 }
75
76 private String portName(PortNumber port) {
77 return port.equals(PortNumber.LOCAL) ? "local" : port.toString();
78 }
79
80 private void printVlans(Port port) {
81 DeviceService deviceService = get(DeviceService.class);
82 DriverService driverService = get(DriverService.class);
83
84 DeviceId deviceId = (DeviceId) port.element().id();
85 Device device = deviceService.getDevice(deviceId);
86
87 if (!device.is(VlanQuery.class)) {
88 // The relevant behavior is not supported by the device.
89 print(NO_SUPPORT);
90 return;
91 }
92
93 DriverHandler h = driverService.createHandler(deviceId);
94 VlanQuery vlanQuery = h.behaviour(VlanQuery.class);
95
96 try {
97 Set<VlanId> vlanIds = vlanQuery.queryVlanIds(port.number());
98
99 if (vlanIds.isEmpty()) {
100 print(VLAN_NOT_AVAILABLE);
101 } else {
102 print(AVAIL_VLANS, getRanges(vlanIds).toString());
103 }
104 } catch (Exception e) {
105 print(FAILURE + e.getMessage());
106 }
107 }
108
109 private static ArrayList getRanges(Set<VlanId> vlans) {
110 short i = 0;
111 short[] vlanArray = new short[vlans.size()];
112 for (VlanId vlanId : vlans) {
113 vlanArray[i++] = vlanId.toShort();
114 }
115 Arrays.sort(vlanArray);
116
117 ArrayList ranges = new ArrayList();
118 short rStart = 0;
119 short rEnd = 0;
120
121 for (i = 2; i < vlanArray.length; i++) {
122 if (vlanArray[i] == vlanArray[i - 1] + 1 &&
123 vlanArray[i] == vlanArray[i - 2] + 2) {
124 // Three consecutive VLAN-IDs found, so range exists.
125 if (rEnd == vlanArray[i - 1]) {
126 // Range already exists, so step the end.
127 rEnd = vlanArray[i];
128 } else {
129 // Setup a new range.
130 rStart = vlanArray[i - 2];
131 rEnd = vlanArray[i];
132 }
133 } else {
134 // Not in a range.
135 if (rEnd == vlanArray[i - 1]) {
136 // Previous range is discontinued and is stored.
137 ranges.add(rStart + "-" + rEnd);
138 } else {
139 // No previous range.
140 if (vlanArray[i] != vlanArray[i - 1] + 1) {
141 // Current VLAN-ID is not 2nd consecutive.
142 if (vlanArray[i - 1] == vlanArray[i - 2] + 1) {
143 // The 2 previous VLAN-IDs were consequetive, so 2nd
144 // last is stored separately.
145 ranges.add(vlanArray[i - 2]);
146 }
147 // Previous is stored, when current is not consequetive.
148 ranges.add(vlanArray[i - 1]);
149 }
150 }
151 }
152 }
153 if (rEnd == vlanArray[vlanArray.length - 1]) {
154 // Array finished with a range.
155 ranges.add(rStart + "-" + rEnd);
156 } else {
157 if (vlanArray[vlanArray.length - 1] == vlanArray[vlanArray.length - 2] + 1) {
158 // Previous is stored, when current is consequetive.
159 ranges.add(vlanArray[vlanArray.length - 2]);
160 }
161 // Last item is stored
162 ranges.add(vlanArray[vlanArray.length - 1]);
163 }
164 return ranges;
165 }
166}