blob: 6850b0fe69a1a1459ea71df4693efdddbea49292 [file] [log] [blame]
Charles Chanf7b1b4b2019-01-16 15:30:39 -08001/*
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 */
16package org.onosproject.portloadbalancer.cli;
17
18import org.apache.karaf.shell.api.action.Command;
19import org.apache.karaf.shell.api.action.lifecycle.Service;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.portloadbalancer.api.PortLoadBalancer;
23import org.onosproject.portloadbalancer.api.PortLoadBalancerId;
24import org.onosproject.portloadbalancer.api.PortLoadBalancerService;
25
26import 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")
33public 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}