blob: 4ac5f15d96c4d6ab0df9ad227b364e378b01b7c8 [file] [log] [blame]
Hyunsun Moon53381e82017-03-28 19:58:28 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moon53381e82017-03-28 19:58:28 +09003 *
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.ofagent.cli;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.incubator.net.virtual.NetworkId;
22import org.onosproject.ofagent.api.OFSwitch;
23import org.onosproject.ofagent.api.OFSwitchService;
24
25import java.util.Set;
26import java.util.stream.Collectors;
27
28/**
29 * Lists virtual OF switches.
30 */
31@Command(scope = "onos", name = "ofagent-switches", description = "Lists all OF switches")
32public 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}