Implement security group manager, codec and watcher with unit tests
Change-Id: Ib2201d140b9dcb2eff453f13447113bdba66babd
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListSecurityGroupCommand.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListSecurityGroupCommand.java
new file mode 100644
index 0000000..3f1b78b
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListSecurityGroupCommand.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.kubevirtnetworking.cli;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroup;
+import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupService;
+
+import java.util.Comparator;
+import java.util.List;
+
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_ID_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NUMBER_LENGTH;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
+
+/**
+ * Lists kubevirt security groups.
+ */
+@Service
+@Command(scope = "onos", name = "kubevirt-security-groups",
+ description = "Lists all kubevirt security groups")
+public class KubevirtListSecurityGroupCommand extends AbstractShellCommand {
+ @Override
+ protected void doExecute() throws Exception {
+ KubevirtSecurityGroupService service = get(KubevirtSecurityGroupService.class);
+ List<KubevirtSecurityGroup> sgs = Lists.newArrayList(service.securityGroups());
+ sgs.sort(Comparator.comparing(KubevirtSecurityGroup::name));
+
+ String format = genFormatString(ImmutableList.of(CLI_ID_LENGTH,
+ CLI_NAME_LENGTH, CLI_NUMBER_LENGTH));
+
+ if (outputJson()) {
+ print("%s", json(sgs));
+ } else {
+ print(format, "ID", "Name", "# of Rules");
+
+ for (KubevirtSecurityGroup sg : sgs) {
+ print(format, StringUtils.substring(sg.id(), 0,
+ CLI_ID_LENGTH - CLI_MARGIN_LENGTH),
+ StringUtils.substring(sg.name(), 0,
+ CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+ StringUtils.substring(String.valueOf(sg.rules().size()), 0,
+ CLI_NUMBER_LENGTH - CLI_MARGIN_LENGTH)
+ );
+ }
+ }
+ }
+
+ private String json(List<KubevirtSecurityGroup> sgs) {
+ ObjectMapper mapper = new ObjectMapper();
+ ArrayNode result = mapper.createArrayNode();
+
+ for (KubevirtSecurityGroup sg : sgs) {
+ result.add(jsonForEntity(sg, KubevirtSecurityGroup.class));
+ }
+
+ return prettyJson(mapper, result.toString());
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtSecurityGroupCompleter.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtSecurityGroupCompleter.java
new file mode 100644
index 0000000..893605f
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtSecurityGroupCompleter.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.kubevirtnetworking.cli;
+
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.CommandLine;
+import org.apache.karaf.shell.api.console.Completer;
+import org.apache.karaf.shell.api.console.Session;
+import org.apache.karaf.shell.support.completers.StringsCompleter;
+import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroup;
+import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupService;
+
+import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.stream.Collectors;
+
+import static org.onosproject.cli.AbstractShellCommand.get;
+
+/**
+ * Kubevirt security group name completer.
+ */
+@Service
+public class KubevirtSecurityGroupCompleter implements Completer {
+ @Override
+ public int complete(Session session, CommandLine commandLine, List<String> candidates) {
+ StringsCompleter delegate = new StringsCompleter();
+ KubevirtSecurityGroupService service = get(KubevirtSecurityGroupService.class);
+
+ Set<String> sgNames = service.securityGroups().stream()
+ .map(KubevirtSecurityGroup::name).collect(Collectors.toSet());
+
+ SortedSet<String> strings = delegate.getStrings();
+
+ strings.addAll(sgNames);
+
+ return delegate.complete(session, commandLine, candidates);
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtShowPodCommand.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtShowPodCommand.java
index 514e651..c8bf0a9 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtShowPodCommand.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtShowPodCommand.java
@@ -85,6 +85,7 @@
print(" Pull Policy: %s", container.getImagePullPolicy());
print(" Commands: %s", container.getCommand());
print(" Args: %s", container.getArgs());
+ counter++;
}
}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtShowSecurityGroupCommand.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtShowSecurityGroupCommand.java
new file mode 100644
index 0000000..4ac752d
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtShowSecurityGroupCommand.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.kubevirtnetworking.cli;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroup;
+import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupRule;
+import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupService;
+
+import java.util.List;
+
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
+
+/**
+ * Show a detailed security group info.
+ */
+@Service
+@Command(scope = "onos", name = "kubevirt-security-group",
+ description = "Displays a security group details")
+public class KubevirtShowSecurityGroupCommand extends AbstractShellCommand {
+
+ @Option(name = "--name",
+ description = "Filter security group by specific name", multiValued = true)
+ @Completion(KubevirtSecurityGroupCompleter.class)
+ private List<String> names;
+
+ @Override
+ protected void doExecute() throws Exception {
+ KubevirtSecurityGroupService service = get(KubevirtSecurityGroupService.class);
+
+ if (names == null || names.size() == 0) {
+ print("Need to specify at least one security group name using --name option.");
+ return;
+ }
+
+ for (String name : names) {
+ KubevirtSecurityGroup sg = service.securityGroups().stream()
+ .filter(s -> s.name().equals(name))
+ .findAny().orElse(null);
+ if (sg == null) {
+ print("Unable to find %s", name);
+ continue;
+ }
+
+ if (outputJson()) {
+ print("%s", json(sg));
+ } else {
+ printSecurityGroup(sg);
+ }
+ }
+ }
+
+ private void printSecurityGroup(KubevirtSecurityGroup sg) {
+ print("Name: %s", sg.name());
+ print(" ID: %s", sg.id());
+ print(" Description: %s", sg.description());
+
+ int counter = 1;
+ for (KubevirtSecurityGroupRule rule : sg.rules()) {
+ print(" Rule #%d:", counter);
+ print(" ID: %s", rule.id());
+ print(" Direction: %s", rule.direction());
+ print(" EtherType: %s", rule.etherType());
+ print(" Protocol: %s", rule.protocol());
+ print(" PortRangeMax: %s", rule.portRangeMax());
+ print(" PortRangeMin: %s", rule.portRangeMin());
+ print(" RemoteIpPrefix: %s", rule.remoteIpPrefix());
+ print(" RemoteGroupID: %s", rule.remoteGroupId());
+ counter++;
+ }
+ }
+
+ private String json(KubevirtSecurityGroup sg) {
+ return prettyJson(new ObjectMapper(),
+ jsonForEntity(sg, KubevirtSecurityGroup.class).toString());
+ }
+}