Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-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.k8snode.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.k8snode.api.DefaultK8sHost; |
| 24 | import org.onosproject.k8snode.api.K8sHost; |
| 25 | import org.onosproject.k8snode.api.K8sHostEvent; |
| 26 | import org.onosproject.k8snode.api.K8sHostState; |
| 27 | import org.onosproject.k8snode.api.K8sHostStore; |
| 28 | import org.onosproject.k8snode.api.K8sHostStoreDelegate; |
| 29 | import org.onosproject.store.AbstractStore; |
| 30 | import org.onosproject.store.serializers.KryoNamespaces; |
| 31 | import org.onosproject.store.service.ConsistentMap; |
| 32 | import org.onosproject.store.service.MapEvent; |
| 33 | import org.onosproject.store.service.MapEventListener; |
| 34 | import org.onosproject.store.service.Serializer; |
| 35 | import org.onosproject.store.service.StorageService; |
| 36 | import org.onosproject.store.service.Versioned; |
| 37 | import org.osgi.service.component.annotations.Activate; |
| 38 | import org.osgi.service.component.annotations.Component; |
| 39 | import org.osgi.service.component.annotations.Deactivate; |
| 40 | import org.osgi.service.component.annotations.Reference; |
| 41 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 42 | import org.slf4j.Logger; |
| 43 | |
| 44 | import java.util.Collection; |
| 45 | import java.util.HashSet; |
| 46 | import java.util.Set; |
| 47 | import java.util.concurrent.ExecutorService; |
| 48 | |
| 49 | import static com.google.common.base.Preconditions.checkArgument; |
| 50 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 51 | import static org.onlab.util.Tools.groupedThreads; |
| 52 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_COMPLETE; |
| 53 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_CREATED; |
| 54 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_INCOMPLETE; |
| 55 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_REMOVED; |
| 56 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_UPDATED; |
| 57 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_NODES_ADDED; |
| 58 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_NODES_REMOVED; |
| 59 | import static org.onosproject.k8snode.api.K8sHostState.COMPLETE; |
| 60 | import static org.onosproject.k8snode.api.K8sHostState.INCOMPLETE; |
| 61 | import static org.slf4j.LoggerFactory.getLogger; |
| 62 | |
| 63 | /** |
| 64 | * Implementation of kubernetes host store using consistent map. |
| 65 | */ |
| 66 | @Component(immediate = true, service = K8sHostStore.class) |
| 67 | public class DistributedK8sHostStore |
| 68 | extends AbstractStore<K8sHostEvent, K8sHostStoreDelegate> |
| 69 | implements K8sHostStore { |
| 70 | |
| 71 | private final Logger log = getLogger(getClass()); |
| 72 | |
| 73 | private static final String ERR_NOT_FOUND = " does not exist"; |
| 74 | private static final String ERR_DUPLICATE = " already exists"; |
| 75 | private static final String APP_ID = "org.onosproject.k8snode"; |
| 76 | |
| 77 | private static final KryoNamespace |
| 78 | SERIALIZER_K8S_HOST = KryoNamespace.newBuilder() |
| 79 | .register(KryoNamespaces.API) |
| 80 | .register(K8sHost.class) |
| 81 | .register(DefaultK8sHost.class) |
| 82 | .register(K8sHostState.class) |
| 83 | .register(Collection.class) |
| 84 | .build(); |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 87 | protected CoreService coreService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 90 | protected StorageService storageService; |
| 91 | |
| 92 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 93 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 94 | |
| 95 | private final MapEventListener<String, K8sHost> hostMapListener = |
| 96 | new K8sHostMapListener(); |
| 97 | private ConsistentMap<String, K8sHost> hostStore; |
| 98 | |
| 99 | @Activate |
| 100 | protected void activate() { |
| 101 | ApplicationId appId = coreService.registerApplication(APP_ID); |
| 102 | hostStore = storageService.<String, K8sHost>consistentMapBuilder() |
| 103 | .withSerializer(Serializer.using(SERIALIZER_K8S_HOST)) |
| 104 | .withName("k8s-hoststore") |
| 105 | .withApplicationId(appId) |
| 106 | .build(); |
| 107 | hostStore.addListener(hostMapListener); |
| 108 | log.info("Started"); |
| 109 | } |
| 110 | |
| 111 | @Deactivate |
| 112 | protected void deactivate() { |
| 113 | hostStore.removeListener(hostMapListener); |
| 114 | eventExecutor.shutdown(); |
| 115 | log.info("Stopped"); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void createHost(K8sHost host) { |
| 120 | hostStore.compute(host.hostIp().toString(), (hostIp, existing) -> { |
| 121 | final String error = host.hostIp().toString() + ERR_DUPLICATE; |
| 122 | checkArgument(existing == null, error); |
| 123 | return host; |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void updateHost(K8sHost host) { |
| 129 | hostStore.compute(host.hostIp().toString(), (hostIp, existing) -> { |
| 130 | final String error = host.hostIp().toString() + ERR_NOT_FOUND; |
| 131 | checkArgument(existing != null, error); |
| 132 | return host; |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public K8sHost removeHost(IpAddress hostIp) { |
| 138 | Versioned<K8sHost> host = hostStore.remove(hostIp.toString()); |
| 139 | if (host == null) { |
| 140 | final String error = hostIp.toString() + ERR_NOT_FOUND; |
| 141 | throw new IllegalArgumentException(error); |
| 142 | } |
| 143 | return host.value(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Set<K8sHost> hosts() { |
| 148 | return ImmutableSet.copyOf(hostStore.asJavaMap().values()); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public K8sHost host(IpAddress hostIp) { |
| 153 | return hostStore.asJavaMap().get(hostIp.toString()); |
| 154 | } |
| 155 | |
| 156 | private class K8sHostMapListener |
| 157 | implements MapEventListener<String, K8sHost> { |
| 158 | |
| 159 | @Override |
| 160 | public void event(MapEvent<String, K8sHost> event) { |
| 161 | switch (event.type()) { |
| 162 | case INSERT: |
| 163 | log.debug("Kubernetes host created {}", event.newValue()); |
| 164 | eventExecutor.execute(() -> |
| 165 | notifyDelegate(new K8sHostEvent( |
| 166 | K8S_HOST_CREATED, event.newValue().value() |
| 167 | ))); |
| 168 | break; |
| 169 | case UPDATE: |
| 170 | log.debug("Kubernetes host updated {}", event.newValue()); |
| 171 | eventExecutor.execute(() -> { |
| 172 | notifyDelegate(new K8sHostEvent( |
| 173 | K8S_HOST_UPDATED, event.newValue().value() |
| 174 | )); |
| 175 | |
| 176 | if (event.newValue().value().state() == COMPLETE) { |
| 177 | notifyDelegate(new K8sHostEvent( |
| 178 | K8S_HOST_COMPLETE, |
| 179 | event.newValue().value() |
| 180 | )); |
| 181 | } else if (event.newValue().value().state() == INCOMPLETE) { |
| 182 | notifyDelegate(new K8sHostEvent( |
| 183 | K8S_HOST_INCOMPLETE, |
| 184 | event.newValue().value() |
| 185 | )); |
| 186 | } |
| 187 | |
| 188 | K8sHost origHost = event.newValue().value(); |
| 189 | Set<String> oldNodes = event.oldValue().value().nodeNames(); |
| 190 | Set<String> newNodes = event.newValue().value().nodeNames(); |
| 191 | |
| 192 | Set<String> addedNodes = new HashSet<>(newNodes); |
| 193 | Set<String> removedNodes = new HashSet<>(oldNodes); |
| 194 | |
| 195 | addedNodes.removeAll(oldNodes); |
| 196 | removedNodes.removeAll(newNodes); |
| 197 | |
| 198 | if (addedNodes.size() > 0) { |
| 199 | K8sHost addedHost = DefaultK8sHost.builder() |
| 200 | .hostIp(origHost.hostIp()) |
| 201 | .state(origHost.state()) |
| 202 | .nodeNames(addedNodes) |
| 203 | .build(); |
| 204 | notifyDelegate(new K8sHostEvent(K8S_NODES_ADDED, addedHost)); |
| 205 | } |
| 206 | |
| 207 | if (removedNodes.size() > 0) { |
| 208 | K8sHost removedHost = DefaultK8sHost.builder() |
| 209 | .hostIp(origHost.hostIp()) |
| 210 | .state(origHost.state()) |
| 211 | .nodeNames(removedNodes) |
| 212 | .build(); |
| 213 | notifyDelegate(new K8sHostEvent(K8S_NODES_REMOVED, removedHost)); |
| 214 | } |
| 215 | }); |
| 216 | break; |
| 217 | case REMOVE: |
| 218 | log.debug("Kubernetes host removed {}", event.oldValue()); |
| 219 | eventExecutor.execute(() -> |
| 220 | notifyDelegate(new K8sHostEvent( |
| 221 | K8S_HOST_REMOVED, event.oldValue().value() |
| 222 | ))); |
| 223 | break; |
| 224 | default: |
| 225 | // do nothing |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |