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 | |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Argument; |
| 22 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 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 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 32 | import static org.onosproject.cli.AbstractShellCommand.get; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 33 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.modelEntityToJson; |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 34 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Lists OpenStack security groups. |
| 38 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 39 | @Service |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 40 | @Command(scope = "onos", name = "openstack-security-groups", |
| 41 | description = "Lists all OpenStack security groups") |
| 42 | public class OpenstackSecurityGroupListCommand extends AbstractShellCommand { |
| 43 | |
| 44 | private static final String FORMAT = "%-40s%-20s"; |
| 45 | |
| 46 | @Argument(name = "networkId", description = "Network ID") |
| 47 | private String networkId = null; |
| 48 | |
| 49 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 50 | protected void doExecute() { |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 51 | OpenstackSecurityGroupService service = get(OpenstackSecurityGroupService.class); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 52 | |
| 53 | List<SecurityGroup> sgs = Lists.newArrayList(service.securityGroups()); |
| 54 | sgs.sort(Comparator.comparing(SecurityGroup::getId)); |
| 55 | |
| 56 | if (outputJson()) { |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 57 | 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 Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 63 | } |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 67 | private String json(List<SecurityGroup> securityGroups) { |
| 68 | ObjectMapper mapper = new ObjectMapper(); |
| 69 | ArrayNode result = mapper.createArrayNode(); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 70 | for (SecurityGroup sg: securityGroups) { |
Jian Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 71 | result.add(modelEntityToJson(sg, NeutronSecurityGroup.class)); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 72 | } |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 73 | return prettyJson(mapper, result.toString()); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 74 | } |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 75 | } |