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