Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 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.ofagent.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 22 | import org.onosproject.ofagent.api.OFSwitch; |
| 23 | import org.onosproject.ofagent.api.OFSwitchService; |
| 24 | |
| 25 | import java.util.Set; |
| 26 | import java.util.stream.Collectors; |
| 27 | |
| 28 | /** |
| 29 | * Lists virtual OF switches. |
| 30 | */ |
| 31 | @Command(scope = "onos", name = "ofagent-switches", description = "Lists all OF switches") |
| 32 | public class OFSwitchListCommand extends AbstractShellCommand { |
| 33 | |
| 34 | private static final String FORMAT = "%-35s%-8s"; |
| 35 | |
| 36 | @Argument(index = 0, name = "network", description = "Virtual network ID", |
| 37 | required = false, multiValued = false) |
| 38 | private long networkId = NetworkId.NONE.id(); |
| 39 | |
| 40 | @Override |
| 41 | protected void execute() { |
| 42 | OFSwitchService service = get(OFSwitchService.class); |
| 43 | |
| 44 | Set<OFSwitch> ofSwitches; |
| 45 | if (networkId != NetworkId.NONE.id()) { |
| 46 | ofSwitches = service.ofSwitches(NetworkId.networkId(networkId)); |
| 47 | } else { |
| 48 | ofSwitches = service.ofSwitches(); |
| 49 | } |
| 50 | |
| 51 | print(FORMAT, "DPID", "Connected controllers"); |
| 52 | ofSwitches.forEach(ofSwitch -> { |
| 53 | Set<String> channels = ofSwitch.controllerChannels().stream() |
| 54 | .map(channel -> channel.remoteAddress().toString()) |
| 55 | .collect(Collectors.toSet()); |
| 56 | print(FORMAT, ofSwitch.dpid(), channels); |
| 57 | }); |
| 58 | } |
| 59 | } |