blob: 9e914910da51467d7261cb5b7cfce11198a4691d [file] [log] [blame]
Jian Li36a0c4d2018-11-14 00:28:17 +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
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.google.common.collect.Lists;
21import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknetworking.api.OpenstackRouterService;
25import org.openstack4j.model.network.RouterInterface;
26import org.openstack4j.openstack.networking.domain.NeutronRouter;
27
28import java.util.List;
29
30import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.modelEntityToJson;
31import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
32
33/**
34 * Lists Openstack router interfaces.
35 */
36@Service
37@Command(scope = "onos", name = "openstack-router-interfaces",
38 description = "Lists all OpenStack routers interfaces")
39public class OpenstackRouterInterfaceListCommand extends AbstractShellCommand {
40 private static final String FORMAT = "%-40s%-40s%-40s%-40s%-40s";
41
42 private final OpenstackRouterService routerService =
43 AbstractShellCommand.get(OpenstackRouterService.class);
44
45 @Override
46 protected void doExecute() {
47 List<RouterInterface> routerInterfaces =
48 Lists.newArrayList(routerService.routerInterfaces());
49
50 if (outputJson()) {
51 print("%s", json(routerInterfaces));
52 } else {
53 print(FORMAT, "ID", "RouterName", "SubnetID", "PortID", "TenantID");
54 for (RouterInterface routerInterface: routerInterfaces) {
55 print(FORMAT, routerInterface.getId(),
56 routerService.router(routerInterface.getId()),
57 routerInterface.getSubnetId(),
58 routerInterface.getPortId(),
59 routerInterface.getTenantId());
60 }
61 }
62 }
63
64 private String json(List<RouterInterface> routerInterfaces) {
65 ObjectMapper mapper = new ObjectMapper();
66 ArrayNode result = mapper.createArrayNode();
67 for (RouterInterface routerInterface: routerInterfaces) {
68 result.add(modelEntityToJson(routerInterface, NeutronRouter.class));
69 }
70 return prettyJson(mapper, result.toString());
71 }
72}