blob: e67fa2f120743b388eaf3c84241dfd356c7dcc49 [file] [log] [blame]
daniel parkeeb8e042018-02-21 14:06:58 +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;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Jian Lidb521c12018-11-19 17:42:35 +090021import org.apache.karaf.shell.api.action.Completion;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
daniel parkeeb8e042018-02-21 14:06:58 +090023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknetworking.api.ExternalPeerRouter;
daniel park32b42202018-03-14 16:53:44 +090025import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
daniel parkeeb8e042018-02-21 14:06:58 +090026
27import java.util.List;
28
Jian Li5ecfd1a2018-12-10 11:41:03 +090029import static org.onosproject.cli.AbstractShellCommand.get;
30
daniel parkeeb8e042018-02-21 14:06:58 +090031/**
32 * Deletes external peer router.
33 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070034@Service
daniel parkeeb8e042018-02-21 14:06:58 +090035@Command(scope = "onos", name = "openstack-delete-peer-router",
36 description = "Delete external peer router")
37public class DeleteExternalPeerRouterCommand extends AbstractShellCommand {
38 @Argument(index = 0, name = "ip address", description = "ip address",
39 required = true, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090040 @Completion(IpAddressCompleter.class)
daniel parkeeb8e042018-02-21 14:06:58 +090041 private String ipAddress = null;
42
43 private static final String FORMAT = "%-20s%-20s%-20s";
Jian Li5ecfd1a2018-12-10 11:41:03 +090044 private static final String NO_ELEMENT =
45 "There's no external peer router information with given ip address";
daniel parkeeb8e042018-02-21 14:06:58 +090046
47 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070048 protected void doExecute() {
Jian Li5ecfd1a2018-12-10 11:41:03 +090049 OpenstackNetworkAdminService service = get(OpenstackNetworkAdminService.class);
daniel parkeeb8e042018-02-21 14:06:58 +090050
51 if (service.externalPeerRouters().stream()
Jian Li5e2ad4a2018-07-16 13:40:53 +090052 .noneMatch(router -> router.ipAddress().toString().equals(ipAddress))) {
daniel parkeeb8e042018-02-21 14:06:58 +090053 print(NO_ELEMENT);
54 return;
55 }
56
57 try {
58 service.deleteExternalPeerRouter(ipAddress);
59 } catch (IllegalArgumentException e) {
Daniel Parka3ffbdb2018-11-28 13:51:39 +090060 log.error("Exception occurred because of {}", e);
daniel parkeeb8e042018-02-21 14:06:58 +090061 }
62 print(FORMAT, "Router IP", "Mac Address", "VLAN ID");
63 List<ExternalPeerRouter> routers = Lists.newArrayList(service.externalPeerRouters());
64
65 for (ExternalPeerRouter router: routers) {
Jian Li5e2ad4a2018-07-16 13:40:53 +090066 print(FORMAT, router.ipAddress(),
67 router.macAddress().toString(),
68 router.vlanId());
daniel parkeeb8e042018-02-21 14:06:58 +090069 }
70
71 }
72}