[ONOS-8014] Implement kubernetes namespace store and manager

Change-Id: I86200d9a49a7935738e6599829d297196ab50131
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNamespaceListCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNamespaceListCommand.java
new file mode 100644
index 0000000..4d69082
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sNamespaceListCommand.java
@@ -0,0 +1,83 @@
+/*
+ * 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.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.Lists;
+import io.fabric8.kubernetes.api.model.Namespace;
+import io.fabric8.kubernetes.client.utils.Serialization;
+import org.apache.karaf.shell.api.action.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.k8snetworking.api.K8sNamespaceService;
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.List;
+
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
+
+/**
+ * Lists kubernetes namespaces.
+ */
+@Command(scope = "onos", name = "k8s-namespaces",
+        description = "Lists all kubernetes namespaces")
+public class K8sNamespaceListCommand extends AbstractShellCommand {
+
+    private static final String FORMAT = "%-50s%-15s%-30s";
+
+    @Override
+    protected void doExecute() {
+        K8sNamespaceService service = get(K8sNamespaceService.class);
+        List<Namespace> namespaces = Lists.newArrayList(service.namespaces());
+        namespaces.sort(Comparator.comparing(n -> n.getMetadata().getName()));
+
+        if (outputJson()) {
+            print("%s", json(namespaces));
+        } else {
+            print(FORMAT, "Name", "Phase", "Labels");
+
+            for (Namespace namespace : namespaces) {
+
+                print(FORMAT,
+                        namespace.getMetadata().getName(),
+                        namespace.getStatus().getPhase(),
+                        namespace.getMetadata() != null &&
+                                namespace.getMetadata().getLabels() != null &&
+                                !namespace.getMetadata().getLabels().isEmpty() ?
+                                namespace.getMetadata().getLabels() : "");
+            }
+        }
+    }
+
+    private String json(List<Namespace> namespaces) {
+        ObjectMapper mapper = new ObjectMapper();
+        ArrayNode result = mapper.createArrayNode();
+
+        try {
+            for (Namespace namespace : namespaces) {
+                ObjectNode json = (ObjectNode) new ObjectMapper()
+                        .readTree(Serialization.asJson(namespace));
+                result.add(json);
+            }
+            return prettyJson(mapper, result.toString());
+        } catch (IOException e) {
+            log.warn("Failed to parse Namespace's JSON string.");
+            return "";
+        }
+    }
+}
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sSyncStateCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sSyncStateCommand.java
index 4c7e83d..41795aa 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sSyncStateCommand.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sSyncStateCommand.java
@@ -17,6 +17,7 @@
 
 import com.google.common.collect.Lists;
 import io.fabric8.kubernetes.api.model.Endpoints;
+import io.fabric8.kubernetes.api.model.Namespace;
 import io.fabric8.kubernetes.api.model.Pod;
 import io.fabric8.kubernetes.api.model.extensions.Ingress;
 import io.fabric8.kubernetes.api.model.networking.NetworkPolicy;
@@ -29,6 +30,7 @@
 import org.onosproject.k8snetworking.api.DefaultK8sPort;
 import org.onosproject.k8snetworking.api.K8sEndpointsAdminService;
 import org.onosproject.k8snetworking.api.K8sIngressAdminService;
+import org.onosproject.k8snetworking.api.K8sNamespaceAdminService;
 import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
 import org.onosproject.k8snetworking.api.K8sNetworkPolicyAdminService;
 import org.onosproject.k8snetworking.api.K8sPodAdminService;
@@ -58,6 +60,7 @@
     private static final String ENDPOINTS_FORMAT = "%-50s%-50s%-20s";
     private static final String INGRESS_FORMAT = "%-50s%-15s%-30s";
     private static final String NETWORK_POLICY_FORMAT = "%-50s%-15s%-30s";
+    private static final String NAMESPACE_FORMAT = "%-50s%-15s%-30s";
 
     private static final String PORT_ID = "portId";
     private static final String DEVICE_ID = "deviceId";
