blob: 1abfee2429b2b6faa017c26b9e3e687438b9a845 [file] [log] [blame]
Jian Li186fde52019-08-26 14:54:43 +09001/*
2 * Copyright 2019-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.k8snetworking.cli;
17
18import com.google.common.collect.Maps;
19import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Option;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.k8snetworking.api.K8sIpamService;
26import org.onosproject.k8snetworking.api.K8sNetwork;
27import org.onosproject.k8snetworking.api.K8sNetworkService;
28import org.onosproject.k8snetworking.api.K8sPort;
29
30import java.util.Map;
31import java.util.Set;
32
33import static org.onlab.packet.MacAddress.ZERO;
34
35/**
36 * Lists kubernetes IP addresses.
37 */
38@Command(scope = "onos", name = "k8s-ips",
39 description = "Lists all kubernetes IP addresses")
40public class K8sIpAddressListCommand extends AbstractShellCommand {
41
42 private static final String FORMAT = "%-30s%-20s%-30s";
43
44 @Argument(index = 0, name = "networkIds", description = "Network identifiers",
45 required = false, multiValued = true)
46 private String[] networkIds = null;
47
48 @Option(name = "-a", aliases = "--available",
49 description = "Available IP addresses",
50 required = false, multiValued = false)
51 private boolean available = false;
52
53 @Option(name = "-r", aliases = "--reserved",
54 description = "Allocated IP addresses",
55 required = false, multiValued = false)
56 private boolean reserved = false;
57
58 @Override
59 protected void doExecute() {
60 K8sIpamService ipamService = get(K8sIpamService.class);
61 K8sNetworkService networkService = get(K8sNetworkService.class);
62
63 if (networkIds == null || networkIds.length == 0) {
64 networkIds = networkService.networks().stream()
65 .map(K8sNetwork::networkId).toArray(String[]::new);
66 }
67
68 Map<String, Map<IpAddress, MacAddress>> ipMacs = Maps.newConcurrentMap();
69
70 if (available && reserved) {
71 error("Only one of list options (available | reserved) can be specified.");
72 return;
73 }
74
75 if (!(available || reserved)) {
76 error("At least one of list options (available | reserved) should be specified.");
77 return;
78 }
79
80 for (String networkId : networkIds) {
81 Map<IpAddress, MacAddress> tmpIpMacs = Maps.newConcurrentMap();
82 if (available) {
83 ipamService.availableIps(networkId)
84 .forEach(n -> tmpIpMacs.put(n, ZERO));
85 }
86
87 if (reserved) {
88 Set<K8sPort> ports = networkService.ports(networkId);
89 ipamService.allocatedIps(networkId).forEach(ip -> {
90 MacAddress mac = ports.stream()
91 .filter(p -> p.ipAddress().equals(ip))
92 .map(K8sPort::macAddress).findAny().orElse(ZERO);
93 tmpIpMacs.put(ip, mac);
94 });
95 }
96 ipMacs.put(networkId, tmpIpMacs);
97 }
98
99 if (ipMacs.size() > 0) {
100 print(FORMAT, "Network ID", "IP Address", "MAC Address");
101 ipMacs.forEach((k, v) -> v.forEach((ip, mac) -> print(FORMAT, k, ip, mac)));
102 } else {
103 print("No IP addresses are available or reserved.");
104 }
105 }
106}