blob: e436f7ca41e51535054b809aee2f901b564c4325 [file] [log] [blame]
Jian Li3ba5c582021-01-14 11:30:36 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.cli;
17
18import com.google.common.collect.ImmutableList;
19import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Completion;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onlab.packet.IpAddress;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
26import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
27import org.onosproject.kubevirtnetworking.api.KubevirtNetworkService;
28
29import java.util.ArrayList;
30import java.util.Collections;
31import java.util.List;
32
33import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_AVAILABILITY;
34import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
35import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString;
36
37/**
38 * Lists all IP addresses.
39 */
40@Service
41@Command(scope = "onos", name = "kubevirt-ips",
42 description = "Lists all IP addresses")
43public class KubevirtListIpAddressCommand extends AbstractShellCommand {
44
45 @Argument(name = "networkId", description = "Network ID")
46 @Completion(KubevirtNetworkIdCompleter.class)
47 private String networkId = null;
48
49 @Override
50 protected void doExecute() throws Exception {
51 KubevirtNetworkService service = get(KubevirtNetworkService.class);
Jian Lifc7e6cf2021-04-08 11:13:24 +090052
53 if (networkId == null) {
54 error("No network identifier was specified");
55 return;
56 }
57
Jian Li3ba5c582021-01-14 11:30:36 +090058 KubevirtNetwork network = service.network(networkId);
59
60 if (network == null) {
61 print("No network was found with the given network ID");
62 return;
63 }
64
65 KubevirtIpPool pool = network.ipPool();
66 if (pool == null) {
67 print("No IP pool was found with the given network ID");
68 return;
69 }
70
71 String format = genFormatString(ImmutableList.of(
72 CLI_IP_ADDRESS_LENGTH, CLI_IP_ADDRESS_AVAILABILITY));
73 print(format, "IP Address", "Availability");
74
75 List<IpAddress> sortedAllocatedIps = new ArrayList<>(pool.allocatedIps());
76 Collections.sort(sortedAllocatedIps);
77 for (IpAddress ip : sortedAllocatedIps) {
78 print(format, ip.toString(), "[ X ]");
79 }
80
81 List<IpAddress> sortedAvailableIps = new ArrayList<>(pool.availableIps());
82 Collections.sort(sortedAvailableIps);
83 for (IpAddress ip : sortedAvailableIps) {
84 print(format, ip.toString(), "[ O ]");
85 }
86 }
87}