Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.openstacknetworking.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 26 | import org.openstack4j.model.network.SecurityGroup; |
| 27 | import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup; |
| 28 | |
| 29 | import java.util.Comparator; |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
Jian Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 33 | import static org.onosproject.openstacknetworking.util.OpenstackUtil.modelEntityToJson; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Lists OpenStack security groups. |
| 37 | */ |
| 38 | @Command(scope = "onos", name = "openstack-security-groups", |
| 39 | description = "Lists all OpenStack security groups") |
| 40 | public 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 Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 74 | result.add(modelEntityToJson(sg, NeutronSecurityGroup.class)); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 75 | } |
| 76 | return result; |
| 77 | } |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 78 | } |