blob: c4090e0baa73d5ae44853f35c0dc3cd5f9d3fe9e [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;
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onlab.packet.Ip4Address;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.openstacknetworking.api.ExternalPeerRouter;
daniel park32b42202018-03-14 16:53:44 +090027import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
daniel parkb5817102018-02-15 00:18:51 +090028
29import java.util.List;
30
31/**
32 * Updates external peer router.
33 */
34@Command(scope = "onos", name = "openstack-update-peer-router",
35 description = "Update external peer router")
36public class UpdateExternalPeerRouterCommand extends AbstractShellCommand {
37
38 @Argument(index = 0, name = "ip address", description = "ip address",
39 required = true, multiValued = false)
40 private String ipAddress = null;
41
42 @Argument(index = 1, name = "mac address", description = "mac address",
43 required = true, multiValued = false)
44 private String macAddress = null;
45
46 @Argument(index = 2, name = "vlan id", description = "vlan id",
47 required = true, multiValued = false)
48 private String vlanId = null;
49
50 private static final String FORMAT = "%-20s%-20s%-20s";
51 private static final String NO_ELEMENT = "There's no external peer router information with given ip address";
52 private static final String NONE = "None";
53
54 @Override
55 protected void execute() {
daniel park32b42202018-03-14 16:53:44 +090056 OpenstackNetworkAdminService service = AbstractShellCommand.get(OpenstackNetworkAdminService.class);
daniel parkb5817102018-02-15 00:18:51 +090057
58 IpAddress externalPeerIpAddress = IpAddress.valueOf(
59 IpAddress.Version.INET, Ip4Address.valueOf(ipAddress).toOctets());
60
61 if (service.externalPeerRouters().isEmpty()) {
62 print(NO_ELEMENT);
63 return;
64 } else if (service.externalPeerRouters().stream()
Jian Li5e2ad4a2018-07-16 13:40:53 +090065 .noneMatch(router -> router.ipAddress().toString().equals(ipAddress))) {
daniel parkb5817102018-02-15 00:18:51 +090066 print(NO_ELEMENT);
67 return;
68 }
69 try {
70 if (vlanId.equals(NONE)) {
71 service.updateExternalPeerRouter(externalPeerIpAddress,
72 MacAddress.valueOf(macAddress),
73 VlanId.NONE);
74
75 } else {
76 service.updateExternalPeerRouter(externalPeerIpAddress,
77 MacAddress.valueOf(macAddress),
78 VlanId.vlanId(vlanId));
79 }
80 } catch (IllegalArgumentException e) {
81 log.error("Exception occurred because of {}", e.toString());
82 }
83
84
85 print(FORMAT, "Router IP", "Mac Address", "VLAN ID");
86 List<ExternalPeerRouter> routers = Lists.newArrayList(service.externalPeerRouters());
87
88 for (ExternalPeerRouter router: routers) {
Jian Li5e2ad4a2018-07-16 13:40:53 +090089 print(FORMAT, router.ipAddress(),
90 router.macAddress().toString(),
91 router.vlanId());
daniel parkb5817102018-02-15 00:18:51 +090092 }
93 }
94}
95