blob: 9f67ef99fb9c94b556bd8cf81c612dd5a60c1093 [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;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070021import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
daniel parkb5817102018-02-15 00:18:51 +090023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknetworking.api.ExternalPeerRouter;
25import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
26
27import java.util.List;
28
Jian Lif1efbe52018-07-17 23:20:16 +090029import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
Jian Li5e2ad4a2018-07-16 13:40:53 +090030
daniel parkb5817102018-02-15 00:18:51 +090031/**
32 * Lists external peer router lists.
33 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070034@Service
daniel parkb5817102018-02-15 00:18:51 +090035@Command(scope = "onos", name = "openstack-peer-routers",
36 description = "Lists external peer router lists")
37public class ExternalPeerRouterListCommand extends AbstractShellCommand {
38
39 private static final String FORMAT = "%-20s%-20s%-20s";
40
41 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070042 protected void doExecute() {
daniel parkb5817102018-02-15 00:18:51 +090043 OpenstackNetworkService service = AbstractShellCommand.get(OpenstackNetworkService.class);
daniel parkb5817102018-02-15 00:18:51 +090044 List<ExternalPeerRouter> routers = Lists.newArrayList(service.externalPeerRouters());
45
Jian Li5e2ad4a2018-07-16 13:40:53 +090046 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 parkb5817102018-02-15 00:18:51 +090055 }
56 }
Jian Li5e2ad4a2018-07-16 13:40:53 +090057
Jian Lif1efbe52018-07-17 23:20:16 +090058 private String json(AbstractShellCommand context, List<ExternalPeerRouter> routers) {
Jian Li5e2ad4a2018-07-16 13:40:53 +090059 ObjectMapper mapper = new ObjectMapper();
Jian Lif1efbe52018-07-17 23:20:16 +090060 ArrayNode result = mapper.createArrayNode();
Jian Li5e2ad4a2018-07-16 13:40:53 +090061 routers.forEach(r -> result.add(context.jsonForEntity(r, ExternalPeerRouter.class)));
62
Jian Lif1efbe52018-07-17 23:20:16 +090063 return prettyJson(mapper, result.toString());
Jian Li5e2ad4a2018-07-16 13:40:53 +090064 }
daniel parkb5817102018-02-15 00:18:51 +090065}