Jian Li | 0b48112 | 2021-01-17 04:26:18 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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; |
| 19 | import org.onlab.packet.MacAddress; |
| 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtPort; |
| 24 | import org.onosproject.kubevirtnetworking.api.KubevirtPort; |
| 25 | import org.onosproject.kubevirtnetworking.api.KubevirtPortEvent; |
| 26 | import org.onosproject.kubevirtnetworking.api.KubevirtPortStore; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtPortStoreDelegate; |
| 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.KubevirtPortEvent.Type.KUBEVIRT_PORT_CREATED; |
| 51 | import static org.onosproject.kubevirtnetworking.api.KubevirtPortEvent.Type.KUBEVIRT_PORT_REMOVED; |
| 52 | import static org.onosproject.kubevirtnetworking.api.KubevirtPortEvent.Type.KUBEVIRT_PORT_UPDATED; |
| 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
| 55 | /** |
| 56 | * Implementation of kubevirt pod store using consistent map. |
| 57 | */ |
| 58 | @Component(immediate = true, service = KubevirtPortStore.class) |
| 59 | public class DistributedKubevirtPortStore |
| 60 | extends AbstractStore<KubevirtPortEvent, KubevirtPortStoreDelegate> |
| 61 | implements KubevirtPortStore { |
| 62 | |
| 63 | private final Logger log = getLogger(getClass()); |
| 64 | |
| 65 | private static final String ERR_NOT_FOUND = " does not exist"; |
| 66 | private static final String ERR_DUPLICATE = " already exists"; |
| 67 | private static final String APP_ID = "org.onosproject.kubevirtnetwork"; |
| 68 | |
| 69 | private static final KryoNamespace |
| 70 | SERIALIZER_KUBEVIRT_PORT = KryoNamespace.newBuilder() |
| 71 | .register(KryoNamespaces.API) |
| 72 | .register(KubevirtPort.class) |
| 73 | .register(DefaultKubevirtPort.class) |
| 74 | .register(Collection.class) |
| 75 | .build(); |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 78 | protected CoreService coreService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 81 | protected StorageService storageService; |
| 82 | |
| 83 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 84 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 85 | |
| 86 | private final MapEventListener<String, KubevirtPort> portMapListener = |
| 87 | new KubevirtPortMapListener(); |
| 88 | |
| 89 | private ConsistentMap<String, KubevirtPort> portStore; |
| 90 | |
| 91 | @Activate |
| 92 | protected void activate() { |
| 93 | ApplicationId appId = coreService.registerApplication(APP_ID); |
| 94 | portStore = storageService.<String, KubevirtPort>consistentMapBuilder() |
| 95 | .withSerializer(Serializer.using(SERIALIZER_KUBEVIRT_PORT)) |
| 96 | .withName("kubevirt-portstore") |
| 97 | .withApplicationId(appId) |
| 98 | .build(); |
| 99 | portStore.addListener(portMapListener); |
| 100 | log.info("Started"); |
| 101 | } |
| 102 | |
| 103 | @Deactivate |
| 104 | protected void deactivate() { |
| 105 | portStore.removeListener(portMapListener); |
| 106 | eventExecutor.shutdown(); |
| 107 | log.info("Stopped"); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void createPort(KubevirtPort port) { |
| 112 | portStore.compute(port.macAddress().toString(), (mac, existing) -> { |
| 113 | final String error = port.macAddress().toString() + ERR_DUPLICATE; |
| 114 | checkArgument(existing == null, error); |
| 115 | return port; |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void updatePort(KubevirtPort port) { |
| 121 | portStore.compute(port.macAddress().toString(), (mac, existing) -> { |
| 122 | final String error = port.macAddress().toString() + ERR_NOT_FOUND; |
| 123 | checkArgument(existing != null, error); |
| 124 | return port; |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public KubevirtPort removePort(MacAddress mac) { |
| 130 | Versioned<KubevirtPort> port = portStore.remove(mac.toString()); |
| 131 | if (port == null) { |
| 132 | final String error = mac.toString() + ERR_NOT_FOUND; |
| 133 | throw new IllegalArgumentException(error); |
| 134 | } |
| 135 | return port.value(); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public KubevirtPort port(MacAddress mac) { |
| 140 | return portStore.asJavaMap().get(mac.toString()); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Set<KubevirtPort> ports() { |
| 145 | return ImmutableSet.copyOf(portStore.asJavaMap().values()); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void clear() { |
| 150 | portStore.clear(); |
| 151 | } |
| 152 | |
| 153 | private class KubevirtPortMapListener implements MapEventListener<String, KubevirtPort> { |
| 154 | |
| 155 | @Override |
| 156 | public void event(MapEvent<String, KubevirtPort> event) { |
| 157 | switch (event.type()) { |
| 158 | case INSERT: |
| 159 | log.debug("Kubevirt port created"); |
| 160 | eventExecutor.execute(() -> |
| 161 | notifyDelegate(new KubevirtPortEvent( |
| 162 | KUBEVIRT_PORT_CREATED, event.newValue().value()))); |
| 163 | break; |
| 164 | case UPDATE: |
| 165 | log.debug("Kubevirt port updated"); |
| 166 | eventExecutor.execute(() -> |
| 167 | notifyDelegate(new KubevirtPortEvent( |
| 168 | KUBEVIRT_PORT_UPDATED, event.newValue().value()))); |
| 169 | break; |
| 170 | case REMOVE: |
| 171 | log.debug("Kubevirt port removed"); |
| 172 | // if the event object has invalid port value, we do not |
| 173 | // propagate KUBEVIRT_PORT_REMOVED event. |
| 174 | if (event.oldValue() != null && event.oldValue().value() != null) { |
| 175 | notifyDelegate(new KubevirtPortEvent( |
| 176 | KUBEVIRT_PORT_REMOVED, event.oldValue().value())); |
| 177 | } |
| 178 | break; |
| 179 | default: |
| 180 | // do nothing |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |