Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-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.kubevirtnetworking.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 19 | import org.onlab.packet.IpAddress; |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.kubevirtnetworking.api.DefaultKubernetesExternalLb; |
| 24 | import org.onosproject.kubevirtnetworking.api.KubernetesExternalLb; |
| 25 | import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent; |
| 26 | import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbStore; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbStoreDelegate; |
| 28 | import org.onosproject.store.AbstractStore; |
| 29 | import org.onosproject.store.serializers.KryoNamespaces; |
| 30 | import org.onosproject.store.service.ConsistentMap; |
| 31 | import org.onosproject.store.service.MapEvent; |
| 32 | import org.onosproject.store.service.MapEventListener; |
| 33 | import org.onosproject.store.service.Serializer; |
| 34 | import org.onosproject.store.service.StorageService; |
| 35 | import org.onosproject.store.service.Versioned; |
| 36 | import org.osgi.service.component.annotations.Activate; |
| 37 | import org.osgi.service.component.annotations.Component; |
| 38 | import org.osgi.service.component.annotations.Deactivate; |
| 39 | import org.osgi.service.component.annotations.Reference; |
| 40 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | import java.util.Collection; |
| 44 | import java.util.Set; |
| 45 | import java.util.concurrent.ExecutorService; |
| 46 | |
| 47 | import static com.google.common.base.Preconditions.checkArgument; |
| 48 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 49 | import static org.onlab.util.Tools.groupedThreads; |
| 50 | import static org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent.Type.KUBERNETES_EXTERNAL_LOAD_BALANCER_CREATED; |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 51 | import static org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent.Type.KUBERNETES_EXTERNAL_LOAD_BALANCER_GATEWAY_CHANGED; |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 52 | import static org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent.Type.KUBERNETES_EXTERNAL_LOAD_BALANCER_REMOVED; |
| 53 | import static org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent.Type.KUBERNETES_EXTERNAL_LOAD_BALANCER_UPDATED; |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 54 | import static org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent.Type.KUBERNETES_EXTERNAL_LOAD_BALANCER_WORKER_CHANGED; |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | |
| 57 | /** |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 58 | * Implementation of kubernetes external load balancer store using consistent map. |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 59 | */ |
| 60 | @Component(immediate = true, service = KubernetesExternalLbStore.class) |
| 61 | public class DistributedKubernetesExternalLbStore |
| 62 | extends AbstractStore<KubernetesExternalLbEvent, KubernetesExternalLbStoreDelegate> |
| 63 | implements KubernetesExternalLbStore { |
| 64 | |
| 65 | private final Logger log = getLogger(getClass()); |
| 66 | |
| 67 | private static final String ERR_NOT_FOUND = " does not exist"; |
| 68 | private static final String ERR_DUPLICATE = " already exists"; |
| 69 | private static final String APP_ID = "org.onosproject.kubevirtnetwork"; |
| 70 | |
| 71 | private static final KryoNamespace SERIALIZER_KUBERNETES_EXTERNAL_LB = KryoNamespace.newBuilder() |
| 72 | .register(KryoNamespaces.API) |
| 73 | .register(KubernetesExternalLb.class) |
| 74 | .register(DefaultKubernetesExternalLb.class) |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 75 | .register(IpAddress.class) |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 76 | .register(Collection.class) |
| 77 | .build(); |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 80 | protected CoreService coreService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 83 | protected StorageService storageService; |
| 84 | |
| 85 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 86 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 87 | |
| 88 | private final MapEventListener<String, KubernetesExternalLb> lbMapEventListener = |
| 89 | new KubernetesExternalLbMapListener(); |
| 90 | |
| 91 | private ConsistentMap<String, KubernetesExternalLb> lbStore; |
| 92 | |
| 93 | @Activate |
| 94 | protected void activate() { |
| 95 | ApplicationId appId = coreService.registerApplication(APP_ID); |
| 96 | lbStore = storageService.<String, KubernetesExternalLb>consistentMapBuilder() |
| 97 | .withSerializer(Serializer.using(SERIALIZER_KUBERNETES_EXTERNAL_LB)) |
| 98 | .withName("kubernetes-lbstore") |
| 99 | .withApplicationId(appId) |
| 100 | .build(); |
| 101 | |
| 102 | lbStore.addListener(lbMapEventListener); |
| 103 | log.info("Started"); |
| 104 | } |
| 105 | |
| 106 | @Deactivate |
| 107 | protected void deactivate() { |
| 108 | lbStore.removeListener(lbMapEventListener); |
| 109 | eventExecutor.shutdown(); |
| 110 | log.info("Stopped"); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void createLoadBalancer(KubernetesExternalLb lb) { |
| 115 | lbStore.compute(lb.serviceName(), (seviceName, existing) -> { |
| 116 | final String error = lb.serviceName() + ERR_DUPLICATE; |
| 117 | checkArgument(existing == null, error); |
| 118 | return lb; |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void updateLoadBalancer(KubernetesExternalLb lb) { |
| 124 | lbStore.compute(lb.serviceName(), (seviceName, existing) -> { |
| 125 | final String error = lb.serviceName() + ERR_NOT_FOUND; |
| 126 | checkArgument(existing != null, error); |
| 127 | return lb; |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public KubernetesExternalLb removeLoadBalancer(String serviceName) { |
| 133 | |
| 134 | Versioned<KubernetesExternalLb> lb = lbStore.remove(serviceName); |
| 135 | |
| 136 | if (lb == null) { |
| 137 | final String error = serviceName + ERR_NOT_FOUND; |
| 138 | throw new IllegalArgumentException(error); |
| 139 | } |
| 140 | return lb.value(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public KubernetesExternalLb loadBalancer(String serviceName) { |
| 145 | return lbStore.asJavaMap().get(serviceName); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Set<KubernetesExternalLb> loadBalancers() { |
| 150 | return ImmutableSet.copyOf(lbStore.asJavaMap().values()); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void clear() { |
| 155 | lbStore.clear(); |
| 156 | } |
| 157 | |
| 158 | private class KubernetesExternalLbMapListener implements MapEventListener<String, KubernetesExternalLb> { |
| 159 | @Override |
| 160 | public void event(MapEvent<String, KubernetesExternalLb> event) { |
| 161 | switch (event.type()) { |
| 162 | case INSERT: |
| 163 | eventExecutor.execute(() -> |
| 164 | notifyDelegate(new KubernetesExternalLbEvent( |
| 165 | KUBERNETES_EXTERNAL_LOAD_BALANCER_CREATED, event.newValue().value()))); |
| 166 | break; |
| 167 | case UPDATE: |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 168 | eventExecutor.execute(() -> processMapUpdate(event)); |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 169 | break; |
| 170 | case REMOVE: |
| 171 | eventExecutor.execute(() -> |
| 172 | notifyDelegate(new KubernetesExternalLbEvent( |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 173 | KUBERNETES_EXTERNAL_LOAD_BALANCER_REMOVED, event.oldValue().value()))); |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 174 | break; |
| 175 | default: |
| 176 | //do nothing |
| 177 | break; |
| 178 | } |
| 179 | } |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame^] | 180 | |
| 181 | private void processMapUpdate(MapEvent<String, KubernetesExternalLb> event) { |
| 182 | log.debug("Kubernetes External LB updated"); |
| 183 | |
| 184 | KubernetesExternalLb oldValue = event.oldValue().value(); |
| 185 | KubernetesExternalLb newValue = event.newValue().value(); |
| 186 | |
| 187 | if (oldValue.electedGateway() != null && newValue.electedGateway() != null && |
| 188 | !oldValue.electedGateway().equals(newValue.electedGateway())) { |
| 189 | notifyDelegate(new KubernetesExternalLbEvent( |
| 190 | KUBERNETES_EXTERNAL_LOAD_BALANCER_GATEWAY_CHANGED, |
| 191 | newValue, oldValue.electedGateway(), oldValue.electedWorker()) |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | if (oldValue.electedWorker() != null && newValue.electedWorker() != null && |
| 196 | !oldValue.electedWorker().equals(newValue.electedWorker())) { |
| 197 | notifyDelegate(new KubernetesExternalLbEvent( |
| 198 | KUBERNETES_EXTERNAL_LOAD_BALANCER_WORKER_CHANGED, |
| 199 | newValue, oldValue.electedWorker()) |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | notifyDelegate(new KubernetesExternalLbEvent( |
| 204 | KUBERNETES_EXTERNAL_LOAD_BALANCER_UPDATED, event.newValue().value())); |
| 205 | |
| 206 | } |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 207 | } |
| 208 | } |