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 | |
Jian Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.openstacknetworking.api.ExternalPeerRouter; |
| 25 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 26 | |
| 27 | import java.util.List; |
| 28 | |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 29 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson; |
Jian Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame] | 30 | |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 31 | /** |
| 32 | * Lists external peer router lists. |
| 33 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 34 | @Service |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 35 | @Command(scope = "onos", name = "openstack-peer-routers", |
| 36 | description = "Lists external peer router lists") |
| 37 | public class ExternalPeerRouterListCommand extends AbstractShellCommand { |
| 38 | |
| 39 | private static final String FORMAT = "%-20s%-20s%-20s"; |
| 40 | |
| 41 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 42 | protected void doExecute() { |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 43 | OpenstackNetworkService service = AbstractShellCommand.get(OpenstackNetworkService.class); |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 44 | List<ExternalPeerRouter> routers = Lists.newArrayList(service.externalPeerRouters()); |
| 45 | |
Jian Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame] | 46 | if (outputJson()) { |
| 47 | print("%s", json(this, routers)); |
| 48 | } else { |
| 49 | print(FORMAT, "Router IP", "Mac Address", "VLAN ID"); |
| 50 | for (ExternalPeerRouter router: routers) { |
| 51 | print(FORMAT, router.ipAddress(), |
| 52 | router.macAddress().toString(), |
| 53 | router.vlanId()); |
| 54 | } |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 55 | } |
| 56 | } |
Jian Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame] | 57 | |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 58 | private String json(AbstractShellCommand context, List<ExternalPeerRouter> routers) { |
Jian Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame] | 59 | ObjectMapper mapper = new ObjectMapper(); |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 60 | ArrayNode result = mapper.createArrayNode(); |
Jian Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame] | 61 | routers.forEach(r -> result.add(context.jsonForEntity(r, ExternalPeerRouter.class))); |
| 62 | |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 63 | return prettyJson(mapper, result.toString()); |
Jian Li | 5e2ad4a | 2018-07-16 13:40:53 +0900 | [diff] [blame] | 64 | } |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 65 | } |