@@ -70,6 +73,8 @@
     protected void doExecute() {
         K8sApiConfigService configService = get(K8sApiConfigService.class);
         K8sPodAdminService podAdminService = get(K8sPodAdminService.class);
+        K8sNamespaceAdminService namespaceAdminService =
+                get(K8sNamespaceAdminService.class);
         K8sServiceAdminService serviceAdminService =
                 get(K8sServiceAdminService.class);
         K8sIngressAdminService ingressAdminService =
@@ -95,6 +100,17 @@
             return;
         }
 
+        print("\nSynchronizing kubernetes namespaces");
+        print(NAMESPACE_FORMAT, "Name", "Phase", "Labels");
+        client.namespaces().list().getItems().forEach(ns -> {
+            if (namespaceAdminService.namespace(ns.getMetadata().getUid()) != null) {
+                namespaceAdminService.updateNamespace(ns);
+            } else {
+                namespaceAdminService.createNamespace(ns);
+            }
+            printNamespace(ns);
+        });
+
         print("Synchronizing kubernetes services");
         print(SERVICE_FORMAT, "Name", "Cluster IP", "Ports");
         client.services().inAnyNamespace().list().getItems().forEach(svc -> {
@@ -182,6 +198,16 @@
                 ports.isEmpty() ? "" : ports);
     }
 
