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; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.collect.Lists; |
| 23 | import org.apache.karaf.shell.commands.Argument; |
| 24 | import org.apache.karaf.shell.commands.Command; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService; |
| 27 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 28 | import org.openstack4j.model.network.SecurityGroup; |
| 29 | import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup; |
| 30 | |
| 31 | import java.util.Comparator; |
| 32 | import java.util.List; |
| 33 | |
| 34 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 35 | |
| 36 | /** |
| 37 | * Lists OpenStack security groups. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "openstack-security-groups", |
| 40 | description = "Lists all OpenStack security groups") |
| 41 | public 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 |
| 49 | protected void execute() { |
| 50 | 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()) { |
| 57 | try { |
| 58 | print("%s", mapper().writeValueAsString(json(sgs))); |
| 59 | } catch (JsonProcessingException e) { |
| 60 | error("Failed to list security groups in JSON format"); |
| 61 | } |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | print("Hint: use --json option to see security group rules as well\n"); |
| 66 | print(FORMAT, "ID", "Name"); |
| 67 | for (SecurityGroup sg: service.securityGroups()) { |
| 68 | print(FORMAT, sg.getId(), sg.getName()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private JsonNode json(List<SecurityGroup> securityGroups) { |
| 73 | ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode(); |
| 74 | for (SecurityGroup sg: securityGroups) { |
| 75 | result.add(writeSecurityGroup(sg)); |
| 76 | } |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | private ObjectNode writeSecurityGroup(SecurityGroup sg) { |
| 81 | try { |
| 82 | String strSg = ObjectMapperSingleton.getContext(NeutronSecurityGroup.class) |
| 83 | .writerFor(NeutronSecurityGroup.class) |
| 84 | .writeValueAsString(sg); |
| 85 | return (ObjectNode) mapper().readTree(strSg.getBytes()); |
| 86 | } catch (Exception e) { |
| 87 | throw new IllegalArgumentException(); |
| 88 | } |
| 89 | } |
| 90 | } |