Jian Li | 5172870 | 2019-05-17 18:38:56 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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.openstacknetworking.cli; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.apache.karaf.shell.api.action.Argument; |
| 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | import org.apache.karaf.shell.api.action.Completion; |
| 23 | import org.apache.karaf.shell.api.action.Option; |
| 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 28 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 29 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
| 30 | import org.openstack4j.model.network.Port; |
| 31 | |
| 32 | import java.util.List; |
| 33 | import java.util.Set; |
| 34 | import java.util.stream.Collectors; |
| 35 | |
| 36 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.ifaceNameFromOsPortId; |
| 37 | |
| 38 | /** |
| 39 | * Shows the list of the openvswitch ports detached from real interfaces. |
| 40 | */ |
| 41 | @Service |
| 42 | @Command(scope = "onos", name = "openstack-detached-ports", |
| 43 | description = "Shows the detached VM's tap port list.") |
| 44 | public class OpenstackDetachedPortListCommand extends AbstractShellCommand { |
| 45 | |
| 46 | private static final String PORT_NAME = "portName"; |
| 47 | private static final String FORMAT = "%-25s%-25s%-25s"; |
| 48 | |
| 49 | @Option(name = "-a", aliases = "--all", description = "Apply this command to all nodes", |
| 50 | required = false, multiValued = false) |
| 51 | private boolean isAll = false; |
| 52 | |
| 53 | @Argument(index = 0, name = "hostnames", description = "Hostname(s) to apply this command", |
| 54 | required = false, multiValued = true) |
| 55 | @Completion(OpenstackComputeNodeCompleter.class) |
| 56 | private String[] hostnames = null; |
| 57 | |
| 58 | @Override |
| 59 | protected void doExecute() { |
| 60 | OpenstackNodeService nodeService = get(OpenstackNodeService.class); |
| 61 | OpenstackNetworkService networkService = get(OpenstackNetworkService.class); |
| 62 | DeviceService deviceService = get(DeviceService.class); |
| 63 | |
| 64 | if (isAll) { |
| 65 | hostnames = nodeService.completeNodes().stream() |
| 66 | .map(OpenstackNode::hostname).toArray(String[]::new); |
| 67 | } |
| 68 | |
| 69 | if (hostnames == null) { |
| 70 | print("Please specify one of hostname or --all options."); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | print(FORMAT, "Hostname", "Integration Bridge", "Detached Port"); |
| 75 | |
| 76 | for (String hostname : hostnames) { |
| 77 | networkService.ports().forEach(p -> { |
| 78 | if (hostname.equals(p.getHostId())) { |
| 79 | OpenstackNode osNode = nodeService.node(p.getHostId()); |
| 80 | if (osNode != null) { |
| 81 | Set<String> detachedPortNames = |
| 82 | detachedOvsPort(p, |
| 83 | deviceService.getPorts(osNode.intgBridge())); |
| 84 | detachedPortNames.forEach(dp -> |
| 85 | print(FORMAT, hostname, osNode.intgBridge().toString(), dp) |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | }); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private Set<String> detachedOvsPort(Port osPort, |
| 94 | List<org.onosproject.net.Port> ovsPorts) { |
| 95 | Set<String> portNames = ovsPorts.stream() |
| 96 | .filter(ovsPort -> ovsPort.annotations() != null || |
| 97 | ovsPort.annotations().keys().contains(PORT_NAME)) |
| 98 | .map(ovsPort -> ovsPort.annotations().value(PORT_NAME)) |
| 99 | .collect(Collectors.toSet()); |
| 100 | |
| 101 | String tapPort = ifaceNameFromOsPortId(osPort.getId()); |
| 102 | Set<String> detachedPortNames = Sets.newConcurrentHashSet(); |
| 103 | |
| 104 | if (!portNames.contains(tapPort)) { |
| 105 | detachedPortNames.add(tapPort); |
| 106 | } |
| 107 | |
| 108 | return ImmutableSet.copyOf(detachedPortNames); |
| 109 | } |
| 110 | } |