blob: 089f8509a21e0fef2b3a8ff433f98922fd71c3ab [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;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Lic2538102018-07-03 22:42:07 +090022import org.onosproject.cli.AbstractShellCommand;
Jimo Jung14e87bf2018-09-03 16:28:13 +090023import org.onosproject.net.DeviceId;
24import org.onosproject.openstacknode.api.OpenstackNode;
25import org.onosproject.openstacknode.api.OpenstackNodeService;
Jian Lic2538102018-07-03 22:42:07 +090026import org.onosproject.openstackvtap.api.OpenstackVtap;
27import org.onosproject.openstackvtap.api.OpenstackVtapService;
28
Jimo Jung14e87bf2018-09-03 16:28:13 +090029import java.util.Objects;
Jian Lic2538102018-07-03 22:42:07 +090030import java.util.Set;
Jimo Jung14e87bf2018-09-03 16:28:13 +090031import java.util.stream.Collectors;
Jian Lic2538102018-07-03 22:42:07 +090032
Jian Li26ef1302018-07-04 14:37:06 +090033import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getVtapTypeFromString;
34
Jian Lic2538102018-07-03 22:42:07 +090035/**
Jimo Jung14e87bf2018-09-03 16:28:13 +090036 * Lists openstack vtap rules.
Jian Lic2538102018-07-03 22:42:07 +090037 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070038@Service
Jian Lic2538102018-07-03 22:42:07 +090039@Command(scope = "onos", name = "openstack-vtap-list",
40 description = "OpenstackVtap list")
41public class OpenstackVtapListCommand extends AbstractShellCommand {
42
Jimo Jung14e87bf2018-09-03 16:28:13 +090043 private final OpenstackVtapService vtapService = get(OpenstackVtapService.class);
44 private final OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
Jian Lic2538102018-07-03 22:42:07 +090045
46 @Argument(index = 0, name = "type",
Jimo Jung14e87bf2018-09-03 16:28:13 +090047 description = "vtap type [any|all|rx|tx]",
Jian Lic2538102018-07-03 22:42:07 +090048 required = false, multiValued = false)
Jimo Jung14e87bf2018-09-03 16:28:13 +090049 String vtapType = "any";
Jian Lic2538102018-07-03 22:42:07 +090050
51 private static final String FORMAT = "ID { %s }: type [%s], srcIP [%s], dstIP [%s]";
Jimo Jung14e87bf2018-09-03 16:28:13 +090052 private static final String FORMAT_TX_NODES = " tx openstack nodes: %s";
53 private static final String FORMAT_RX_NODES = " rx openstack nodes: %s";
Jian Lic2538102018-07-03 22:42:07 +090054
55 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070056 protected void doExecute() {
Jimo Jung14e87bf2018-09-03 16:28:13 +090057 OpenstackVtap.Type type = getVtapTypeFromString(vtapType);
58 Set<OpenstackVtap> openstackVtaps = vtapService.getVtaps(type);
59 for (OpenstackVtap vtap : openstackVtaps) {
Jian Lic2538102018-07-03 22:42:07 +090060 print(FORMAT,
Jimo Jung14e87bf2018-09-03 16:28:13 +090061 vtap.id().toString(),
62 vtap.type().toString(),
63 vtap.vtapCriterion().srcIpPrefix().toString(),
64 vtap.vtapCriterion().dstIpPrefix().toString());
65 print(FORMAT_TX_NODES, osNodeNames(vtap.txDeviceIds()));
66 print(FORMAT_RX_NODES, osNodeNames(vtap.rxDeviceIds()));
Jian Lic2538102018-07-03 22:42:07 +090067 }
68 }
Jimo Jung14e87bf2018-09-03 16:28:13 +090069
70 private Set<String> osNodeNames(Set<DeviceId> deviceIds) {
71 if (deviceIds == null) {
72 return ImmutableSet.of();
73 } else {
74 return deviceIds.parallelStream()
75 .map(osNodeService::node)
76 .filter(Objects::nonNull)
77 .map(OpenstackNode::hostname)
78 .collect(Collectors.toSet());
79 }
80 }
81
Jian Lic2538102018-07-03 22:42:07 +090082}