Charles Chan | f7b1b4b | 2019-01-16 15:30:39 -0800 | [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.portloadbalancer.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.api.action.Command; |
| 19 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.portloadbalancer.api.PortLoadBalancer; |
| 23 | import org.onosproject.portloadbalancer.api.PortLoadBalancerId; |
| 24 | import org.onosproject.portloadbalancer.api.PortLoadBalancerService; |
| 25 | |
| 26 | import java.util.Map; |
| 27 | |
| 28 | /** |
| 29 | * Command to show all port load balancers. |
| 30 | */ |
| 31 | @Service |
| 32 | @Command(scope = "onos", name = "plbs", description = "Lists port load balancers") |
| 33 | public class PortLoadBalancerListCommand extends AbstractShellCommand { |
| 34 | |
| 35 | // Operation constant |
| 36 | private static final String AVAILABLE = "Available"; |
| 37 | |
| 38 | @Override |
| 39 | public void doExecute() { |
| 40 | PortLoadBalancerService service = get(PortLoadBalancerService.class); |
| 41 | // Get port load balancers and reservations |
| 42 | Map<PortLoadBalancerId, PortLoadBalancer> portLoadBalancerStore = service.getPortLoadBalancers(); |
| 43 | Map<PortLoadBalancerId, ApplicationId> portLoadBalancerResStore = service.getReservations(); |
| 44 | // Print id -> ports, mode, reservation |
| 45 | portLoadBalancerStore.forEach((portLoadBalancerId, portLoadBalancer) -> |
| 46 | print("%s -> %s, %s, %s", portLoadBalancerId, portLoadBalancer.ports(), portLoadBalancer.mode(), |
| 47 | portLoadBalancerResStore.get(portLoadBalancerId) == null ? AVAILABLE : |
| 48 | portLoadBalancerResStore.get(portLoadBalancerId).name())); |
| 49 | } |
| 50 | } |