Jian Li | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.k8snetworking.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Jian Li | adebcbc | 2020-09-23 01:25:08 +0900 | [diff] [blame^] | 19 | import io.fabric8.kubernetes.api.model.FieldsV1; |
Jian Li | 7388ff5 | 2020-09-22 23:25:06 +0900 | [diff] [blame] | 20 | import io.fabric8.kubernetes.api.model.ManagedFieldsEntry; |
Jian Li | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 21 | import io.fabric8.kubernetes.api.model.Namespace; |
| 22 | import io.fabric8.kubernetes.api.model.NamespaceSpec; |
| 23 | import io.fabric8.kubernetes.api.model.NamespaceStatus; |
| 24 | import io.fabric8.kubernetes.api.model.ObjectMeta; |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.k8snetworking.api.K8sNamespaceEvent; |
| 29 | import org.onosproject.k8snetworking.api.K8sNamespaceStore; |
| 30 | import org.onosproject.k8snetworking.api.K8sNamespaceStoreDelegate; |
| 31 | import org.onosproject.store.AbstractStore; |
| 32 | import org.onosproject.store.serializers.KryoNamespaces; |
| 33 | import org.onosproject.store.service.ConsistentMap; |
| 34 | import org.onosproject.store.service.MapEvent; |
| 35 | import org.onosproject.store.service.MapEventListener; |
| 36 | import org.onosproject.store.service.Serializer; |
| 37 | import org.onosproject.store.service.StorageService; |
| 38 | import org.onosproject.store.service.Versioned; |
| 39 | import org.osgi.service.component.annotations.Activate; |
| 40 | import org.osgi.service.component.annotations.Component; |
| 41 | import org.osgi.service.component.annotations.Deactivate; |
| 42 | import org.osgi.service.component.annotations.Reference; |
| 43 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 44 | import org.slf4j.Logger; |
| 45 | |
Jian Li | 0f45961 | 2019-07-11 10:38:02 +0900 | [diff] [blame] | 46 | import java.util.LinkedHashMap; |
Jian Li | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 47 | import java.util.Set; |
| 48 | import java.util.concurrent.ExecutorService; |
| 49 | |
| 50 | import static com.google.common.base.Preconditions.checkArgument; |
| 51 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 52 | import static org.onlab.util.Tools.groupedThreads; |
| 53 | import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_CREATED; |
| 54 | import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_REMOVED; |
| 55 | import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_UPDATED; |
| 56 | import static org.slf4j.LoggerFactory.getLogger; |
| 57 | |
| 58 | /** |
| 59 | * Implementation of kubernetes namespace store using consistent map. |
| 60 | */ |
| 61 | @Component(immediate = true, service = K8sNamespaceStore.class) |
| 62 | public class DistributedK8sNamespaceStore |
| 63 | extends AbstractStore<K8sNamespaceEvent, K8sNamespaceStoreDelegate> |
| 64 | implements K8sNamespaceStore { |
| 65 | |
| 66 | private final Logger log = getLogger(getClass()); |
| 67 | |
| 68 | private static final String ERR_NOT_FOUND = " does not exist"; |
| 69 | private static final String ERR_DUPLICATE = " already exists"; |
| 70 | private static final String APP_ID = "org.onosproject.k8snetwork"; |
| 71 | |
| 72 | private static final KryoNamespace |
| 73 | SERIALIZER_K8S_NAMESPACE = KryoNamespace.newBuilder() |
| 74 | .register(KryoNamespaces.API) |
| 75 | .register(Namespace.class) |
| 76 | .register(ObjectMeta.class) |
| 77 | .register(NamespaceSpec.class) |
| 78 | .register(NamespaceStatus.class) |
Jian Li | 7388ff5 | 2020-09-22 23:25:06 +0900 | [diff] [blame] | 79 | .register(ManagedFieldsEntry.class) |
Jian Li | adebcbc | 2020-09-23 01:25:08 +0900 | [diff] [blame^] | 80 | .register(FieldsV1.class) |
Jian Li | 0f45961 | 2019-07-11 10:38:02 +0900 | [diff] [blame] | 81 | .register(LinkedHashMap.class) |
Jian Li | 324d6dc | 2019-07-10 10:55:15 +0900 | [diff] [blame] | 82 | .build(); |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 85 | protected CoreService coreService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 88 | protected StorageService storageService; |
| 89 | |
| 90 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 91 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 92 | |
| 93 | private final MapEventListener<String, Namespace> namespaceMapListener = |
| 94 | new K8sNamespaceMapListener(); |
| 95 | |
| 96 | private ConsistentMap<String, Namespace> namespaceStore; |
| 97 | |
| 98 | @Activate |
| 99 | protected void activate() { |
| 100 | ApplicationId appId = coreService.registerApplication(APP_ID); |
| 101 | namespaceStore = storageService.<String, Namespace>consistentMapBuilder() |
| 102 | .withSerializer(Serializer.using(SERIALIZER_K8S_NAMESPACE)) |
| 103 | .withName("k8s-namespace-store") |
| 104 | .withApplicationId(appId) |
| 105 | .build(); |
| 106 | |
| 107 | namespaceStore.addListener(namespaceMapListener); |
| 108 | log.info("Started"); |
| 109 | } |
| 110 | |
| 111 | @Deactivate |
| 112 | protected void deactivate() { |
| 113 | namespaceStore.removeListener(namespaceMapListener); |
| 114 | eventExecutor.shutdown(); |
| 115 | log.info("Stopped"); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void createNamespace(Namespace namespace) { |
| 120 | namespaceStore.compute(namespace.getMetadata().getUid(), (uid, existing) -> { |
| 121 | final String error = namespace.getMetadata().getUid() + ERR_DUPLICATE; |
| 122 | checkArgument(existing == null, error); |
| 123 | return namespace; |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void updateNamespace(Namespace namespace) { |
| 129 | namespaceStore.compute(namespace.getMetadata().getUid(), (uid, existing) -> { |
| 130 | final String error = namespace.getMetadata().getUid() + ERR_NOT_FOUND; |
| 131 | checkArgument(existing != null, error); |
| 132 | return namespace; |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Namespace removeNamespace(String uid) { |
| 138 | Versioned<Namespace> namespace = namespaceStore.remove(uid); |
| 139 | if (namespace == null) { |
| 140 | final String error = uid + ERR_NOT_FOUND; |
| 141 | throw new IllegalArgumentException(error); |
| 142 | } |
| 143 | return namespace.value(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Namespace namespace(String uid) { |
| 148 | return namespaceStore.asJavaMap().get(uid); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Set<Namespace> namespaces() { |
| 153 | return ImmutableSet.copyOf(namespaceStore.asJavaMap().values()); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void clear() { |
| 158 | namespaceStore.clear(); |
| 159 | } |
| 160 | |
| 161 | private class K8sNamespaceMapListener implements MapEventListener<String, Namespace> { |
| 162 | |
| 163 | @Override |
| 164 | public void event(MapEvent<String, Namespace> event) { |
| 165 | |
| 166 | switch (event.type()) { |
| 167 | case INSERT: |
| 168 | log.debug("Kubernetes namespace created {}", event.newValue()); |
| 169 | eventExecutor.execute(() -> |
| 170 | notifyDelegate(new K8sNamespaceEvent( |
| 171 | K8S_NAMESPACE_CREATED, event.newValue().value()))); |
| 172 | break; |
| 173 | case UPDATE: |
| 174 | log.debug("Kubernetes namespace updated {}", event.newValue()); |
| 175 | eventExecutor.execute(() -> |
| 176 | notifyDelegate(new K8sNamespaceEvent( |
| 177 | K8S_NAMESPACE_UPDATED, event.newValue().value()))); |
| 178 | break; |
| 179 | case REMOVE: |
| 180 | log.debug("Kubernetes namespace removed {}", event.oldValue()); |
| 181 | eventExecutor.execute(() -> |
| 182 | notifyDelegate(new K8sNamespaceEvent( |
| 183 | K8S_NAMESPACE_REMOVED, event.oldValue().value()))); |
| 184 | break; |
| 185 | default: |
| 186 | // do nothing |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |