Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstackvtap.cli; |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 17 | |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableSet; |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 19 | import org.apache.karaf.shell.commands.Argument; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 24 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 25 | import org.onosproject.openstackvtap.api.OpenstackVtap; |
| 26 | import org.onosproject.openstackvtap.api.OpenstackVtapService; |
| 27 | |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 28 | import java.util.Objects; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 29 | import java.util.Set; |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 30 | import java.util.stream.Collectors; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 31 | |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 32 | import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getVtapTypeFromString; |
| 33 | |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 34 | /** |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 35 | * Lists openstack vtap rules. |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 36 | */ |
| 37 | @Command(scope = "onos", name = "openstack-vtap-list", |
| 38 | description = "OpenstackVtap list") |
| 39 | public class OpenstackVtapListCommand extends AbstractShellCommand { |
| 40 | |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 41 | private final OpenstackVtapService vtapService = get(OpenstackVtapService.class); |
| 42 | private final OpenstackNodeService osNodeService = get(OpenstackNodeService.class); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 43 | |
| 44 | @Argument(index = 0, name = "type", |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 45 | description = "vtap type [any|all|rx|tx]", |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 46 | required = false, multiValued = false) |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 47 | String vtapType = "any"; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 48 | |
| 49 | private static final String FORMAT = "ID { %s }: type [%s], srcIP [%s], dstIP [%s]"; |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 50 | private static final String FORMAT_TX_NODES = " tx openstack nodes: %s"; |
| 51 | private static final String FORMAT_RX_NODES = " rx openstack nodes: %s"; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 52 | |
| 53 | @Override |
| 54 | protected void execute() { |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 55 | OpenstackVtap.Type type = getVtapTypeFromString(vtapType); |
| 56 | Set<OpenstackVtap> openstackVtaps = vtapService.getVtaps(type); |
| 57 | for (OpenstackVtap vtap : openstackVtaps) { |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 58 | print(FORMAT, |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 59 | 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 Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 65 | } |
| 66 | } |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 67 | |
| 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 Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 80 | } |