Support to store and monitor kubevirt POD resource
Change-Id: I2b484a806549620c5b1352da63061cd850b0284c
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java
index 7a5d873..0ff5fec 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java
@@ -63,6 +63,7 @@
public static final int CLI_MAC_ADDRESS_LENGTH = 25;
public static final int CLI_PORTS_LENGTH = 20;
public static final int CLI_NAMESPACE_LENGTH = 15;
+ public static final int CLI_STATUS_LENGTH = 15;
public static final int CLI_PHASE_LENGTH = 15;
public static final int CLI_TYPE_LENGTH = 15;
public static final int CLI_TYPES_LENGTH = 30;
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodAdminService.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodAdminService.java
new file mode 100644
index 0000000..668081a
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodAdminService.java
@@ -0,0 +1,50 @@
+/*
+ * 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.api;
+
+import io.fabric8.kubernetes.api.model.Pod;
+
+/**
+ * Service for administering the inventory of kubevirt service.
+ */
+public interface KubevirtPodAdminService extends KubevirtPodService {
+
+ /**
+ * Creates a kubevirt service with the given information.
+ *
+ * @param pod the new kubevirt pod
+ */
+ void createPod(Pod pod);
+
+ /**
+ * Updates the kubevirt pod with the given information.
+ *
+ * @param pod the updated kubevirt pod
+ */
+ void updatePod(Pod pod);
+
+ /**
+ * Removes the kubevirt pod.
+ *
+ * @param uid kubevirt pod UID
+ */
+ void removePod(String uid);
+
+ /**
+ * Clears the existing pods.
+ */
+ void clear();
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodEvent.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodEvent.java
new file mode 100644
index 0000000..a1f5b83
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodEvent.java
@@ -0,0 +1,92 @@
+/*
+ * 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.api;
+
+import io.fabric8.kubernetes.api.model.Pod;
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * Kubevirt pod event class.
+ */
+public class KubevirtPodEvent extends AbstractEvent<KubevirtPodEvent.Type, Pod> {
+
+ /**
+ * Creates an event of a given type for the specified kubevirt pod.
+ *
+ * @param type kubevirt pod event type
+ * @param subject kubevirt pod
+ */
+ public KubevirtPodEvent(Type type, Pod subject) {
+ super(type, subject);
+ }
+
+ public enum Type {
+ /**
+ * Signifies that a new kubevirt pod is created.
+ */
+ KUBEVIRT_POD_CREATED,
+
+ /**
+ * Signifies that the kubevirt pod is updated.
+ */
+ KUBEVIRT_POD_UPDATED,
+
+ /**
+ * Signifies that the kubevirt pod is in Pending phase.
+ */
+ KUBEVIRT_POD_PENDING,
+
+ /**
+ * Signifies that the kubevirt pod is in Running phase.
+ */
+ KUBEVIRT_POD_RUNNING,
+
+ /**
+ * Signifies that the kubevirt pod is in Succeeded phase.
+ */
+ KUBEVIRT_POD_SUCCEEDED,
+
+ /**
+ * Signifies that the kubevirt pod is in Failed phase.
+ */
+ KUBEVIRT_POD_FAILED,
+
+ /**
+ * Signifies that the kubevirt pod is in Unknown phase.
+ */
+ KUBEVIRT_POD_UNKNOWN,
+
+ /**
+ * Signifies that the kubevirt pod is in Completed phase.
+ */
+ KUBEVIRT_POD_COMPLETED,
+
+ /**
+ * Signifies that the kubevirt pod is in CrashLoopBackOff phase.
+ */
+ KUBEVIRT_POD_CRASH_LOOP_BACK_OFF,
+
+ /**
+ * Signifies that the kubevirt pod annotation is added.
+ */
+ KUBEVIRT_POD_ANNOTATION_ADDED,
+
+ /**
+ * Signifies that the kubevirt pod is removed.
+ */
+ KUBEVIRT_POD_REMOVED,
+ }
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodListener.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodListener.java
new file mode 100644
index 0000000..5f980b6
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodListener.java
@@ -0,0 +1,24 @@
+/*
+ * 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.api;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Listener for kubevirt pod event.
+ */
+public interface KubevirtPodListener extends EventListener<KubevirtPodEvent> {
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodService.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodService.java
new file mode 100644
index 0000000..5c99d5c
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodService.java
@@ -0,0 +1,43 @@
+/*
+ * 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.api;
+
+import io.fabric8.kubernetes.api.model.Pod;
+import org.onosproject.event.ListenerService;
+
+import java.util.Set;
+
+/**
+ * Service for interacting with the inventory of kubevirt service.
+ */
+public interface KubevirtPodService
+ extends ListenerService<KubevirtPodEvent, KubevirtPodListener> {
+
+ /**
+ * Returns the kubevirt pod with the supplied pod UID.
+ *
+ * @param uid pod UID
+ * @return kubevirt pod
+ */
+ Pod pod(String uid);
+
+ /**
+ * Returns all kubevirt pods registered.
+ *
+ * @return set of kubevirt pods
+ */
+ Set<Pod> pods();
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodStore.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodStore.java
new file mode 100644
index 0000000..6376f3e
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodStore.java
@@ -0,0 +1,69 @@
+/*
+ * 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.api;
+
+import io.fabric8.kubernetes.api.model.Pod;
+import org.onosproject.store.Store;
+
+import java.util.Set;
+
+/**
+ * Manages inventory of kubevirt pod; not intended for direct use.
+ */
+public interface KubevirtPodStore extends Store<KubevirtPodEvent, KubevirtPodStoreDelegate> {
+
+ /**
+ * Creates the new kubevirt pod.
+ *
+ * @param pod kubevirt pod
+ */
+ void createPod(Pod pod);
+
+ /**
+ * Updates the kubevirt pod.
+ *
+ * @param pod kubevirt pod
+ */
+ void updatePod(Pod pod);
+
+ /**
+ * Removes the kubevirt pod with the given pod UID.
+ *
+ * @param uid kubevirt pod UID
+ * @return removed kubevirt pod; null if failed
+ */
+ Pod removePod(String uid);
+
+ /**
+ * Returns the kubevirt pod with the given pod UID.
+ *
+ * @param uid kubevirt pod UID
+ * @return kubevirt pod; null if not found
+ */
+ Pod pod(String uid);
+
+ /**
+ * Returns all kubevirt pods.
+ *
+ * @return set of kubevirt pods
+ */
+ Set<Pod> pods();
+
+ /**
+ * Removes all kubevirt pods.
+ */
+ void clear();
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodStoreDelegate.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodStoreDelegate.java
new file mode 100644
index 0000000..5eb7f0d
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtPodStoreDelegate.java
@@ -0,0 +1,24 @@
+/*
+ * 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.api;
+
+import org.onosproject.store.StoreDelegate;
+
+/**
+ * Kubevirt pod store delegate abstraction.
+ */
+public interface KubevirtPodStoreDelegate extends StoreDelegate<KubevirtPodEvent> {
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListPodCommand.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListPodCommand.java
new file mode 100644
index 0000000..2ad937a
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListPodCommand.java
@@ -0,0 +1,97 @@
+/*
+ * 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.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import io.fabric8.kubernetes.api.model.Pod;
+import io.fabric8.kubernetes.client.utils.Serialization;
+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.KubevirtPodService;
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.List;
+
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAMESPACE_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_STATUS_LENGTH;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
+
+/**
+ * Lists kubevirt pods.
+ */
+@Service
+@Command(scope = "onos", name = "kubevirt-pods",
+ description = "Lists all kubevirt pods")
+public class KubevirtListPodCommand extends AbstractShellCommand {
+ @Override
+ protected void doExecute() throws Exception {
+ KubevirtPodService service = get(KubevirtPodService.class);
+ List<Pod> pods = Lists.newArrayList(service.pods());
+ pods.sort(Comparator.comparing(p -> p.getMetadata().getName()));
+
+ String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
+ CLI_NAMESPACE_LENGTH, CLI_STATUS_LENGTH, CLI_IP_ADDRESS_LENGTH));
+
+ if (outputJson()) {
+ print("%s", json(pods));
+ } else {
+ print(format, "Name", "Namespace", "Status", "IP Address");
+
+ for (Pod pod : pods) {
+
+ List<String> containers = Lists.newArrayList();
+
+ pod.getSpec().getContainers().forEach(c -> containers.add(c.getName()));
+
+ print(format,
+ StringUtils.substring(pod.getMetadata().getName(),
+ 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+ StringUtils.substring(pod.getMetadata().getNamespace(),
+ 0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH),
+ pod.getStatus().getPhase(),
+ StringUtils.substring(pod.getStatus().getPodIP(),
+ 0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH));
+ }
+ }
+ }
+
+ private String json(List<Pod> pods) {
+ ObjectMapper mapper = new ObjectMapper();
+ ArrayNode result = mapper.createArrayNode();
+ try {
+ for (Pod pod : pods) {
+ ObjectNode json = (ObjectNode) new ObjectMapper()
+ .readTree(Serialization.asJson(pod));
+ result.add(json);
+ }
+ return prettyJson(mapper, result.toString());
+ } catch (IOException e) {
+ log.warn("Failed to parse POD's JSON string.");
+ return "";
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubevirtPodStore.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubevirtPodStore.java
new file mode 100644
index 0000000..3b1a2fc
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubevirtPodStore.java
@@ -0,0 +1,365 @@
+/*
+ * 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.impl;
+
+import com.google.common.collect.ImmutableSet;
+import io.fabric8.kubernetes.api.model.Affinity;
+import io.fabric8.kubernetes.api.model.Capabilities;
+import io.fabric8.kubernetes.api.model.ConfigMapEnvSource;
+import io.fabric8.kubernetes.api.model.ConfigMapKeySelector;
+import io.fabric8.kubernetes.api.model.ConfigMapVolumeSource;
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerPort;
+import io.fabric8.kubernetes.api.model.ContainerState;
+import io.fabric8.kubernetes.api.model.ContainerStateRunning;
+import io.fabric8.kubernetes.api.model.ContainerStateTerminated;
+import io.fabric8.kubernetes.api.model.ContainerStateWaiting;
+import io.fabric8.kubernetes.api.model.ContainerStatus;
+import io.fabric8.kubernetes.api.model.EmptyDirVolumeSource;
+import io.fabric8.kubernetes.api.model.EnvFromSource;
+import io.fabric8.kubernetes.api.model.EnvVar;
+import io.fabric8.kubernetes.api.model.EnvVarSource;
+import io.fabric8.kubernetes.api.model.ExecAction;
+import io.fabric8.kubernetes.api.model.FieldsV1;
+import io.fabric8.kubernetes.api.model.HTTPGetAction;
+import io.fabric8.kubernetes.api.model.HTTPHeader;
+import io.fabric8.kubernetes.api.model.HostPathVolumeSource;
+import io.fabric8.kubernetes.api.model.IntOrString;
+import io.fabric8.kubernetes.api.model.KeyToPath;
+import io.fabric8.kubernetes.api.model.LabelSelector;
+import io.fabric8.kubernetes.api.model.LabelSelectorRequirement;
+import io.fabric8.kubernetes.api.model.Lifecycle;
+import io.fabric8.kubernetes.api.model.ManagedFieldsEntry;
+import io.fabric8.kubernetes.api.model.NodeAffinity;
+import io.fabric8.kubernetes.api.model.NodeSelector;
+import io.fabric8.kubernetes.api.model.NodeSelectorRequirement;
+import io.fabric8.kubernetes.api.model.NodeSelectorTerm;
+import io.fabric8.kubernetes.api.model.ObjectFieldSelector;
+import io.fabric8.kubernetes.api.model.ObjectMeta;
+import io.fabric8.kubernetes.api.model.OwnerReference;
+import io.fabric8.kubernetes.api.model.PersistentVolumeClaimVolumeSource;
+import io.fabric8.kubernetes.api.model.Pod;
+import io.fabric8.kubernetes.api.model.PodAffinity;
+import io.fabric8.kubernetes.api.model.PodAffinityTerm;
+import io.fabric8.kubernetes.api.model.PodAntiAffinity;
+import io.fabric8.kubernetes.api.model.PodCondition;
+import io.fabric8.kubernetes.api.model.PodIP;
+import io.fabric8.kubernetes.api.model.PodSecurityContext;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodStatus;
+import io.fabric8.kubernetes.api.model.PreferredSchedulingTerm;
+import io.fabric8.kubernetes.api.model.Probe;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceFieldSelector;
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import io.fabric8.kubernetes.api.model.SELinuxOptions;
+import io.fabric8.kubernetes.api.model.SecretEnvSource;
+import io.fabric8.kubernetes.api.model.SecretKeySelector;
+import io.fabric8.kubernetes.api.model.SecretVolumeSource;
+import io.fabric8.kubernetes.api.model.SecurityContext;
+import io.fabric8.kubernetes.api.model.TCPSocketAction;
+import io.fabric8.kubernetes.api.model.Toleration;
+import io.fabric8.kubernetes.api.model.Volume;
+import io.fabric8.kubernetes.api.model.VolumeDevice;
+import io.fabric8.kubernetes.api.model.VolumeMount;
+import io.fabric8.kubernetes.api.model.WeightedPodAffinityTerm;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodEvent;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodStore;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodStoreDelegate;
+import org.onosproject.store.AbstractStore;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.MapEvent;
+import org.onosproject.store.service.MapEventListener;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Versioned;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_ANNOTATION_ADDED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_COMPLETED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_CRASH_LOOP_BACK_OFF;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_CREATED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_FAILED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_PENDING;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_REMOVED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_RUNNING;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_SUCCEEDED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_UNKNOWN;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_UPDATED;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of kubevirt pod store using consistent map.
+ */
+@Component(immediate = true, service = KubevirtPodStore.class)
+public class DistributedKubevirtPodStore
+ extends AbstractStore<KubevirtPodEvent, KubevirtPodStoreDelegate>
+ implements KubevirtPodStore {
+ private final Logger log = getLogger(getClass());
+
+ private static final String ERR_NOT_FOUND = " does not exist";
+ private static final String ERR_DUPLICATE = " already exists";
+ private static final String APP_ID = "org.onosproject.k8snetwork";
+
+ private static final String PENDING = "Pending";
+ private static final String RUNNING = "Running";
+ private static final String SUCCEEDED = "Succeeded";
+ private static final String FAILED = "Failed";
+ private static final String UNKNOWN = "Unknown";
+ private static final String COMPLETED = "Completed";
+ private static final String CRASH_LOOP_BACK_OFF = "CrashLoopBackOff";
+
+ private static final KryoNamespace
+ SERIALIZER_K8S_POD = KryoNamespace.newBuilder()
+ .register(KryoNamespaces.API)
+ .register(Pod.class)
+ .register(ObjectMeta.class)
+ .register(PodSpec.class)
+ .register(PodStatus.class)
+ .register(PodCondition.class)
+ .register(PodIP.class)
+ .register(Container.class)
+ .register(EnvVar.class)
+ .register(EnvVarSource.class)
+ .register(EnvFromSource.class)
+ .register(ConfigMapEnvSource.class)
+ .register(ConfigMapKeySelector.class)
+ .register(ObjectFieldSelector.class)
+ .register(ResourceFieldSelector.class)
+ .register(SecretKeySelector.class)
+ .register(Lifecycle.class)
+ .register(SecretEnvSource.class)
+ .register(ContainerStatus.class)
+ .register(ContainerState.class)
+ .register(ContainerStateRunning.class)
+ .register(ContainerStateTerminated.class)
+ .register(ContainerStateWaiting.class)
+ .register(OwnerReference.class)
+ .register(Probe.class)
+ .register(ExecAction.class)
+ .register(HTTPGetAction.class)
+ .register(HTTPHeader.class)
+ .register(TCPSocketAction.class)
+ .register(ContainerPort.class)
+ .register(ResourceRequirements.class)
+ .register(SecurityContext.class)
+ .register(PodSecurityContext.class)
+ .register(SELinuxOptions.class)
+ .register(Volume.class)
+ .register(VolumeDevice.class)
+ .register(VolumeMount.class)
+ .register(IntOrString.class)
+ .register(Toleration.class)
+ .register(PersistentVolumeClaimVolumeSource.class)
+ .register(SecretVolumeSource.class)
+ .register(EmptyDirVolumeSource.class)
+ .register(Quantity.class)
+ .register(Capabilities.class)
+ .register(ConfigMapVolumeSource.class)
+ .register(KeyToPath.class)
+ .register(HostPathVolumeSource.class)
+ .register(Affinity.class)
+ .register(NodeAffinity.class)
+ .register(NodeSelector.class)
+ .register(NodeSelectorTerm.class)
+ .register(NodeSelectorRequirement.class)
+ .register(PreferredSchedulingTerm.class)
+ .register(PodAffinity.class)
+ .register(WeightedPodAffinityTerm.class)
+ .register(PodAffinityTerm.class)
+ .register(LabelSelector.class)
+ .register(LabelSelectorRequirement.class)
+ .register(PodAntiAffinity.class)
+ .register(ManagedFieldsEntry.class)
+ .register(FieldsV1.class)
+ .register(LinkedHashMap.class)
+ .register(Collection.class)
+ .build();
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected StorageService storageService;
+
+ private final ExecutorService eventExecutor = newSingleThreadExecutor(
+ groupedThreads(this.getClass().getSimpleName(), "event-handler", log));
+
+ private final MapEventListener<String, Pod> podMapListener = new KubevirtPodMapListener();
+
+ private ConsistentMap<String, Pod> podStore;
+
+ @Activate
+ protected void activate() {
+ ApplicationId appId = coreService.registerApplication(APP_ID);
+ podStore = storageService.<String, Pod>consistentMapBuilder()
+ .withSerializer(Serializer.using(SERIALIZER_K8S_POD))
+ .withName("kubevirt-pod-store")
+ .withApplicationId(appId)
+ .build();
+
+ podStore.addListener(podMapListener);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ podStore.removeListener(podMapListener);
+ eventExecutor.shutdown();
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createPod(Pod pod) {
+ podStore.compute(pod.getMetadata().getUid(), (uid, existing) -> {
+ final String error = pod.getMetadata().getUid() + ERR_DUPLICATE;
+ checkArgument(existing == null, error);
+ return pod;
+ });
+ }
+
+ @Override
+ public void updatePod(Pod pod) {
+ podStore.compute(pod.getMetadata().getUid(), (uid, existing) -> {
+ final String error = pod.getMetadata().getUid() + ERR_NOT_FOUND;
+ checkArgument(existing != null, error);
+ return pod;
+ });
+ }
+
+ @Override
+ public Pod removePod(String uid) {
+ Versioned<Pod> pod = podStore.remove(uid);
+ if (pod == null) {
+ final String error = uid + ERR_NOT_FOUND;
+ throw new IllegalArgumentException(error);
+ }
+ return pod.value();
+ }
+
+ @Override
+ public Pod pod(String uid) {
+ return podStore.asJavaMap().get(uid);
+ }
+
+ @Override
+ public Set<Pod> pods() {
+ return ImmutableSet.copyOf(podStore.asJavaMap().values());
+ }
+
+ @Override
+ public void clear() {
+ podStore.clear();
+ }
+
+ private class KubevirtPodMapListener implements MapEventListener<String, Pod> {
+
+ @Override
+ public void event(MapEvent<String, Pod> event) {
+
+ switch (event.type()) {
+ case INSERT:
+ log.debug("Kubernetes pod created {}", event.newValue());
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubevirtPodEvent(
+ KUBEVIRT_POD_CREATED, event.newValue().value())));
+ break;
+ case UPDATE:
+ log.debug("Kubernetes pod updated {}", event.newValue());
+ eventExecutor.execute(() -> processUpdate(event));
+ break;
+ case REMOVE:
+ log.debug("Kubernetes pod removed {}", event.oldValue());
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubevirtPodEvent(
+ KUBEVIRT_POD_REMOVED, event.oldValue().value())));
+ break;
+ default:
+ // do nothing
+ break;
+ }
+ }
+
+ private void processUpdate(MapEvent<String, Pod> event) {
+ notifyDelegate(new KubevirtPodEvent(
+ KUBEVIRT_POD_UPDATED, event.newValue().value()));
+
+ String oldPhase = event.oldValue().value().getStatus().getPhase();
+ String newPhase = event.newValue().value().getStatus().getPhase();
+
+ if (!PENDING.equals(oldPhase) && PENDING.equals(newPhase)) {
+ notifyDelegate(new KubevirtPodEvent(
+ KUBEVIRT_POD_PENDING, event.newValue().value()));
+ }
+
+ if (!RUNNING.equals(oldPhase) && RUNNING.equals(newPhase)) {
+ notifyDelegate(new KubevirtPodEvent(
+ KUBEVIRT_POD_RUNNING, event.newValue().value()));
+ }
+
+ if (!SUCCEEDED.equals(oldPhase) && SUCCEEDED.equals(newPhase)) {
+ notifyDelegate(new KubevirtPodEvent(
+ KUBEVIRT_POD_SUCCEEDED, event.newValue().value()));
+ }
+
+ if (!FAILED.equals(oldPhase) && FAILED.equals(newPhase)) {
+ notifyDelegate(new KubevirtPodEvent(
+ KUBEVIRT_POD_FAILED, event.newValue().value()));
+ }
+
+ if (!UNKNOWN.equals(oldPhase) && UNKNOWN.equals(newPhase)) {
+ notifyDelegate(new KubevirtPodEvent(
+ KUBEVIRT_POD_UNKNOWN, event.newValue().value()));
+ }
+
+ if (!COMPLETED.equals(oldPhase) && COMPLETED.equals(newPhase)) {
+ notifyDelegate(new KubevirtPodEvent(
+ KUBEVIRT_POD_COMPLETED, event.newValue().value()));
+ }
+
+ if (!CRASH_LOOP_BACK_OFF.equals(oldPhase) && CRASH_LOOP_BACK_OFF.equals(newPhase)) {
+ notifyDelegate(new KubevirtPodEvent(
+ KUBEVIRT_POD_CRASH_LOOP_BACK_OFF, event.newValue().value()));
+ }
+
+ Map<String, String> oldAnnot = event.oldValue().value().getMetadata().getAnnotations();
+ Map<String, String> newAnnot = event.newValue().value().getMetadata().getAnnotations();
+
+ if (oldAnnot == null && newAnnot != null) {
+ notifyDelegate(new KubevirtPodEvent(
+ KUBEVIRT_POD_ANNOTATION_ADDED, event.newValue().value()));
+ }
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodManager.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodManager.java
new file mode 100644
index 0000000..3bb2241
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodManager.java
@@ -0,0 +1,162 @@
+/*
+ * 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.impl;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
+import io.fabric8.kubernetes.api.model.Pod;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.event.ListenerRegistry;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodAdminService;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodEvent;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodListener;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodService;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodStore;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodStoreDelegate;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provides implementation of administering and interfacing kubernetes pod.
+ */
+@Component(
+ immediate = true,
+ service = {KubevirtPodAdminService.class, KubevirtPodService.class}
+)
+public class KubevirtPodManager
+ extends ListenerRegistry<KubevirtPodEvent, KubevirtPodListener>
+ implements KubevirtPodAdminService, KubevirtPodService {
+
+ protected final Logger log = getLogger(getClass());
+
+ private static final String MSG_POD = "Kubernetes pod %s %s";
+ private static final String MSG_CREATED = "created";
+ private static final String MSG_UPDATED = "updated";
+ private static final String MSG_REMOVED = "removed";
+
+ private static final String ERR_NULL_POD = "Kubevirt pod cannot be null";
+ private static final String ERR_NULL_POD_UID = "Kubevirt pod UID cannot be null";
+
+ private static final String ERR_IN_USE = " still in use";
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected KubevirtPodStore kubevirtPodStore;
+
+ private final KubevirtPodStoreDelegate delegate = new InternalPodStorageDelegate();
+
+ private ApplicationId appId;
+
+ @Activate
+ protected void activate() {
+ appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID);
+
+ kubevirtPodStore.setDelegate(delegate);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ kubevirtPodStore.unsetDelegate(delegate);
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createPod(Pod pod) {
+ checkNotNull(pod, ERR_NULL_POD);
+ checkArgument(!Strings.isNullOrEmpty(pod.getMetadata().getUid()),
+ ERR_NULL_POD_UID);
+
+ kubevirtPodStore.createPod(pod);
+
+ log.info(String.format(MSG_POD, pod.getMetadata().getName(), MSG_CREATED));
+ }
+
+ @Override
+ public void updatePod(Pod pod) {
+ checkNotNull(pod, ERR_NULL_POD);
+ checkArgument(!Strings.isNullOrEmpty(pod.getMetadata().getUid()),
+ ERR_NULL_POD_UID);
+
+ kubevirtPodStore.updatePod(pod);
+
+ log.info(String.format(MSG_POD, pod.getMetadata().getName(), MSG_UPDATED));
+ }
+
+ @Override
+ public void removePod(String uid) {
+ checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_POD_UID);
+
+ synchronized (this) {
+ if (isPodInUse(uid)) {
+ final String error = String.format(MSG_POD, uid, ERR_IN_USE);
+ throw new IllegalStateException(error);
+ }
+
+ Pod pod = kubevirtPodStore.removePod(uid);
+
+ if (pod != null) {
+ log.info(String.format(MSG_POD,
+ pod.getMetadata().getName(), MSG_REMOVED));
+ }
+ }
+ }
+
+ @Override
+ public void clear() {
+ kubevirtPodStore.clear();
+ }
+
+ @Override
+ public Pod pod(String uid) {
+ checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_POD_UID);
+ return kubevirtPodStore.pod(uid);
+ }
+
+ @Override
+ public Set<Pod> pods() {
+ return ImmutableSet.copyOf(kubevirtPodStore.pods());
+ }
+
+ private boolean isPodInUse(String uid) {
+ return false;
+ }
+
+ private class InternalPodStorageDelegate implements KubevirtPodStoreDelegate {
+
+ @Override
+ public void notify(KubevirtPodEvent event) {
+ if (event != null) {
+ log.trace("send kubernetes pod event {}", event);
+ process(event);
+ }
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodWatcher.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodWatcher.java
new file mode 100644
index 0000000..c792bef
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodWatcher.java
@@ -0,0 +1,204 @@
+/*
+ * 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.impl;
+
+import io.fabric8.kubernetes.api.model.Pod;
+import io.fabric8.kubernetes.client.KubernetesClient;
+import io.fabric8.kubernetes.client.Watcher;
+import io.fabric8.kubernetes.client.WatcherException;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.LeadershipService;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodAdminService;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigListener;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigService;
+import org.onosproject.mastership.MastershipService;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import java.util.Objects;
+import java.util.concurrent.ExecutorService;
+
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.k8sClient;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Kubernetes pod watcher used for feeding pod information.
+ */
+@Component(immediate = true)
+public class KubevirtPodWatcher {
+
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected MastershipService mastershipService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected ClusterService clusterService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected LeadershipService leadershipService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected KubevirtPodAdminService kubevirtPodAdminService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected KubevirtApiConfigService kubevirtApiConfigService;
+
+ private final ExecutorService eventExecutor = newSingleThreadExecutor(
+ groupedThreads(this.getClass().getSimpleName(), "event-handler"));
+ private final Watcher<Pod> internalKubevirtPodWatcher = new InternalKubevirtPodWatcher();
+ private final InternalKubevirtApiConfigListener
+ internalKubevirtApiConfigListener = new InternalKubevirtApiConfigListener();
+
+ private ApplicationId appId;
+ private NodeId localNodeId;
+
+ @Activate
+ protected void activate() {
+ appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID);
+ localNodeId = clusterService.getLocalNode().id();
+ leadershipService.runForLeadership(appId.name());
+ kubevirtApiConfigService.addListener(internalKubevirtApiConfigListener);
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ kubevirtApiConfigService.removeListener(internalKubevirtApiConfigListener);
+ leadershipService.withdraw(appId.name());
+ eventExecutor.shutdown();
+
+ log.info("Stopped");
+ }
+
+ private class InternalKubevirtApiConfigListener implements KubevirtApiConfigListener {
+
+ private boolean isRelevantHelper() {
+ return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
+ }
+
+ @Override
+ public void event(KubevirtApiConfigEvent event) {
+
+ switch (event.type()) {
+ case KUBEVIRT_API_CONFIG_UPDATED:
+ eventExecutor.execute(this::processConfigUpdating);
+ break;
+ case KUBEVIRT_API_CONFIG_CREATED:
+ case KUBEVIRT_API_CONFIG_REMOVED:
+ default:
+ // do nothing
+ break;
+ }
+ }
+
+ private void processConfigUpdating() {
+ if (!isRelevantHelper()) {
+ return;
+ }
+
+ KubernetesClient client = k8sClient(kubevirtApiConfigService);
+
+ if (client != null) {
+ client.pods().inAnyNamespace().watch(internalKubevirtPodWatcher);
+ }
+ }
+ }
+
+ private class InternalKubevirtPodWatcher implements Watcher<Pod> {
+
+ @Override
+ public void eventReceived(Action action, Pod pod) {
+ switch (action) {
+ case ADDED:
+ eventExecutor.execute(() -> processAddition(pod));
+ break;
+ case MODIFIED:
+ eventExecutor.execute(() -> processModification(pod));
+ break;
+ case DELETED:
+ eventExecutor.execute(() -> processDeletion(pod));
+ break;
+ case ERROR:
+ log.warn("Failures processing pod manipulation.");
+ break;
+ default:
+ break;
+ }
+ }
+
+ @Override
+ public void onClose(WatcherException e) {
+ log.warn("Pod watcher OnClose", e);
+ }
+
+ private void processAddition(Pod pod) {
+ if (!isMaster()) {
+ return;
+ }
+
+ log.trace("Process pod {} creating event from API server.",
+ pod.getMetadata().getName());
+
+ if (kubevirtPodAdminService.pod(pod.getMetadata().getUid()) == null) {
+ kubevirtPodAdminService.createPod(pod);
+ }
+ }
+
+ private void processModification(Pod pod) {
+ if (!isMaster()) {
+ return;
+ }
+
+ log.trace("Process pod {} updating event from API server.",
+ pod.getMetadata().getName());
+
+ if (kubevirtPodAdminService.pod(pod.getMetadata().getUid()) != null) {
+ kubevirtPodAdminService.updatePod(pod);
+ }
+ }
+
+ private void processDeletion(Pod pod) {
+ if (!isMaster()) {
+ return;
+ }
+
+ log.trace("Process pod {} removal event from API server.",
+ pod.getMetadata().getName());
+
+ kubevirtPodAdminService.removePod(pod.getMetadata().getUid());
+ }
+
+ private boolean isMaster() {
+ return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodManagerTest.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodManagerTest.java
new file mode 100644
index 0000000..3ca7d03
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtPodManagerTest.java
@@ -0,0 +1,223 @@
+/*
+ * 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.impl;
+
+import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.MoreExecutors;
+import io.fabric8.kubernetes.api.model.ObjectMeta;
+import io.fabric8.kubernetes.api.model.Pod;
+import io.fabric8.kubernetes.api.model.PodStatus;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.event.Event;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodEvent;
+import org.onosproject.kubevirtnetworking.api.KubevirtPodListener;
+import org.onosproject.store.service.TestStorageService;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_CREATED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_REMOVED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_UPDATED;
+
+/**
+ * Unit tests for kubevirt pod manager.
+ */
+public class KubevirtPodManagerTest {
+
+ private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
+
+ private static final String UNKNOWN_UID = "unknown_uid";
+ private static final String UPDATED_UID = "updated_uid";
+ private static final String UPDATED_NAME = "updated_name";
+
+ private static final String POD_UID = "pod_uid";
+ private static final String POD_NAME = "pod_name";
+
+ private static final Pod POD = createKubevirtPod(POD_UID, POD_NAME);
+ private static final Pod POD_UPDATED = createKubevirtPod(POD_UID, UPDATED_NAME);
+
+ private final TestKubevirtPodListener testListener = new TestKubevirtPodListener();
+
+ private KubevirtPodManager target;
+ private DistributedKubevirtPodStore kubevirtPodStore;
+
+ @Before
+ public void setUp() throws Exception {
+ kubevirtPodStore = new DistributedKubevirtPodStore();
+ TestUtils.setField(kubevirtPodStore, "coreService", new TestCoreService());
+ TestUtils.setField(kubevirtPodStore, "storageService", new TestStorageService());
+ TestUtils.setField(kubevirtPodStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
+ kubevirtPodStore.activate();
+
+ target = new KubevirtPodManager();
+ TestUtils.setField(target, "coreService", new TestCoreService());
+ target.kubevirtPodStore = kubevirtPodStore;
+ target.addListener(testListener);
+ target.activate();
+ }
+
+ @After
+ public void tearDown() {
+ target.removeListener(testListener);
+ kubevirtPodStore.deactivate();
+ target.deactivate();
+ kubevirtPodStore = null;
+ target = null;
+ }
+
+ /**
+ * Tests if getting all pods return correct set of pods.
+ */
+ @Test
+ public void testGetPods() {
+ createBasicPods();
+ assertEquals("Number of pods did not match", 1, target.pods().size());
+ }
+
+ /**
+ * Tests if getting a pod with UID returns the correct pod.
+ */
+ @Test
+ public void testGetPodByUid() {
+ createBasicPods();
+ assertNotNull("Pod did not match", target.pod(POD_UID));
+ assertNull("Pod did not match", target.pod(UNKNOWN_UID));
+ }
+
+ /**
+ * Tests creating and removing a pod, and checks if it triggers proper events.
+ */
+ @Test
+ public void testCreateAndRemovePod() {
+ target.createPod(POD);
+ assertEquals("Number of pods did not match", 1, target.pods().size());
+ assertNotNull("Pod was not created", target.pod(POD_UID));
+
+ target.removePod(POD_UID);
+ assertEquals("Number of pods did not match", 0, target.pods().size());
+ assertNull("Pod was not removed", target.pod(POD_UID));
+
+ validateEvents(KUBEVIRT_POD_CREATED, KUBEVIRT_POD_REMOVED);
+ }
+
+ /**
+ * Tests updating a pod, and checks if it triggers proper events.
+ */
+ @Test
+ public void testCreateAndUpdatePod() {
+ target.createPod(POD);
+ assertEquals("Number of pods did not match", 1, target.pods().size());
+ assertEquals("Pod did not match", POD_NAME,
+ target.pod(POD_UID).getMetadata().getName());
+
+ target.updatePod(POD_UPDATED);
+
+ assertEquals("Number of pods did not match", 1, target.pods().size());
+ assertEquals("Pod did not match", UPDATED_NAME,
+ target.pod(POD_UID).getMetadata().getName());
+ validateEvents(KUBEVIRT_POD_CREATED, KUBEVIRT_POD_UPDATED);
+ }
+
+ /**
+ * Tests if creating a null pod fails with an exception.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testCreateNullPod() {
+ target.createPod(null);
+ }
+
+ /**
+ * Tests if creating a duplicate pod fails with an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testCreateDuplicatePod() {
+ target.createPod(POD);
+ target.createPod(POD);
+ }
+
+ /**
+ * Tests if removing pod with null ID fails with an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testRemovePodWithNull() {
+ target.removePod(null);
+ }
+
+ /**
+ * Tests if updating an unregistered pod fails with an exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testUpdateUnregisteredPod() {
+ target.updatePod(POD);
+ }
+
+ private void createBasicPods() {
+ target.createPod(POD);
+ }
+
+ private static Pod createKubevirtPod(String uid, String name) {
+ ObjectMeta meta = new ObjectMeta();
+ meta.setUid(uid);
+ meta.setName(name);
+
+ PodStatus status = new PodStatus();
+ status.setPhase("Running");
+
+ Pod pod = new Pod();
+ pod.setApiVersion("v1");
+ pod.setKind("pod");
+ pod.setMetadata(meta);
+ pod.setStatus(status);
+
+ return pod;
+ }
+
+ private static class TestCoreService extends CoreServiceAdapter {
+
+ @Override
+ public ApplicationId registerApplication(String name) {
+ return TEST_APP_ID;
+ }
+ }
+
+ private static class TestKubevirtPodListener implements KubevirtPodListener {
+ private List<KubevirtPodEvent> events = Lists.newArrayList();
+
+ @Override
+ public void event(KubevirtPodEvent event) {
+ events.add(event);
+ }
+ }
+
+ private void validateEvents(Enum... types) {
+ int i = 0;
+ assertEquals("Number of events did not match", types.length, testListener.events.size());
+ for (Event event : testListener.events) {
+ assertEquals("Incorrect event received", types[i], event.type());
+ i++;
+ }
+ testListener.events.clear();
+ }
+}