Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.opencord.cordvtn.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.google.common.base.Strings; |
| 22 | import com.google.common.collect.Lists; |
| 23 | import org.apache.karaf.shell.commands.Argument; |
| 24 | import org.apache.karaf.shell.commands.Command; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 26 | import org.opencord.cordvtn.api.core.ServiceNetworkService; |
| 27 | import org.opencord.cordvtn.api.net.ServicePort; |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 28 | |
| 29 | import java.util.Collections; |
| 30 | import java.util.List; |
| 31 | import java.util.stream.Collectors; |
| 32 | |
| 33 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 34 | |
| 35 | /** |
| 36 | * Lists VTN networks. |
| 37 | */ |
| 38 | @Command(scope = "onos", name = "cordvtn-ports", |
| 39 | description = "Lists all VTN ports") |
| 40 | public class CordVtnPortListCommand extends AbstractShellCommand { |
| 41 | |
Hyunsun Moon | a0e8b66 | 2016-11-21 12:32:01 -0800 | [diff] [blame] | 42 | private static final String FORMAT = "%-40s%-20s%-18s%-8s%s"; |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 43 | |
| 44 | @Argument(name = "networkId", description = "Network ID") |
| 45 | private String networkId = null; |
| 46 | |
| 47 | @Override |
| 48 | protected void execute() { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 49 | ServiceNetworkService service = AbstractShellCommand.get(ServiceNetworkService.class); |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 50 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 51 | List<ServicePort> ports = Lists.newArrayList(service.servicePorts()); |
| 52 | Collections.sort(ports, ServicePort.SERVICE_PORT_COMPARATOR); |
Hyunsun Moon | a0e8b66 | 2016-11-21 12:32:01 -0800 | [diff] [blame] | 53 | if (!Strings.isNullOrEmpty(networkId)) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 54 | ports.removeIf(port -> !port.networkId().id().equals(networkId)); |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (outputJson()) { |
| 58 | try { |
| 59 | print("%s", mapper().writeValueAsString(json(ports))); |
| 60 | } catch (JsonProcessingException e) { |
| 61 | print("Failed to list networks in JSON format"); |
| 62 | } |
| 63 | } else { |
Hyunsun Moon | a0e8b66 | 2016-11-21 12:32:01 -0800 | [diff] [blame] | 64 | print(FORMAT, "ID", "MAC", "IP", "VLAN", "WAN IPs"); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 65 | for (ServicePort port: ports) { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 66 | List<String> floatingIps = port.addressPairs().stream() |
| 67 | .map(ip -> ip.ip().toString()) |
| 68 | .collect(Collectors.toList()); |
| 69 | print(FORMAT, port.id(), |
| 70 | port.mac(), |
| 71 | port.ip(), |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 72 | port.vlanId() != null ? port.vlanId() : "", |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 73 | floatingIps.isEmpty() ? "" : floatingIps); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 78 | private JsonNode json(List<ServicePort> ports) { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 79 | ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode(); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 80 | for (ServicePort port: ports) { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 81 | ArrayNode addrPairs = mapper().createArrayNode(); |
| 82 | port.addressPairs().forEach(pair -> addrPairs.add( |
| 83 | mapper().createObjectNode() |
| 84 | .put("ip", pair.ip().toString()) |
| 85 | .put("mac", pair.mac().toString()))); |
| 86 | |
| 87 | result.add(mapper().createObjectNode() |
| 88 | .put("id", port.id().id()) |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 89 | .put("networkId", port.networkId().id()) |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 90 | .put("mac", port.mac().toString()) |
| 91 | .put("ip", port.ip().toString()) |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 92 | .put("vlan", port.vlanId() != null ? |
| 93 | port.vlanId().toString() : null) |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 94 | .set("addressPairs", addrPairs)); |
| 95 | } |
| 96 | return result; |
| 97 | } |
| 98 | } |