blob: 81fcd8c0418828a40b226c7c873d7213c8e27be9 [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
daniel parkb5817102018-02-15 00:18:51 +090022import org.onlab.packet.Ip4Address;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
26import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.openstacknetworking.api.ExternalPeerRouter;
daniel park32b42202018-03-14 16:53:44 +090028import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
daniel parkb5817102018-02-15 00:18:51 +090029
30import java.util.List;
31
32/**
33 * Updates external peer router.
34 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070035@Service
daniel parkb5817102018-02-15 00:18:51 +090036@Command(scope = "onos", name = "openstack-update-peer-router",
37 description = "Update external peer router")
38public class UpdateExternalPeerRouterCommand extends AbstractShellCommand {
39
40 @Argument(index = 0, name = "ip address", description = "ip address",
41 required = true, multiValued = false)
42 private String ipAddress = null;
43
44 @Argument(index = 1, name = "mac address", description = "mac address",
45 required = true, multiValued = false)
46 private String macAddress = null;
47
48 @Argument(index = 2, name = "vlan id", description = "vlan id",
49 required = true, multiValued = false)
50 private String vlanId = null;
51
52 private static final String FORMAT = "%-20s%-20s%-20s";
53 private static final String NO_ELEMENT = "There's no external peer router information with given ip address";
54 private static final String NONE = "None";
55
56 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070057 protected void doExecute() {
daniel park32b42202018-03-14 16:53:44 +090058 OpenstackNetworkAdminService service = AbstractShellCommand.get(OpenstackNetworkAdminService.class);
daniel parkb5817102018-02-15 00:18:51 +090059
60 IpAddress externalPeerIpAddress = IpAddress.valueOf(
61 IpAddress.Version.INET, Ip4Address.valueOf(ipAddress).toOctets());
62
63 if (service.externalPeerRouters().isEmpty()) {
64 print(NO_ELEMENT);
65 return;
66 } else if (service.externalPeerRouters().stream()
Jian Li5e2ad4a2018-07-16 13:40:53 +090067 .noneMatch(router -> router.ipAddress().toString().equals(ipAddress))) {
daniel parkb5817102018-02-15 00:18:51 +090068 print(NO_ELEMENT);
69 return;
70 }
71 try {
72 if (vlanId.equals(NONE)) {
73 service.updateExternalPeerRouter(externalPeerIpAddress,
74 MacAddress.valueOf(macAddress),
75 VlanId.NONE);
76
77 } else {
78 service.updateExternalPeerRouter(externalPeerIpAddress,
79 MacAddress.valueOf(macAddress),
80 VlanId.vlanId(vlanId));
81 }
82 } catch (IllegalArgumentException e) {
83 log.error("Exception occurred because of {}", e.toString());
84 }
85
86
87 print(FORMAT, "Router IP", "Mac Address", "VLAN ID");
88 List<ExternalPeerRouter> routers = Lists.newArrayList(service.externalPeerRouters());
89
90 for (ExternalPeerRouter router: routers) {
Jian Li5e2ad4a2018-07-16 13:40:53 +090091 print(FORMAT, router.ipAddress(),
92 router.macAddress().toString(),
93 router.vlanId());
daniel parkb5817102018-02-15 00:18:51 +090094 }
95 }
96}
97