daniel park | eeb8e04 | 2018-02-21 14:06:58 +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; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
Jian Li | db521c1 | 2018-11-19 17:42:35 +0900 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.openstacknetworking.api.ExternalPeerRouter; |
daniel park | 32b4220 | 2018-03-14 16:53:44 +0900 | [diff] [blame] | 25 | import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 26 | |
| 27 | import java.util.List; |
| 28 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 29 | import static org.onosproject.cli.AbstractShellCommand.get; |
| 30 | |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 31 | /** |
| 32 | * Deletes external peer router. |
| 33 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 34 | @Service |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 35 | @Command(scope = "onos", name = "openstack-delete-peer-router", |
| 36 | description = "Delete external peer router") |
| 37 | public class DeleteExternalPeerRouterCommand extends AbstractShellCommand { |
| 38 | @Argument(index = 0, name = "ip address", description = "ip address", |
| 39 | required = true, multiValued = false) |
Jian Li | db521c1 | 2018-11-19 17:42:35 +0900 | [diff] [blame] | 40 | @Completion(IpAddressCompleter.class) |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 41 | private String ipAddress = null; |
| 42 | |
| 43 | private static final String FORMAT = "%-20s%-20s%-20s"; |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 44 | private static final String NO_ELEMENT = |
| 45 | "There's no external peer router information with given ip address"; |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 46 | |
| 47 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 48 | protected void doExecute() { |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 49 | OpenstackNetworkAdminService service = get(OpenstackNetworkAdminService.class); |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 50 | |
| 51 | if (service.externalPeerRouters().stream() |
Jian Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame] | 52 | .noneMatch(router -> router.ipAddress().toString().equals(ipAddress))) { |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 53 | print(NO_ELEMENT); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | try { |
| 58 | service.deleteExternalPeerRouter(ipAddress); |
| 59 | } catch (IllegalArgumentException e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 60 | log.error("Exception occurred because of {}", e); |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 61 | } |
| 62 | print(FORMAT, "Router IP", "Mac Address", "VLAN ID"); |
| 63 | List<ExternalPeerRouter> routers = Lists.newArrayList(service.externalPeerRouters()); |
| 64 | |
| 65 | for (ExternalPeerRouter router: routers) { |
Jian Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame] | 66 | print(FORMAT, router.ipAddress(), |
| 67 | router.macAddress().toString(), |
| 68 | router.vlanId()); |
daniel park | eeb8e04 | 2018-02-21 14:06:58 +0900 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | } |
| 72 | } |