blob: e772840e4e60dc86002a868807b39c195662d7e0 [file] [log] [blame]
Hyunsun Moonae51e732017-04-25 17:46:21 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moonae51e732017-04-25 17:46:21 +09003 *
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 Lif1efbe52018-07-17 23:20:16 +090018import com.fasterxml.jackson.databind.ObjectMapper;
Hyunsun Moonae51e732017-04-25 17:46:21 +090019import com.fasterxml.jackson.databind.node.ArrayNode;
Hyunsun Moonae51e732017-04-25 17:46:21 +090020import com.google.common.collect.Lists;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070021import org.apache.karaf.shell.api.action.Argument;
22import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Hyunsun Moonae51e732017-04-25 17:46:21 +090024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService;
Hyunsun Moonae51e732017-04-25 17:46:21 +090026import org.openstack4j.model.network.SecurityGroup;
27import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
28
29import java.util.Comparator;
30import java.util.List;
31
Jian Lidea0fdb2018-04-02 19:02:48 +090032import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.modelEntityToJson;
Jian Lif1efbe52018-07-17 23:20:16 +090033import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
Hyunsun Moonae51e732017-04-25 17:46:21 +090034
35/**
36 * Lists OpenStack security groups.
37 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070038@Service
Hyunsun Moonae51e732017-04-25 17:46:21 +090039@Command(scope = "onos", name = "openstack-security-groups",
40 description = "Lists all OpenStack security groups")
41public class OpenstackSecurityGroupListCommand extends AbstractShellCommand {
42
43 private static final String FORMAT = "%-40s%-20s";
44
45 @Argument(name = "networkId", description = "Network ID")
46 private String networkId = null;
47
48 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070049 protected void doExecute() {
Hyunsun Moonae51e732017-04-25 17:46:21 +090050 OpenstackSecurityGroupService service =
51 AbstractShellCommand.get(OpenstackSecurityGroupService.class);
52
53 List<SecurityGroup> sgs = Lists.newArrayList(service.securityGroups());
54 sgs.sort(Comparator.comparing(SecurityGroup::getId));
55
56 if (outputJson()) {
Jian Lif1efbe52018-07-17 23:20:16 +090057 print("%s", json(sgs));
58 } else {
59 print("Hint: use --json option to see security group rules as well\n");
60 print(FORMAT, "ID", "Name");
61 for (SecurityGroup sg: service.securityGroups()) {
62 print(FORMAT, sg.getId(), sg.getName());
Hyunsun Moonae51e732017-04-25 17:46:21 +090063 }
Hyunsun Moonae51e732017-04-25 17:46:21 +090064 }
65 }
66
Jian Lif1efbe52018-07-17 23:20:16 +090067 private String json(List<SecurityGroup> securityGroups) {
68 ObjectMapper mapper = new ObjectMapper();
69 ArrayNode result = mapper.createArrayNode();
Hyunsun Moonae51e732017-04-25 17:46:21 +090070 for (SecurityGroup sg: securityGroups) {
Jian Lieb9f77d2018-02-20 11:25:45 +090071 result.add(modelEntityToJson(sg, NeutronSecurityGroup.class));
Hyunsun Moonae51e732017-04-25 17:46:21 +090072 }
Jian Lif1efbe52018-07-17 23:20:16 +090073 return prettyJson(mapper, result.toString());
Hyunsun Moonae51e732017-04-25 17:46:21 +090074 }
Hyunsun Moonae51e732017-04-25 17:46:21 +090075}