+    private void printNamespace(Namespace namespace) {
+        print(NAMESPACE_FORMAT,
+                namespace.getMetadata().getName(),
+                namespace.getStatus().getPhase(),
+                namespace.getMetadata() != null &&
+                        namespace.getMetadata().getLabels() != null &&
+                        !namespace.getMetadata().getLabels().isEmpty() ?
+                        namespace.getMetadata().getLabels() : "");
+    }
+
     private void printService(io.fabric8.kubernetes.api.model.Service service) {
 
         List<Integer> ports = Lists.newArrayList();
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/DistributedK8sNamespaceStore.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/DistributedK8sNamespaceStore.java
new file mode 100644
index 0000000..a60354a
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/DistributedK8sNamespaceStore.java
@@ -0,0 +1,185 @@
+/*
+ * 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.Namespace;
+import io.fabric8.kubernetes.api.model.NamespaceSpec;
+import io.fabric8.kubernetes.api.model.NamespaceStatus;
+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.K8sNamespaceEvent;
+import org.onosproject.k8snetworking.api.K8sNamespaceStore;
+import org.onosproject.k8snetworking.api.K8sNamespaceStoreDelegate;
+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.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.K8sNamespaceEvent.Type.K8S_NAMESPACE_CREATED;
+import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_REMOVED;
+import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_UPDATED;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of kubernetes namespace store using consistent map.
+ */
+@Component(immediate = true, service = K8sNamespaceStore.class)
+public class DistributedK8sNamespaceStore
+        extends AbstractStore<K8sNamespaceEvent, K8sNamespaceStoreDelegate>
+        implements K8sNamespaceStore {
+
+    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_NAMESPACE = KryoNamespace.newBuilder()
+            .register(KryoNamespaces.API)
+            .register(Namespace.class)
+            .register(ObjectMeta.class)
+            .register(NamespaceSpec.class)
+            .register(NamespaceStatus.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, Namespace> namespaceMapListener =
+            new K8sNamespaceMapListener();
+
+    private ConsistentMap<String, Namespace> namespaceStore;
+
+    @Activate
+    protected void activate() {
+        ApplicationId appId = coreService.registerApplication(APP_ID);
+        namespaceStore = storageService.<String, Namespace>consistentMapBuilder()
+                .withSerializer(Serializer.using(SERIALIZER_K8S_NAMESPACE))
+                .withName("k8s-namespace-store")
+                .withApplicationId(appId)
+                .build();
+
+        namespaceStore.addListener(namespaceMapListener);
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        namespaceStore.removeListener(namespaceMapListener);
+        eventExecutor.shutdown();
+        log.info("Stopped");
+    }
+
+    @Override
+    public void createNamespace(Namespace namespace) {
+        namespaceStore.compute(namespace.getMetadata().getUid(), (uid, existing) -> {
+            final String error = namespace.getMetadata().getUid() + ERR_DUPLICATE;
+            checkArgument(existing == null, error);
+            return namespace;
+        });
+    }
+
+    @Override
+    public void updateNamespace(Namespace namespace) {
+        namespaceStore.compute(namespace.getMetadata().getUid(), (uid, existing) -> {
+            final String error  = namespace.getMetadata().getUid() + ERR_NOT_FOUND;
+            checkArgument(existing != null, error);
+            return namespace;
+        });
+    }
+
+    @Override
+    public Namespace removeNamespace(String uid) {
+        Versioned<Namespace> namespace = namespaceStore.remove(uid);
+        if (namespace == null) {
+            final String error = uid + ERR_NOT_FOUND;
+            throw new IllegalArgumentException(error);
+        }
+        return namespace.value();
+    }
+
+    @Override
+    public Namespace namespace(String uid) {
+        return namespaceStore.asJavaMap().get(uid);
+    }
+
+    @Override
+    public Set<Namespace> namespaces() {
+        return ImmutableSet.copyOf(namespaceStore.asJavaMap().values());
+    }
+
+    @Override
+    public void clear() {
+        namespaceStore.clear();
+    }
+
+    private class K8sNamespaceMapListener implements MapEventListener<String, Namespace> {
+
+        @Override
+        public void event(MapEvent<String, Namespace> event) {
+
+            switch (event.type()) {
+                case INSERT:
+                    log.debug("Kubernetes namespace created {}", event.newValue());
+                    eventExecutor.execute(() ->
+                            notifyDelegate(new K8sNamespaceEvent(
+                                    K8S_NAMESPACE_CREATED, event.newValue().value())));
+                    break;
+                case UPDATE:
+                    log.debug("Kubernetes namespace updated {}", event.newValue());
+                    eventExecutor.execute(() ->
+                            notifyDelegate(new K8sNamespaceEvent(
+                                    K8S_NAMESPACE_UPDATED, event.newValue().value())));
+                    break;
+                case REMOVE:
+                    log.debug("Kubernetes namespace removed {}", event.oldValue());
+                    eventExecutor.execute(() ->
+                            notifyDelegate(new K8sNamespaceEvent(
+                                    K8S_NAMESPACE_REMOVED, event.oldValue().value())));
+                    break;
+                default:
+                    // do nothing
+                    break;
+            }
+        }
+    }
+}
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNamespaceManager.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNamespaceManager.java
new file mode 100644
index 0000000..3a1e10c
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNamespaceManager.java
@@ -0,0 +1,168 @@
+/*
+ * 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.Namespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.event.ListenerRegistry;
+import org.onosproject.k8snetworking.api.K8sNamespaceAdminService;
+import org.onosproject.k8snetworking.api.K8sNamespaceEvent;
+import org.onosproject.k8snetworking.api.K8sNamespaceListener;
+import org.onosproject.k8snetworking.api.K8sNamespaceService;
+import org.onosproject.k8snetworking.api.K8sNamespaceStore;
+import org.onosproject.k8snetworking.api.K8sNamespaceStoreDelegate;
+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 namespace.
+ */
+@Component(
+        immediate = true,
+        service = {K8sNamespaceAdminService.class, K8sNamespaceService.class}
+)
+public class K8sNamespaceManager
+        extends ListenerRegistry<K8sNamespaceEvent, K8sNamespaceListener>
+        implements K8sNamespaceAdminService, K8sNamespaceService {
+
+    protected final Logger log = getLogger(getClass());
+
+    private static final String MSG_NAMESPACE  = "Kubernetes namespace %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_NAMESPACE = "Kubernetes namespace cannot be null";
+    private static final String
+            ERR_NULL_NAMESPACE_UID  = "Kubernetes namespace 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 K8sNamespaceStore k8sNamespaceStore;
+
+    private final K8sNamespaceStoreDelegate delegate =
+            new InternalNamespaceStorageDelegate();
+
+    private ApplicationId appId;
+
+    @Activate
+    protected void activate() {
+        appId = coreService.registerApplication(K8S_NETWORKING_APP_ID);
+
+        k8sNamespaceStore.setDelegate(delegate);
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        k8sNamespaceStore.unsetDelegate(delegate);
+        log.info("Stopped");
+    }
+
+    @Override
+    public void createNamespace(Namespace namespace) {
+        checkNotNull(namespace, ERR_NULL_NAMESPACE);
+        checkArgument(!Strings.isNullOrEmpty(namespace.getMetadata().getUid()),
+                ERR_NULL_NAMESPACE_UID);
+
+        k8sNamespaceStore.createNamespace(namespace);
+
+        log.info(String.format(MSG_NAMESPACE,
+                namespace.getMetadata().getName(), MSG_CREATED));
+    }
+
+    @Override
+    public void updateNamespace(Namespace namespace) {
+        checkNotNull(namespace, ERR_NULL_NAMESPACE);
+        checkArgument(!Strings.isNullOrEmpty(namespace.getMetadata().getUid()),
+                ERR_NULL_NAMESPACE_UID);
+
+        k8sNamespaceStore.updateNamespace(namespace);
+
+        log.info(String.format(MSG_NAMESPACE,
+                namespace.getMetadata().getName(), MSG_UPDATED));
+    }
+
+    @Override
+    public void removeNamespace(String uid) {
+        checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_NAMESPACE_UID);
+
+        synchronized (this) {
+            if (isNamespaceInUse(uid)) {
+                final String error = String.format(MSG_NAMESPACE, uid, ERR_IN_USE);
+                throw new IllegalStateException(error);
+            }
+        }
+
+        Namespace namespace = k8sNamespaceStore.removeNamespace(uid);
+
+        if (namespace != null) {
+            log.info(String.format(MSG_NAMESPACE,
+                    namespace.getMetadata().getName(), MSG_REMOVED));
+        }
+    }
+
+    @Override
+    public void clear() {
+        k8sNamespaceStore.clear();
+    }
+
+    @Override
+    public Namespace namespace(String uid) {
+        checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_NAMESPACE_UID);
+        return k8sNamespaceStore.namespace(uid);
+    }
+
+    @Override
+    public Set<Namespace> namespaces() {
+        return ImmutableSet.copyOf(k8sNamespaceStore.namespaces());
+    }
+
+    private boolean isNamespaceInUse(String uid) {
+        return false;
+    }
+
+    private class InternalNamespaceStorageDelegate
+            implements K8sNamespaceStoreDelegate {
+
+        @Override
+        public void notify(K8sNamespaceEvent event) {
+            if (event != null) {
+                log.trace("send kubernetes namespace event {}", event);
+                process(event);
+            }
+        }
+    }
+}
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNamespaceWatcher.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNamespaceWatcher.java
new file mode 100644
index 0000000..a9ac4cb
--- /dev/null
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/impl/K8sNamespaceWatcher.java
@@ -0,0 +1,203 @@
+/*
+ * 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 io.fabric8.kubernetes.api.model.Namespace;
+import io.fabric8.kubernetes.client.KubernetesClient;
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.Watcher;
+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.k8snetworking.api.K8sNamespaceAdminService;
+import org.onosproject.k8snode.api.K8sApiConfigEvent;
+import org.onosproject.k8snode.api.K8sApiConfigListener;
+import org.onosproject.k8snode.api.K8sApiConfigService;
+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.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.k8sClient;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Kubernetes namespace watcher used for feeding namespace information.
+ */
+@Component(immediate = true)
+public class K8sNamespaceWatcher {
+
+    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 K8sNamespaceAdminService k8sNamespaceAdminService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected K8sApiConfigService k8sApiConfigService;
+
+    private ApplicationId appId;
+    private NodeId localNodeId;
+
+    private final ExecutorService eventExecutor = newSingleThreadExecutor(
+            groupedThreads(this.getClass().getSimpleName(), "event-handler"));
+
+    private final InternalK8sNamespaceWatcher
+            internalK8sNamespaceWatcher = new InternalK8sNamespaceWatcher();
+    private final InternalK8sApiConfigListener
+            internalK8sApiConfigListener = new InternalK8sApiConfigListener();
+
+    @Activate
+    protected void activate() {
+        appId = coreService.registerApplication(K8S_NETWORKING_APP_ID);
+        localNodeId = clusterService.getLocalNode().id();
+        leadershipService.runForLeadership(appId.name());
+        k8sApiConfigService.addListener(internalK8sApiConfigListener);
+
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        k8sApiConfigService.removeListener(internalK8sApiConfigListener);
+        leadershipService.withdraw(appId.name());
+        eventExecutor.shutdown();
+
+        log.info("Stopped");
+    }
+
+    private class InternalK8sApiConfigListener implements K8sApiConfigListener {
+
+        private boolean isRelevantHelper() {
+            return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
+        }
+
+        @Override
+        public void event(K8sApiConfigEvent event) {
+
+            switch (event.type()) {
+                case K8S_API_CONFIG_UPDATED:
+                    eventExecutor.execute(this::processConfigUpdating);
+                    break;
+                case K8S_API_CONFIG_CREATED:
+                case K8S_API_CONFIG_REMOVED:
+                default:
+                    // do nothing
+                    break;
+            }
+        }
+
+        private void processConfigUpdating() {
+            if (!isRelevantHelper()) {
+                return;
+            }
+
+            KubernetesClient client = k8sClient(k8sApiConfigService);
+
+            if (client != null) {
+                client.namespaces().watch(internalK8sNamespaceWatcher);
+            }
+        }
+    }
+
+    private class InternalK8sNamespaceWatcher implements Watcher<Namespace> {
+
+        @Override
+        public void eventReceived(Action action, Namespace namespace) {
+            switch (action) {
+                case ADDED:
+                    eventExecutor.execute(() -> processAddition(namespace));
+                    break;
+                case MODIFIED:
+                    eventExecutor.execute(() -> processModification(namespace));
+                    break;
+                case DELETED:
+                    eventExecutor.execute(() -> processDeletion(namespace));
+                    break;
+                case ERROR:
+                    log.warn("Failures processing namespace manipulation.");
+                    break;
+                default:
+                    // do nothing
+                    break;
+            }
+        }
+
+        @Override
+        public void onClose(KubernetesClientException e) {
+            log.info("Namespace watcher OnClose: {}" + e);
+        }
+
+        private void processAddition(Namespace namespace) {
+            if (!isMaster()) {
+                return;
+            }
+
+            log.trace("Process namespace {} creating event from API server.",
+                    namespace.getMetadata().getName());
+
+            k8sNamespaceAdminService.createNamespace(namespace);
+        }
+
+        private void processModification(Namespace namespace) {
+            if (!isMaster()) {
+                return;
+            }
+
+            log.trace("Process namespace {} updating event from API server.",
+                    namespace.getMetadata().getName());
+
+            k8sNamespaceAdminService.updateNamespace(namespace);
+        }
+
+        private void processDeletion(Namespace namespace) {
+            if (!isMaster()) {
+                return;
+            }
+
+            log.trace("Process namespace {} removal event from API server.",
+                    namespace.getMetadata().getName());
+
+            k8sNamespaceAdminService.removeNamespace(namespace.getMetadata().getUid());
+        }
+
+        private boolean isMaster() {
+            return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
+        }
+    }
+}
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sManagementWebResource.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sManagementWebResource.java
index 42adb65..b6475e3 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sManagementWebResource.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sManagementWebResource.java
@@ -23,6 +23,7 @@
 import org.onosproject.k8snetworking.api.DefaultK8sPort;
 import org.onosproject.k8snetworking.api.K8sEndpointsAdminService;
 import org.onosproject.k8snetworking.api.K8sIngressAdminService;
