blob: 3f1b78b8e3dab1dabfb74b41add81ec0acd4d62b [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 com.fasterxml.jackson.databind.node.ArrayNode;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.Lists;
22import org.apache.commons.lang.StringUtils;
23import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroup;
27import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupService;
28
29import java.util.Comparator;
30import java.util.List;
31
32import static org.onosproject.kubevirtnetworking.api.Constants.CLI_ID_LENGTH;
33import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH;
34import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH;
35import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NUMBER_LENGTH;
36import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString;
37import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
38
39/**
40 * Lists kubevirt security groups.
41 */
42@Service
43@Command(scope = "onos", name = "kubevirt-security-groups",
44 description = "Lists all kubevirt security groups")
45public class KubevirtListSecurityGroupCommand extends AbstractShellCommand {
46 @Override
47 protected void doExecute() throws Exception {
48 KubevirtSecurityGroupService service = get(KubevirtSecurityGroupService.class);
49 List<KubevirtSecurityGroup> sgs = Lists.newArrayList(service.securityGroups());
50 sgs.sort(Comparator.comparing(KubevirtSecurityGroup::name));
51
52 String format = genFormatString(ImmutableList.of(CLI_ID_LENGTH,
53 CLI_NAME_LENGTH, CLI_NUMBER_LENGTH));
54
55 if (outputJson()) {
56 print("%s", json(sgs));
57 } else {
58 print(format, "ID", "Name", "# of Rules");
59
60 for (KubevirtSecurityGroup sg : sgs) {
61 print(format, StringUtils.substring(sg.id(), 0,
62 CLI_ID_LENGTH - CLI_MARGIN_LENGTH),
63 StringUtils.substring(sg.name(), 0,
64 CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
65 StringUtils.substring(String.valueOf(sg.rules().size()), 0,
66 CLI_NUMBER_LENGTH - CLI_MARGIN_LENGTH)
67 );
68 }
69 }
70 }
71
72 private String json(List<KubevirtSecurityGroup> sgs) {
73 ObjectMapper mapper = new ObjectMapper();
74 ArrayNode result = mapper.createArrayNode();
75
76 for (KubevirtSecurityGroup sg : sgs) {
77 result.add(jsonForEntity(sg, KubevirtSecurityGroup.class));
78 }
79
80 return prettyJson(mapper, result.toString());
81 }
82}