blob: 9342bca78b38c06f0071037e9274ee3aa81a9b7c [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
18import com.fasterxml.jackson.core.JsonProcessingException;
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ArrayNode;
Hyunsun Moonae51e732017-04-25 17:46:21 +090021import com.google.common.collect.Lists;
22import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
24import 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
32import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
Jian Lieb9f77d2018-02-20 11:25:45 +090033import static org.onosproject.openstacknetworking.util.OpenstackUtil.modelEntityToJson;
Hyunsun Moonae51e732017-04-25 17:46:21 +090034
35/**
36 * Lists OpenStack security groups.
37 */
38@Command(scope = "onos", name = "openstack-security-groups",
39 description = "Lists all OpenStack security groups")
40public class OpenstackSecurityGroupListCommand extends AbstractShellCommand {
41
42 private static final String FORMAT = "%-40s%-20s";
43
44 @Argument(name = "networkId", description = "Network ID")
45 private String networkId = null;
46
47 @Override
48 protected void execute() {
49 OpenstackSecurityGroupService service =
50 AbstractShellCommand.get(OpenstackSecurityGroupService.class);
51
52 List<SecurityGroup> sgs = Lists.newArrayList(service.securityGroups());
53 sgs.sort(Comparator.comparing(SecurityGroup::getId));
54
55 if (outputJson()) {
56 try {
57 print("%s", mapper().writeValueAsString(json(sgs)));
58 } catch (JsonProcessingException e) {
59 error("Failed to list security groups in JSON format");
60 }
61 return;
62 }
63
64 print("Hint: use --json option to see security group rules as well\n");
65 print(FORMAT, "ID", "Name");
66 for (SecurityGroup sg: service.securityGroups()) {
67 print(FORMAT, sg.getId(), sg.getName());
68 }
69 }
70
71 private JsonNode json(List<SecurityGroup> securityGroups) {
72 ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode();
73 for (SecurityGroup sg: securityGroups) {
Jian Lieb9f77d2018-02-20 11:25:45 +090074 result.add(modelEntityToJson(sg, NeutronSecurityGroup.class));
Hyunsun Moonae51e732017-04-25 17:46:21 +090075 }
76 return result;
77 }
Hyunsun Moonae51e732017-04-25 17:46:21 +090078}