blob: 7f0c1248dbcbe0713cb679f952dc1018b26b27fd [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 com.google.common.collect.Sets;
19import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.portloadbalancer.api.PortLoadBalancer;
24import org.onosproject.portloadbalancer.api.PortLoadBalancerAdminService;
25import org.onosproject.portloadbalancer.api.PortLoadBalancerId;
26import org.onosproject.portloadbalancer.api.PortLoadBalancerMode;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.PortNumber;
29
30import java.util.Set;
31import java.util.stream.Collectors;
32
33/**
34 * Command to add a port load balancer.
35 */
36@Service
37@Command(scope = "onos", name = "plb-add", description = "Create or update port load balancer ")
38public class PortLoadBalancerAddCommand extends AbstractShellCommand {
39 @Argument(index = 0, name = "deviceId",
40 description = "Device ID",
41 required = true, multiValued = false)
42 private String deviceIdStr;
43
44 @Argument(index = 1, name = "key",
45 description = "port load balancer key",
46 required = true, multiValued = false)
47 private String keyStr;
48
49 @Argument(index = 2, name = "mode",
50 description = "port load balancer mode. STATIC or LACP",
51 required = true, multiValued = false)
52 private String modeStr;
53
54 @Argument(index = 3, name = "ports",
55 description = "port load balancer physical ports",
56 required = true, multiValued = true)
57 private String[] portsStr;
58
59 // Operation constants
60 private static final String CREATE = "Create";
61 private static final String UPDATE = "Update";
62
63 @Override
64 protected void doExecute() {
65 DeviceId deviceId = DeviceId.deviceId(deviceIdStr);
66 int portLoadBalancerKey = Integer.parseInt(keyStr);
67
68 PortLoadBalancerMode mode = PortLoadBalancerMode.valueOf(modeStr.toUpperCase());
69 Set<PortNumber> ports = Sets.newHashSet(portsStr).stream()
70 .map(PortNumber::fromString).collect(Collectors.toSet());
71
72 PortLoadBalancerAdminService portLoadBalancerAdminService = get(PortLoadBalancerAdminService.class);
73 PortLoadBalancerId portLoadBalancerId = new PortLoadBalancerId(deviceId, portLoadBalancerKey);
74 PortLoadBalancer portLoadBalancer = portLoadBalancerAdminService
75 .createOrUpdate(portLoadBalancerId, ports, mode);
76 print("%s of %s executed", portLoadBalancer == null ? CREATE : UPDATE, portLoadBalancerId);
77
78 }
79}