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