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.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.openstacknetworking.api.ExternalPeerRouter; |
daniel park | 32b4220 | 2018-03-14 16:53:44 +0900 | [diff] [blame] | 27 | import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 28 | |
| 29 | import 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") |
| 36 | public 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 park | 32b4220 | 2018-03-14 16:53:44 +0900 | [diff] [blame] | 56 | OpenstackNetworkAdminService service = AbstractShellCommand.get(OpenstackNetworkAdminService.class); |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 57 | |
| 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 Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame^] | 65 | .noneMatch(router -> router.ipAddress().toString().equals(ipAddress))) { |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 66 | 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 Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame^] | 89 | print(FORMAT, router.ipAddress(), |
| 90 | router.macAddress().toString(), |
| 91 | router.vlanId()); |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | |