[ONOS-7934] Add k8s endpoints store and manager with unit tests

Change-Id: I4e67f5fd7d9859339b92f1816f52a092063dc2e4
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/DistributedK8sEndpointsStore.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/DistributedK8sEndpointsStore.java
new file mode 100644
index 0000000..6327451
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/DistributedK8sEndpointsStore.java
@@ -0,0 +1,189 @@
+/*
+ * Copyright 2019-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.k8snetworking.impl;
+
+import com.google.common.collect.ImmutableSet;
+import io.fabric8.kubernetes.api.model.EndpointAddress;
+import io.fabric8.kubernetes.api.model.EndpointPort;
+import io.fabric8.kubernetes.api.model.EndpointSubset;
+import io.fabric8.kubernetes.api.model.Endpoints;
+import io.fabric8.kubernetes.api.model.ObjectMeta;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.k8snetworking.api.K8sEndpointsEvent;
+import org.onosproject.k8snetworking.api.K8sEndpointsStore;
+import org.onosproject.k8snetworking.api.K8sEndpointsStoreDelegate;
+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.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.k8snetworking.api.K8sEndpointsEvent.Type.K8S_ENDPOINTS_CREATED;
+import static org.onosproject.k8snetworking.api.K8sEndpointsEvent.Type.K8S_ENDPOINTS_REMOVED;
+import static org.onosproject.k8snetworking.api.K8sEndpointsEvent.Type.K8S_ENDPOINTS_UPDATED;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of kubernetes service store using consistent map.
+ */
+@Component(immediate = true, service = K8sEndpointsStore.class)
+public class DistributedK8sEndpointsStore
+        extends AbstractStore<K8sEndpointsEvent, K8sEndpointsStoreDelegate>
+        implements K8sEndpointsStore {
+
+    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 KryoNamespace
+            SERIALIZER_K8S_ENDPOINTS = KryoNamespace.newBuilder()
+            .register(KryoNamespaces.API)
+            .register(Endpoints.class)
+            .register(ObjectMeta.class)
+            .register(EndpointSubset.class)
+            .register(EndpointAddress.class)
+            .register(EndpointPort.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, Endpoints> endpointsMapListener =
+            new K8sEndpointsMapListener();
+
+    private ConsistentMap<String, Endpoints> endpointsStore;
+
+    @Activate
+    protected void activate() {
+        ApplicationId appId = coreService.registerApplication(APP_ID);
+        endpointsStore = storageService.<String, Endpoints>consistentMapBuilder()
+                .withSerializer(Serializer.using(SERIALIZER_K8S_ENDPOINTS))
+                .withName("k8s-endpoints-store")
+                .withApplicationId(appId)
+                .build();
+
+        endpointsStore.addListener(endpointsMapListener);
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        endpointsStore.removeListener(endpointsMapListener);
+        eventExecutor.shutdown();
+        log.info("Stopped");
+    }
+
+    @Override
+    public void createEndpoints(Endpoints endpoints) {
+        endpointsStore.compute(endpoints.getMetadata().getUid(), (uid, existing) -> {
+            final String error = endpoints.getMetadata().getUid() + ERR_DUPLICATE;
+            checkArgument(existing == null, error);
+            return endpoints;
+        });
+    }
+
+    @Override
+    public void updateEndpoints(Endpoints endpoints) {
+        endpointsStore.compute(endpoints.getMetadata().getUid(), (uid, existing) -> {
+            final String error = endpoints.getMetadata().getUid() + ERR_NOT_FOUND;
+            checkArgument(existing != null, error);
+            return endpoints;
+        });
+    }
+
+    @Override
+    public Endpoints removeEndpoints(String uid) {
+        Versioned<Endpoints> endpoints = endpointsStore.remove(uid);
+        if (endpoints == null) {
+            final String error = uid + ERR_NOT_FOUND;
+            throw new IllegalArgumentException(error);
+        }
+        return endpoints.value();
+    }
+
+    @Override
+    public Endpoints endpoints(String uid) {
+        return endpointsStore.asJavaMap().get(uid);
+    }
+
+    @Override
+    public Set<Endpoints> endpointses() {
+        return ImmutableSet.copyOf(endpointsStore.asJavaMap().values());
+    }
+
+    @Override
+    public void clear() {
+        endpointsStore.clear();
+    }
+
+    private class K8sEndpointsMapListener implements MapEventListener<String, Endpoints> {
+
+        @Override
+        public void event(MapEvent<String, Endpoints> event) {
+
+            switch (event.type()) {
+                case INSERT:
+                    log.debug("Kubernetes endpoints created {}", event.newValue());
+                    eventExecutor.execute(() ->
+                            notifyDelegate(new K8sEndpointsEvent(
+                                    K8S_ENDPOINTS_CREATED, event.newValue().value())));
+                    break;
+                case UPDATE:
+                    log.debug("Kubernetes endpoints updated {}", event.newValue());
+                    eventExecutor.execute(() ->
+                            notifyDelegate(new K8sEndpointsEvent(
+                                    K8S_ENDPOINTS_UPDATED, event.newValue().value())));
+                    break;
+                case REMOVE:
+                    log.debug("Kubernetes endpoints removed {}", event.oldValue());
+                    eventExecutor.execute(() ->
+                            notifyDelegate(new K8sEndpointsEvent(
+                                    K8S_ENDPOINTS_REMOVED, event.oldValue().value())));
+                    break;
+                default:
+                    // do nothing
+                    break;
+            }
+        }
+    }
+}
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sEndpointsManager.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sEndpointsManager.java
new file mode 100644
index 0000000..de594aa
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sEndpointsManager.java
@@ -0,0 +1,163 @@
+/*
+ * Copyright 2019-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.k8snetworking.impl;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
+import io.fabric8.kubernetes.api.model.Endpoints;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.event.ListenerRegistry;
+import org.onosproject.k8snetworking.api.K8sEndpointsAdminService;
+import org.onosproject.k8snetworking.api.K8sEndpointsEvent;
+import org.onosproject.k8snetworking.api.K8sEndpointsListener;
+import org.onosproject.k8snetworking.api.K8sEndpointsService;
+import org.onosproject.k8snetworking.api.K8sEndpointsStore;
+import org.onosproject.k8snetworking.api.K8sEndpointsStoreDelegate;
+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.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provides implementation of administering and interfacing kubernetes endpoints.
+ */
+@Component(
+        immediate = true,
+        service = {K8sEndpointsAdminService.class, K8sEndpointsService.class}
+)
+public class K8sEndpointsManager
+        extends ListenerRegistry<K8sEndpointsEvent, K8sEndpointsListener>
+        implements K8sEndpointsAdminService, K8sEndpointsService {
+
+    protected final Logger log = getLogger(getClass());
+
+    private static final String MSG_ENDPOINTS  = "Kubernetes endpoints %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_ENDPOINTS  = "Kubernetes endpoints cannot be null";
+    private static final String ERR_NULL_ENDPOINTS_UID  = "Kubernetes endpoints 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 K8sEndpointsStore k8sEndpointsStore;
+
+    private final K8sEndpointsStoreDelegate
+            delegate = new InternalEndpointsStorageDelegate();
+
+    private ApplicationId appId;
+
+    @Activate
+    protected void activate() {
+        appId = coreService.registerApplication(K8S_NETWORKING_APP_ID);
+
+        k8sEndpointsStore.setDelegate(delegate);
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        k8sEndpointsStore.unsetDelegate(delegate);
+        log.info("Stopped");
+    }
+
+    @Override
+    public void createEndpoints(Endpoints endpoints) {
+        checkNotNull(endpoints, ERR_NULL_ENDPOINTS);
+        checkArgument(!Strings.isNullOrEmpty(endpoints.getMetadata().getUid()),
+                ERR_NULL_ENDPOINTS_UID);
+
+        k8sEndpointsStore.createEndpoints(endpoints);
+
+        log.info(String.format(MSG_ENDPOINTS, endpoints.getMetadata().getName(), MSG_CREATED));
+    }
+
+    @Override
+    public void updateEndpoints(Endpoints endpoints) {
+        checkNotNull(endpoints, ERR_NULL_ENDPOINTS);
+        checkArgument(!Strings.isNullOrEmpty(endpoints.getMetadata().getUid()),
+                ERR_NULL_ENDPOINTS_UID);
+
+        k8sEndpointsStore.updateEndpoints(endpoints);
+
+        log.info(String.format(MSG_ENDPOINTS, endpoints.getMetadata().getName(), MSG_UPDATED));
+    }
+
+    @Override
+    public void removeEndpoints(String uid) {
+        checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_ENDPOINTS_UID);
+
+        synchronized (this) {
+            if (isEndpointsInUse(uid)) {
+                final String error = String.format(MSG_ENDPOINTS, uid, ERR_IN_USE);
+                throw new IllegalStateException(error);
+            }
+
+            Endpoints endpoints = k8sEndpointsStore.removeEndpoints(uid);
+
+            if (endpoints != null) {
+                log.info(String.format(MSG_ENDPOINTS,
+                        endpoints.getMetadata().getName(), MSG_REMOVED));
+            }
+        }
+    }
+
+    @Override
+    public void clear() {
+        k8sEndpointsStore.clear();
+    }
+
+    @Override
+    public Endpoints endpoints(String uid) {
+        checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_ENDPOINTS_UID);
+        return k8sEndpointsStore.endpoints(uid);
+    }
+
+    @Override
+    public Set<Endpoints> endpointses() {
+        return ImmutableSet.copyOf(k8sEndpointsStore.endpointses());
+    }
+
+    private boolean isEndpointsInUse(String uid) {
+        return false;
+    }
+
+    private class InternalEndpointsStorageDelegate implements K8sEndpointsStoreDelegate {
+
+        @Override
+        public void notify(K8sEndpointsEvent event) {
+            if (event != null) {
+                log.trace("send kubernetes endpoints event {}", event);
+                process(event);
+            }
+        }
+    }
+}
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/impl/K8sEndpointsManagerTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/impl/K8sEndpointsManagerTest.java
new file mode 100644
index 0000000..06a1411
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/impl/K8sEndpointsManagerTest.java
@@ -0,0 +1,219 @@
+/*
+ * Copyright 2019-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.k8snetworking.impl;
+
+import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.MoreExecutors;
+import io.fabric8.kubernetes.api.model.Endpoints;
+import io.fabric8.kubernetes.api.model.ObjectMeta;
+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.k8snetworking.api.K8sEndpointsEvent;
+import org.onosproject.k8snetworking.api.K8sEndpointsListener;
+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.k8snetworking.api.K8sEndpointsEvent.Type.K8S_ENDPOINTS_CREATED;
+import static org.onosproject.k8snetworking.api.K8sEndpointsEvent.Type.K8S_ENDPOINTS_REMOVED;
+import static org.onosproject.k8snetworking.api.K8sEndpointsEvent.Type.K8S_ENDPOINTS_UPDATED;
+
+/**
+ * Unit tests for kubernetes endpoints manager.
+ */
+public class K8sEndpointsManagerTest {
+
+    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 ENDPOINTS_UID = "endpoints_uid";
+    private static final String ENDPOINTS_NAME = "endpoints_name";
+
+    private static final Endpoints ENDPOINTS = createK8sEndpoints(ENDPOINTS_UID, ENDPOINTS_NAME);
+    private static final Endpoints ENDPOINTS_UPDATED =
+            createK8sEndpoints(ENDPOINTS_UID, UPDATED_NAME);
+
+    private final TestK8sEndpointsListener testListener = new TestK8sEndpointsListener();
+
+    private K8sEndpointsManager target;
+    private DistributedK8sEndpointsStore k8sEndpointsStore;
+
+    @Before
+    public void setUp() throws Exception {
+        k8sEndpointsStore = new DistributedK8sEndpointsStore();
+        TestUtils.setField(k8sEndpointsStore, "coreService", new TestCoreService());
+        TestUtils.setField(k8sEndpointsStore, "storageService", new TestStorageService());
+        TestUtils.setField(k8sEndpointsStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
+        k8sEndpointsStore.activate();
+
+        target = new K8sEndpointsManager();
+        TestUtils.setField(target, "coreService", new TestCoreService());
+        target.k8sEndpointsStore = k8sEndpointsStore;
+        target.addListener(testListener);
+        target.activate();
+    }
+
+    @After
+    public void tearDown() {
+        target.removeListener(testListener);
+        k8sEndpointsStore.deactivate();
+        target.deactivate();
+        k8sEndpointsStore = null;
+        target = null;
+    }
+
+    /**
+     * Tests if getting all endpoints return correct set of endpoints.
+     */
+    @Test
+    public void testGetEndpoints() {
+        createBasicEndpoints();
+        assertEquals("Number of endpoints did not match", 1, target.endpointses().size());
+    }
+
+    /**
+     * Tests if getting a endpoints with UID returns the correct endpoints.
+     */
+    @Test
+    public void testGetEndpointsByUid() {
+        createBasicEndpoints();
+        assertNotNull("Endpoints did not match", target.endpoints(ENDPOINTS_UID));
+        assertNull("Endpoints did not match", target.endpoints(UNKNOWN_UID));
+    }
+
+    /**
+     * Tests creating and removing a endpoints, and checks if it triggers proper events.
+     */
+    @Test
+    public void testCreateAndRemoveEndpoints() {
+        target.createEndpoints(ENDPOINTS);
+        assertEquals("Number of endpoints did not match", 1, target.endpointses().size());
+        assertNotNull("Endpoint was not created", target.endpoints(ENDPOINTS_UID));
+
+        target.removeEndpoints(ENDPOINTS_UID);
+        assertEquals("Number of endpoints did not match", 0, target.endpointses().size());
+        assertNull("Endpoint was not removed", target.endpoints(ENDPOINTS_UID));
+
+        validateEvents(K8S_ENDPOINTS_CREATED, K8S_ENDPOINTS_REMOVED);
+    }
+
+    /**
+     * Tests updating a endpoints, and checks if it triggers proper events.
+     */
+    @Test
+    public void testCreateAndUpdateEndpoints() {
+        target.createEndpoints(ENDPOINTS);
+        assertEquals("Number of endpoints did not match", 1, target.endpointses().size());
+        assertEquals("Endpoint did not match", ENDPOINTS_NAME,
+                target.endpoints(ENDPOINTS_UID).getMetadata().getName());
+
+        target.updateEndpoints(ENDPOINTS_UPDATED);
+
+        assertEquals("Number of endpoints did not match", 1, target.endpointses().size());
+        assertEquals("Endpoints did not match", UPDATED_NAME,
+                target.endpoints(ENDPOINTS_UID).getMetadata().getName());
+        validateEvents(K8S_ENDPOINTS_CREATED, K8S_ENDPOINTS_UPDATED);
+    }
+
+    /**
+     * Tests if creating a null endpoints fails with an exception.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testCreateNullEndpoints() {
+        target.createEndpoints(null);
+    }
+
+    /**
+     * Tests if creating a duplicate endpoints fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateDuplicateEndpoints() {
+        target.createEndpoints(ENDPOINTS);
+        target.createEndpoints(ENDPOINTS);
+    }
+
+    /**
+     * Tests if removing endpoints with null ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testRemoveEndpointsWithNull() {
+        target.removeEndpoints(null);
+    }
+
+    /**
+     * Tests if updating an unregistered endpoints fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateUnregisteredEndpoints() {
+        target.updateEndpoints(ENDPOINTS);
+    }
+
+    private void createBasicEndpoints() {
+        target.createEndpoints(ENDPOINTS);
+    }
+
+    private static Endpoints createK8sEndpoints(String uid, String name) {
+        ObjectMeta meta = new ObjectMeta();
+        meta.setUid(uid);
+        meta.setName(name);
+
+        Endpoints endpoints = new Endpoints();
+        endpoints.setApiVersion("v1");
+        endpoints.setKind("endpoints");
+        endpoints.setMetadata(meta);
+
+        return endpoints;
+    }
+
+    private static class TestCoreService extends CoreServiceAdapter {
+
+        @Override
+        public ApplicationId registerApplication(String name) {
+            return TEST_APP_ID;
+        }
+    }
+
+    private static class TestK8sEndpointsListener implements K8sEndpointsListener {
+        private List<K8sEndpointsEvent> events = Lists.newArrayList();
+
+        @Override
+        public void event(K8sEndpointsEvent 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();
+    }
+}