+import org.onosproject.k8snetworking.api.K8sNamespaceAdminService;
 import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
 import org.onosproject.k8snetworking.api.K8sNetworkPolicyAdminService;
 import org.onosproject.k8snetworking.api.K8sPodAdminService;
@@ -72,6 +73,8 @@
 
     private final K8sApiConfigService configService = get(K8sApiConfigService.class);
     private final K8sPodAdminService podAdminService = get(K8sPodAdminService.class);
+    private final K8sNamespaceAdminService namespaceAdminService =
+                                            get(K8sNamespaceAdminService.class);
     private final K8sServiceAdminService serviceAdminService =
                                             get(K8sServiceAdminService.class);
     private final K8sIngressAdminService ingressAdminService =
@@ -107,6 +110,14 @@
             throw new ItemNotFoundException("Failed to connect to kubernetes API server.");
         }
 
+        client.namespaces().list().getItems().forEach(ns -> {
+            if (namespaceAdminService.namespace(ns.getMetadata().getUid()) != null) {
+                namespaceAdminService.updateNamespace(ns);
+            } else {
+                namespaceAdminService.createNamespace(ns);
+            }
+        });
+
         client.services().inAnyNamespace().list().getItems().forEach(svc -> {
             if (serviceAdminService.service(svc.getMetadata().getUid()) != null) {
                 serviceAdminService.updateService(svc);
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/impl/K8sNamespaceManagerTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/impl/K8sNamespaceManagerTest.java
new file mode 100644
index 0000000..6a6cae4
--- /dev/null
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/impl/K8sNamespaceManagerTest.java
@@ -0,0 +1,218 @@
+/*
+ * 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.Namespace;
+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.K8sNamespaceEvent;
+import org.onosproject.k8snetworking.api.K8sNamespaceListener;
+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.K8sNamespaceEvent.Type.K8S_NAMESPACE_CREATED;
+import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_REMOVED;
+import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_UPDATED;
+
+/**
+ * Unit tests for kubernetes network policy manager.
+ */
+public class K8sNamespaceManagerTest {
+
+    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 NAMESPACE_UID = "namespace_uid";
+    private static final String NAMESPACE_NAME = "namespace_name";
+
+    private static final Namespace NAMESPACE =
+            createK8sNamespace(NAMESPACE_UID, NAMESPACE_NAME);
+    private static final Namespace NAMESPACE_UPDATED =
+            createK8sNamespace(NAMESPACE_UID, UPDATED_NAME);
+
+    private final TestK8sNamespaceListener testListener = new TestK8sNamespaceListener();
+
+    private K8sNamespaceManager target;
+    private DistributedK8sNamespaceStore k8sNamespaceStore;
+
+    @Before
+    public void setUp() throws Exception {
+        k8sNamespaceStore = new DistributedK8sNamespaceStore();
+        TestUtils.setField(k8sNamespaceStore, "coreService", new TestCoreService());
+        TestUtils.setField(k8sNamespaceStore, "storageService", new TestStorageService());
+        TestUtils.setField(k8sNamespaceStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
+        k8sNamespaceStore.activate();
+
+        target = new K8sNamespaceManager();
+        TestUtils.setField(target, "coreService", new TestCoreService());
+        target.k8sNamespaceStore = k8sNamespaceStore;
+        target.addListener(testListener);
+        target.activate();
+    }
+
+    @After
+    public void tearDown() {
+        target.removeListener(testListener);
+        k8sNamespaceStore.deactivate();
+        target.deactivate();
+        k8sNamespaceStore = null;
+        target = null;
+    }
+
+    /**
+     * Tests if getting all namespaces return correct set of namespaces.
+     */
+    @Test
+    public void testGetNamespaces() {
+        createBasicNamespaces();
+        assertEquals("Number of namespaces did not match", 1, target.namespaces().size());
+    }
+
+    /**
+     * Tests if getting a namespace with UID returns the correct namespace.
+     */
+    @Test
+    public void testGetNamespaceByUid() {
+        createBasicNamespaces();
+        assertNotNull("Namespace did not match", target.namespace(NAMESPACE_UID));
+        assertNull("Namespace did not match", target.namespace(UNKNOWN_UID));
+    }
+
+    /**
+     * Tests creating and removing a namespace, and checks if it triggers proper events.
+     */
+    @Test
+    public void testCreateAndRemoveNetworkPolicy() {
+        target.createNamespace(NAMESPACE);
+        assertEquals("Number of namespaces did not match", 1, target.namespaces().size());
+        assertNotNull("Namespace was not created", target.namespace(NAMESPACE_UID));
+
+        target.removeNamespace(NAMESPACE_UID);
+        assertEquals("Number of namespaces did not match", 0, target.namespaces().size());
+        assertNull("Namespace was not removed", target.namespace(NAMESPACE_UID));
+        validateEvents(K8S_NAMESPACE_CREATED, K8S_NAMESPACE_REMOVED);
+    }
+
+    /**
+     * Tests updating a namespace, and checks if it triggers proper events.
+     */
+    @Test
+    public void testCreateAndUpdateNamespace() {
+        target.createNamespace(NAMESPACE);
+        assertEquals("Number of namespaces did not match", 1, target.namespaces().size());
+        assertEquals("Namespace did not match", NAMESPACE_NAME,
+                target.namespace(NAMESPACE_UID).getMetadata().getName());
+
+        target.updateNamespace(NAMESPACE_UPDATED);
+        assertEquals("Number of namespaces did not match", 1, target.namespaces().size());
+        assertEquals("Namespace did not match", UPDATED_NAME,
+                target.namespace(NAMESPACE_UID).getMetadata().getName());
+        validateEvents(K8S_NAMESPACE_CREATED, K8S_NAMESPACE_UPDATED);
+    }
+
+    /**
+     * Tests if creating a null namespace fails with an exception.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testCreateNullNamespace() {
+        target.createNamespace(null);
+    }
+
+    /**
+     * Tests if creating a duplicate namespaces fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateDuplicatedNamespace() {
+        target.createNamespace(NAMESPACE);
+        target.createNamespace(NAMESPACE);
+    }
+
+    /**
+     * Tests if removing namespace with null ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testRemoveNamespaceWithNull() {
+        target.removeNamespace(null);
+    }
+
+    /**
+     * Tests if updating an unregistered namespace fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateUnregisteredNamespace() {
+        target.updateNamespace(NAMESPACE);
+    }
+
+    private void createBasicNamespaces() {
+        target.createNamespace(NAMESPACE);
+    }
+
+    private static Namespace createK8sNamespace(String uid, String name) {
+        ObjectMeta meta = new ObjectMeta();
+        meta.setUid(uid);
+        meta.setName(name);
+
+        Namespace namespace = new Namespace();
+        namespace.setApiVersion("v1");
+        namespace.setKind("Namespace");
+        namespace.setMetadata(meta);
+
+        return namespace;
+    }
+
+    private static class TestCoreService extends CoreServiceAdapter {
+
+        @Override
+        public ApplicationId registerApplication(String name) {
+            return TEST_APP_ID;
+        }
+    }
+
+    private static class TestK8sNamespaceListener implements K8sNamespaceListener {
+        private List<K8sNamespaceEvent> events = Lists.newArrayList();
+
+        @Override
+        public void event(K8sNamespaceEvent 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();
+    }
+}