Jian Li | 7bca127 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.kubevirtnetworking.cli; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 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.Completion; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.kubevirtnetworking.api.KubevirtIpPool; |
| 26 | import org.onosproject.kubevirtnetworking.api.KubevirtNetwork; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkService; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.Collections; |
| 31 | import java.util.List; |
| 32 | |
| 33 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_AVAILABILITY; |
| 34 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH; |
| 35 | import 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") |
| 43 | public 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); |
| 52 | KubevirtNetwork network = service.network(networkId); |
| 53 | |
| 54 | if (network == null) { |
| 55 | print("No network was found with the given network ID"); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | KubevirtIpPool pool = network.ipPool(); |
| 60 | if (pool == null) { |
| 61 | print("No IP pool was found with the given network ID"); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | String format = genFormatString(ImmutableList.of( |
| 66 | CLI_IP_ADDRESS_LENGTH, CLI_IP_ADDRESS_AVAILABILITY)); |
| 67 | print(format, "IP Address", "Availability"); |
| 68 | |
| 69 | List<IpAddress> sortedAllocatedIps = new ArrayList<>(pool.allocatedIps()); |
| 70 | Collections.sort(sortedAllocatedIps); |
| 71 | for (IpAddress ip : sortedAllocatedIps) { |
| 72 | print(format, ip.toString(), "[ X ]"); |
| 73 | } |
| 74 | |
| 75 | List<IpAddress> sortedAvailableIps = new ArrayList<>(pool.availableIps()); |
| 76 | Collections.sort(sortedAvailableIps); |
| 77 | for (IpAddress ip : sortedAvailableIps) { |
| 78 | print(format, ip.toString(), "[ O ]"); |
| 79 | } |
| 80 | } |
| 81 | } |