blob: a0989b33d9a08fe4a950b0a25d4ec15173d8a397 [file] [log] [blame]
Jian Lic2538102018-07-03 22:42:07 +09001/*
2 * Copyright 2018-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 */
16package org.onosproject.openstackvtap.cli;
Jian Li26ef1302018-07-04 14:37:06 +090017
Jimo Jung14e87bf2018-09-03 16:28:13 +090018import com.google.common.collect.ImmutableSet;
Jian Li26ef1302018-07-04 14:37:06 +090019import org.apache.karaf.shell.commands.Argument;
Jian Lic2538102018-07-03 22:42:07 +090020import org.apache.karaf.shell.commands.Command;
21import org.onosproject.cli.AbstractShellCommand;
Jimo Jung14e87bf2018-09-03 16:28:13 +090022import org.onosproject.net.DeviceId;
23import org.onosproject.openstacknode.api.OpenstackNode;
24import org.onosproject.openstacknode.api.OpenstackNodeService;
Jian Lic2538102018-07-03 22:42:07 +090025import org.onosproject.openstackvtap.api.OpenstackVtap;
26import org.onosproject.openstackvtap.api.OpenstackVtapService;
27
Jimo Jung14e87bf2018-09-03 16:28:13 +090028import java.util.Objects;
Jian Lic2538102018-07-03 22:42:07 +090029import java.util.Set;
Jimo Jung14e87bf2018-09-03 16:28:13 +090030import java.util.stream.Collectors;
Jian Lic2538102018-07-03 22:42:07 +090031
Jian Li26ef1302018-07-04 14:37:06 +090032import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getVtapTypeFromString;
33
Jian Lic2538102018-07-03 22:42:07 +090034/**
Jimo Jung14e87bf2018-09-03 16:28:13 +090035 * Lists openstack vtap rules.
Jian Lic2538102018-07-03 22:42:07 +090036 */
37@Command(scope = "onos", name = "openstack-vtap-list",
38 description = "OpenstackVtap list")
39public class OpenstackVtapListCommand extends AbstractShellCommand {
40
Jimo Jung14e87bf2018-09-03 16:28:13 +090041 private final OpenstackVtapService vtapService = get(OpenstackVtapService.class);
42 private final OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
Jian Lic2538102018-07-03 22:42:07 +090043
44 @Argument(index = 0, name = "type",
Jimo Jung14e87bf2018-09-03 16:28:13 +090045 description = "vtap type [any|all|rx|tx]",
Jian Lic2538102018-07-03 22:42:07 +090046 required = false, multiValued = false)
Jimo Jung14e87bf2018-09-03 16:28:13 +090047 String vtapType = "any";
Jian Lic2538102018-07-03 22:42:07 +090048
49 private static final String FORMAT = "ID { %s }: type [%s], srcIP [%s], dstIP [%s]";
Jimo Jung14e87bf2018-09-03 16:28:13 +090050 private static final String FORMAT_TX_NODES = " tx openstack nodes: %s";
51 private static final String FORMAT_RX_NODES = " rx openstack nodes: %s";
Jian Lic2538102018-07-03 22:42:07 +090052
53 @Override
54 protected void execute() {
Jimo Jung14e87bf2018-09-03 16:28:13 +090055 OpenstackVtap.Type type = getVtapTypeFromString(vtapType);
56 Set<OpenstackVtap> openstackVtaps = vtapService.getVtaps(type);
57 for (OpenstackVtap vtap : openstackVtaps) {
Jian Lic2538102018-07-03 22:42:07 +090058 print(FORMAT,
Jimo Jung14e87bf2018-09-03 16:28:13 +090059 vtap.id().toString(),
60 vtap.type().toString(),
61 vtap.vtapCriterion().srcIpPrefix().toString(),
62 vtap.vtapCriterion().dstIpPrefix().toString());
63 print(FORMAT_TX_NODES, osNodeNames(vtap.txDeviceIds()));
64 print(FORMAT_RX_NODES, osNodeNames(vtap.rxDeviceIds()));
Jian Lic2538102018-07-03 22:42:07 +090065 }
66 }
Jimo Jung14e87bf2018-09-03 16:28:13 +090067
68 private Set<String> osNodeNames(Set<DeviceId> deviceIds) {
69 if (deviceIds == null) {
70 return ImmutableSet.of();
71 } else {
72 return deviceIds.parallelStream()
73 .map(osNodeService::node)
74 .filter(Objects::nonNull)
75 .map(OpenstackNode::hostname)
76 .collect(Collectors.toSet());
77 }
78 }
79
Jian Lic2538102018-07-03 22:42:07 +090080}