blob: d851e10c48af31093dd81e63859f886a9b63cb1d [file] [log] [blame]
daniel parkb5817102018-02-15 00:18:51 +09001/*
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.openstacknetworking.cli;
17
18import com.google.common.collect.Lists;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Jian Lidb521c12018-11-19 17:42:35 +090021import org.apache.karaf.shell.api.action.Completion;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
daniel parkb5817102018-02-15 00:18:51 +090023import org.onlab.packet.Ip4Address;
24import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.openstacknetworking.api.ExternalPeerRouter;
daniel park32b42202018-03-14 16:53:44 +090029import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
daniel parkb5817102018-02-15 00:18:51 +090030
31import java.util.List;
32
33/**
34 * Updates external peer router.
35 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070036@Service
daniel parkb5817102018-02-15 00:18:51 +090037@Command(scope = "onos", name = "openstack-update-peer-router",
38 description = "Update external peer router")
39public class UpdateExternalPeerRouterCommand extends AbstractShellCommand {
40
41 @Argument(index = 0, name = "ip address", description = "ip address",
42 required = true, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090043 @Completion(IpAddressCompleter.class)
daniel parkb5817102018-02-15 00:18:51 +090044 private String ipAddress = null;
45
46 @Argument(index = 1, name = "mac address", description = "mac address",
47 required = true, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090048 @Completion(MacAddressCompleter.class)
daniel parkb5817102018-02-15 00:18:51 +090049 private String macAddress = null;
50
51 @Argument(index = 2, name = "vlan id", description = "vlan id",
52 required = true, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090053 @Completion(VlanIdCompleter.class)
daniel parkb5817102018-02-15 00:18:51 +090054 private String vlanId = null;
55
56 private static final String FORMAT = "%-20s%-20s%-20s";
57 private static final String NO_ELEMENT = "There's no external peer router information with given ip address";
58 private static final String NONE = "None";
59
60 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070061 protected void doExecute() {
daniel park32b42202018-03-14 16:53:44 +090062 OpenstackNetworkAdminService service = AbstractShellCommand.get(OpenstackNetworkAdminService.class);
daniel parkb5817102018-02-15 00:18:51 +090063
64 IpAddress externalPeerIpAddress = IpAddress.valueOf(
65 IpAddress.Version.INET, Ip4Address.valueOf(ipAddress).toOctets());
66
67 if (service.externalPeerRouters().isEmpty()) {
68 print(NO_ELEMENT);
69 return;
70 } else if (service.externalPeerRouters().stream()
Jian Li5e2ad4a2018-07-16 13:40:53 +090071 .noneMatch(router -> router.ipAddress().toString().equals(ipAddress))) {
daniel parkb5817102018-02-15 00:18:51 +090072 print(NO_ELEMENT);
73 return;
74 }
75 try {
76 if (vlanId.equals(NONE)) {
77 service.updateExternalPeerRouter(externalPeerIpAddress,
78 MacAddress.valueOf(macAddress),
79 VlanId.NONE);
80
81 } else {
82 service.updateExternalPeerRouter(externalPeerIpAddress,
83 MacAddress.valueOf(macAddress),
84 VlanId.vlanId(vlanId));
85 }
86 } catch (IllegalArgumentException e) {
87 log.error("Exception occurred because of {}", e.toString());
88 }
89
90
91 print(FORMAT, "Router IP", "Mac Address", "VLAN ID");
92 List<ExternalPeerRouter> routers = Lists.newArrayList(service.externalPeerRouters());
93
94 for (ExternalPeerRouter router: routers) {
Jian Li5e2ad4a2018-07-16 13:40:53 +090095 print(FORMAT, router.ipAddress(),
96 router.macAddress().toString(),
97 router.vlanId());
daniel parkb5817102018-02-15 00:18:51 +090098 }
99 }
100}
101