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