blob: 882695211f635912da35d864147c16ec485bc27d [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;
Jian Lidb521c12018-11-19 17:42:35 +090021import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Lic2538102018-07-03 22:42:07 +090023import org.onosproject.cli.AbstractShellCommand;
Jimo Jung14e87bf2018-09-03 16:28:13 +090024import org.onosproject.net.DeviceId;
25import org.onosproject.openstacknode.api.OpenstackNode;
26import org.onosproject.openstacknode.api.OpenstackNodeService;
Jian Lic2538102018-07-03 22:42:07 +090027import org.onosproject.openstackvtap.api.OpenstackVtap;
28import org.onosproject.openstackvtap.api.OpenstackVtapService;
29
Jimo Jung14e87bf2018-09-03 16:28:13 +090030import java.util.Objects;
Jian Lic2538102018-07-03 22:42:07 +090031import java.util.Set;
Jimo Jung14e87bf2018-09-03 16:28:13 +090032import java.util.stream.Collectors;
Jian Lic2538102018-07-03 22:42:07 +090033
Jian Li26ef1302018-07-04 14:37:06 +090034import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getVtapTypeFromString;
35
Jian Lic2538102018-07-03 22:42:07 +090036/**
Jimo Jung14e87bf2018-09-03 16:28:13 +090037 * Lists openstack vtap rules.
Jian Lic2538102018-07-03 22:42:07 +090038 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070039@Service
Jian Lic2538102018-07-03 22:42:07 +090040@Command(scope = "onos", name = "openstack-vtap-list",
41 description = "OpenstackVtap list")
42public class OpenstackVtapListCommand extends AbstractShellCommand {
43
Jimo Jung14e87bf2018-09-03 16:28:13 +090044 private final OpenstackVtapService vtapService = get(OpenstackVtapService.class);
45 private final OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
Jian Lic2538102018-07-03 22:42:07 +090046
47 @Argument(index = 0, name = "type",
Jimo Jung14e87bf2018-09-03 16:28:13 +090048 description = "vtap type [any|all|rx|tx]",
Jian Lic2538102018-07-03 22:42:07 +090049 required = false, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090050 @Completion(VtapTypeCompleter.class)
Jimo Jung14e87bf2018-09-03 16:28:13 +090051 String vtapType = "any";
Jian Lic2538102018-07-03 22:42:07 +090052
53 private static final String FORMAT = "ID { %s }: type [%s], srcIP [%s], dstIP [%s]";
Jimo Jung14e87bf2018-09-03 16:28:13 +090054 private static final String FORMAT_TX_NODES = " tx openstack nodes: %s";
55 private static final String FORMAT_RX_NODES = " rx openstack nodes: %s";
Jian Lic2538102018-07-03 22:42:07 +090056
57 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070058 protected void doExecute() {
Jimo Jung14e87bf2018-09-03 16:28:13 +090059 OpenstackVtap.Type type = getVtapTypeFromString(vtapType);
60 Set<OpenstackVtap> openstackVtaps = vtapService.getVtaps(type);
61 for (OpenstackVtap vtap : openstackVtaps) {
Jian Lic2538102018-07-03 22:42:07 +090062 print(FORMAT,
Jimo Jung14e87bf2018-09-03 16:28:13 +090063 vtap.id().toString(),
64 vtap.type().toString(),
65 vtap.vtapCriterion().srcIpPrefix().toString(),
66 vtap.vtapCriterion().dstIpPrefix().toString());
67 print(FORMAT_TX_NODES, osNodeNames(vtap.txDeviceIds()));
68 print(FORMAT_RX_NODES, osNodeNames(vtap.rxDeviceIds()));
Jian Lic2538102018-07-03 22:42:07 +090069 }
70 }
Jimo Jung14e87bf2018-09-03 16:28:13 +090071
72 private Set<String> osNodeNames(Set<DeviceId> deviceIds) {
73 if (deviceIds == null) {
74 return ImmutableSet.of();
75 } else {
76 return deviceIds.parallelStream()
77 .map(osNodeService::node)
78 .filter(Objects::nonNull)
79 .map(OpenstackNode::hostname)
80 .collect(Collectors.toSet());
81 }
82 }
83
Jian Lic2538102018-07-03 22:42:07 +090084}