daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.cli; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.openstacknetworking.api.ExternalPeerRouter; |
| 26 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 27 | |
| 28 | import 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") |
| 35 | public 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 | } |