blob: ae30021db7fff84ff9e6c38ae46b4d187308bfcd [file] [log] [blame]
daniel parkb5817102018-02-15 00:18:51 +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
Jian Li5e2ad4a2018-07-16 13:40:53 +090018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
daniel parkb5817102018-02-15 00:18:51 +090021import com.google.common.collect.Lists;
22import org.apache.karaf.shell.commands.Command;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknetworking.api.ExternalPeerRouter;
25import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
26
27import java.util.List;
28
Jian Li5e2ad4a2018-07-16 13:40:53 +090029import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
30
daniel parkb5817102018-02-15 00:18:51 +090031/**
32 * Lists external peer router lists.
33 */
34@Command(scope = "onos", name = "openstack-peer-routers",
35 description = "Lists external peer router lists")
36public 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 parkb5817102018-02-15 00:18:51 +090043 List<ExternalPeerRouter> routers = Lists.newArrayList(service.externalPeerRouters());
44
Jian Li5e2ad4a2018-07-16 13:40:53 +090045 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 parkb5817102018-02-15 00:18:51 +090054 }
55 }
Jian Li5e2ad4a2018-07-16 13:40:53 +090056
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 parkb5817102018-02-15 00:18:51 +090064}