blob: eed62e713f9c74cb3b48a612f9babbda27a09a96 [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.VlanId;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.openstacknetworking.api.ExternalPeerRouter;
26import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
27
28import java.util.List;
29
30/**
31 * Updates external peer router macc address.
32 */
33@Command(scope = "onos", name = "openstack-update-peer-router-vlan",
34 description = "Updates external peer router vlan")
35public class UpdateExternalPeerRouterVlanCommand extends AbstractShellCommand {
36 @Argument(index = 0, name = "ip address", description = "ip address",
37 required = true, multiValued = false)
38 private String ipAddress = null;
39
40 @Argument(index = 1, name = "vlan id", description = "vlan id",
41 required = true, multiValued = false)
42 private String vlanId = null;
43
44 private static final String FORMAT = "%-20s%-20s%-20s";
45 private static final String NO_ELEMENT = "There's no external peer router information with given ip address";
46 private static final String NONE = "None";
47
48 @Override
49 protected void execute() {
50 OpenstackNetworkService service = AbstractShellCommand.get(OpenstackNetworkService.class);
51
52 IpAddress externalPeerIpAddress = IpAddress.valueOf(
53 IpAddress.Version.INET, Ip4Address.valueOf(ipAddress).toOctets());
54
55 if (service.externalPeerRouters().isEmpty()) {
56 print(NO_ELEMENT);
57 return;
58 } else if (service.externalPeerRouters().stream()
59 .noneMatch(router -> router.externalPeerRouterIp().toString().equals(ipAddress))) {
60 print(NO_ELEMENT);
61 return;
62 }
63
64 try {
65 if (vlanId.equals(NONE)) {
66 service.updateExternalPeerRouterVlan(externalPeerIpAddress, VlanId.NONE);
67 } else {
68 service.updateExternalPeerRouterVlan(externalPeerIpAddress, VlanId.vlanId(vlanId));
69 }
70 } catch (IllegalArgumentException e) {
71 log.error("Exception occurred because of {}", e.toString());
72 }
73
74 print(FORMAT, "Router IP", "Mac Address", "VLAN ID");
75 List<ExternalPeerRouter> routers = Lists.newArrayList(service.externalPeerRouters());
76
77 for (ExternalPeerRouter router: routers) {
78 print(FORMAT, router.externalPeerRouterIp(),
79 router.externalPeerRouterMac().toString(),
80 router.externalPeerRouterVlanId());
81 }
82 }
83}