blob: 4ac752dc99209b8559876f58fb87b0cdfb7ea149 [file] [log] [blame]
Jian Li47e7af72021-03-05 01:32:04 +09001/*
2 * Copyright 2021-present Open Networking Foundation
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 */
16package org.onosproject.kubevirtnetworking.cli;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Completion;
21import org.apache.karaf.shell.api.action.Option;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroup;
25import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupRule;
26import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupService;
27
28import java.util.List;
29
30import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
31
32/**
33 * Show a detailed security group info.
34 */
35@Service
36@Command(scope = "onos", name = "kubevirt-security-group",
37 description = "Displays a security group details")
38public class KubevirtShowSecurityGroupCommand extends AbstractShellCommand {
39
40 @Option(name = "--name",
41 description = "Filter security group by specific name", multiValued = true)
42 @Completion(KubevirtSecurityGroupCompleter.class)
43 private List<String> names;
44
45 @Override
46 protected void doExecute() throws Exception {
47 KubevirtSecurityGroupService service = get(KubevirtSecurityGroupService.class);
48
49 if (names == null || names.size() == 0) {
50 print("Need to specify at least one security group name using --name option.");
51 return;
52 }
53
54 for (String name : names) {
55 KubevirtSecurityGroup sg = service.securityGroups().stream()
56 .filter(s -> s.name().equals(name))
57 .findAny().orElse(null);
58 if (sg == null) {
59 print("Unable to find %s", name);
60 continue;
61 }
62
63 if (outputJson()) {
64 print("%s", json(sg));
65 } else {
66 printSecurityGroup(sg);
67 }
68 }
69 }
70
71 private void printSecurityGroup(KubevirtSecurityGroup sg) {
72 print("Name: %s", sg.name());
73 print(" ID: %s", sg.id());
74 print(" Description: %s", sg.description());
75
76 int counter = 1;
77 for (KubevirtSecurityGroupRule rule : sg.rules()) {
78 print(" Rule #%d:", counter);
79 print(" ID: %s", rule.id());
80 print(" Direction: %s", rule.direction());
81 print(" EtherType: %s", rule.etherType());
82 print(" Protocol: %s", rule.protocol());
83 print(" PortRangeMax: %s", rule.portRangeMax());
84 print(" PortRangeMin: %s", rule.portRangeMin());
85 print(" RemoteIpPrefix: %s", rule.remoteIpPrefix());
86 print(" RemoteGroupID: %s", rule.remoteGroupId());
87 counter++;
88 }
89 }
90
91 private String json(KubevirtSecurityGroup sg) {
92 return prettyJson(new ObjectMapper(),
93 jsonForEntity(sg, KubevirtSecurityGroup.class).toString());
94 }
95}