blob: 99a95c0d3501ce79c566add4374756f7424cec82 [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.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
daniel parkb5817102018-02-15 00:18:51 +090020import com.google.common.collect.Lists;
21import org.apache.karaf.shell.commands.Command;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.openstacknetworking.api.ExternalPeerRouter;
24import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
25
26import java.util.List;
27
Jian Lif1efbe52018-07-17 23:20:16 +090028import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
Jian Li5e2ad4a2018-07-16 13:40:53 +090029
daniel parkb5817102018-02-15 00:18:51 +090030/**
31 * Lists external peer router lists.
32 */
33@Command(scope = "onos", name = "openstack-peer-routers",
34 description = "Lists external peer router lists")
35public class ExternalPeerRouterListCommand extends AbstractShellCommand {
36
37 private static final String FORMAT = "%-20s%-20s%-20s";
38
39 @Override
40 protected void execute() {
41 OpenstackNetworkService service = AbstractShellCommand.get(OpenstackNetworkService.class);
daniel parkb5817102018-02-15 00:18:51 +090042 List<ExternalPeerRouter> routers = Lists.newArrayList(service.externalPeerRouters());
43
Jian Li5e2ad4a2018-07-16 13:40:53 +090044 if (outputJson()) {
45 print("%s", json(this, routers));
46 } else {
47 print(FORMAT, "Router IP", "Mac Address", "VLAN ID");
48 for (ExternalPeerRouter router: routers) {
49 print(FORMAT, router.ipAddress(),
50 router.macAddress().toString(),
51 router.vlanId());
52 }
daniel parkb5817102018-02-15 00:18:51 +090053 }
54 }
Jian Li5e2ad4a2018-07-16 13:40:53 +090055
Jian Lif1efbe52018-07-17 23:20:16 +090056 private String json(AbstractShellCommand context, List<ExternalPeerRouter> routers) {
Jian Li5e2ad4a2018-07-16 13:40:53 +090057 ObjectMapper mapper = new ObjectMapper();
Jian Lif1efbe52018-07-17 23:20:16 +090058 ArrayNode result = mapper.createArrayNode();
Jian Li5e2ad4a2018-07-16 13:40:53 +090059 routers.forEach(r -> result.add(context.jsonForEntity(r, ExternalPeerRouter.class)));
60
Jian Lif1efbe52018-07-17 23:20:16 +090061 return prettyJson(mapper, result.toString());
Jian Li5e2ad4a2018-07-16 13:40:53 +090062 }
daniel parkb5817102018-02-15 00:18:51 +090063}