blob: 4cd216d70361597a841f2b61e6b62461d6ec2785 [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;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.google.common.collect.Lists;
23import org.apache.karaf.shell.commands.Argument;
24import org.apache.karaf.shell.commands.Command;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService;
27import org.openstack4j.core.transport.ObjectMapperSingleton;
28import org.openstack4j.model.network.SecurityGroup;
29import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
30
31import java.util.Comparator;
32import java.util.List;
33
34import 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")
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
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}