Jian Li | 1c10cf2 | 2021-03-05 01:32:04 +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.util.KryoNamespace; |
| 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.core.CoreService; |
| 22 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtSecurityGroup; |
| 23 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtSecurityGroupRule; |
| 24 | import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroup; |
| 25 | import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupEvent; |
| 26 | import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupRule; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupStore; |
| 28 | import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupStoreDelegate; |
| 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.Set; |
| 46 | import java.util.concurrent.ExecutorService; |
| 47 | import java.util.stream.Collectors; |
| 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.kubevirtnetworking.api.KubevirtSecurityGroupEvent.Type.KUBEVIRT_SECURITY_GROUP_CREATED; |
| 53 | import static org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupEvent.Type.KUBEVIRT_SECURITY_GROUP_REMOVED; |
| 54 | import static org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupEvent.Type.KUBEVIRT_SECURITY_GROUP_RULE_CREATED; |
| 55 | import static org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupEvent.Type.KUBEVIRT_SECURITY_GROUP_RULE_REMOVED; |
| 56 | import static org.slf4j.LoggerFactory.getLogger; |
| 57 | |
| 58 | /** |
| 59 | * Implementation of kubevirt security group store using consistent map. |
| 60 | */ |
| 61 | @Component(immediate = true, service = KubevirtSecurityGroupStore.class) |
| 62 | public class DistributedKubevirtSecurityGroupStore |
| 63 | extends AbstractStore<KubevirtSecurityGroupEvent, KubevirtSecurityGroupStoreDelegate> |
| 64 | implements KubevirtSecurityGroupStore { |
| 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.kubevirtnetwork"; |
| 71 | |
| 72 | |
| 73 | private static final KryoNamespace SERIALIZER_KUBEVIRT_SG = KryoNamespace.newBuilder() |
| 74 | .register(KryoNamespaces.API) |
| 75 | .register(KubevirtSecurityGroup.class) |
| 76 | .register(KubevirtSecurityGroupRule.class) |
| 77 | .register(DefaultKubevirtSecurityGroup.class) |
| 78 | .register(DefaultKubevirtSecurityGroupRule.class) |
| 79 | .register(Collection.class) |
| 80 | .build(); |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 83 | protected CoreService coreService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 86 | protected StorageService storageService; |
| 87 | |
| 88 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 89 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 90 | |
| 91 | private final MapEventListener<String, KubevirtSecurityGroup> securityGroupListener = |
| 92 | new KubevirtSecurityGroupMapListener(); |
| 93 | |
| 94 | private ConsistentMap<String, KubevirtSecurityGroup> sgStore; |
| 95 | |
| 96 | @Activate |
| 97 | protected void activate() { |
| 98 | ApplicationId appId = coreService.registerApplication(APP_ID); |
| 99 | sgStore = storageService.<String, KubevirtSecurityGroup>consistentMapBuilder() |
| 100 | .withSerializer(Serializer.using(SERIALIZER_KUBEVIRT_SG)) |
| 101 | .withName("kubevirt-securitygroupstore") |
| 102 | .withApplicationId(appId) |
| 103 | .build(); |
| 104 | sgStore.addListener(securityGroupListener); |
| 105 | log.info("Started"); |
| 106 | } |
| 107 | |
| 108 | @Deactivate |
| 109 | protected void deactivate() { |
| 110 | sgStore.removeListener(securityGroupListener); |
| 111 | eventExecutor.shutdown(); |
| 112 | log.info("Stopped"); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void createSecurityGroup(KubevirtSecurityGroup sg) { |
| 117 | sgStore.compute(sg.id(), (id, existing) -> { |
| 118 | final String error = sg.id() + ERR_DUPLICATE; |
| 119 | checkArgument(existing == null, error); |
| 120 | return sg; |
| 121 | }); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void updateSecurityGroup(KubevirtSecurityGroup sg) { |
| 126 | sgStore.compute(sg.id(), (id, existing) -> { |
| 127 | final String error = sg.id() + ERR_NOT_FOUND; |
| 128 | checkArgument(existing != null, error); |
| 129 | return sg; |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public KubevirtSecurityGroup removeSecurityGroup(String sgId) { |
| 135 | Versioned<KubevirtSecurityGroup> sg = sgStore.remove(sgId); |
| 136 | if (sg == null) { |
| 137 | final String error = sgId + ERR_NOT_FOUND; |
| 138 | throw new IllegalArgumentException(error); |
| 139 | } |
| 140 | return sg.value(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public KubevirtSecurityGroup securityGroup(String sgId) { |
| 145 | return sgStore.asJavaMap().get(sgId); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Set<KubevirtSecurityGroup> securityGroups() { |
| 150 | return ImmutableSet.copyOf(sgStore.asJavaMap().values()); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void clear() { |
| 155 | sgStore.clear(); |
| 156 | } |
| 157 | |
| 158 | private class KubevirtSecurityGroupMapListener |
| 159 | implements MapEventListener<String, KubevirtSecurityGroup> { |
| 160 | |
| 161 | @Override |
| 162 | public void event(MapEvent<String, KubevirtSecurityGroup> event) { |
| 163 | |
| 164 | switch (event.type()) { |
| 165 | case INSERT: |
| 166 | log.debug("Kubevirt security group created {}", event.newValue()); |
| 167 | eventExecutor.execute(() -> |
| 168 | notifyDelegate(new KubevirtSecurityGroupEvent( |
| 169 | KUBEVIRT_SECURITY_GROUP_CREATED, event.newValue().value()))); |
| 170 | break; |
| 171 | case UPDATE: |
| 172 | log.debug("Kubevirt security group updated {}", event.newValue()); |
| 173 | eventExecutor.execute(() -> processUpdate( |
| 174 | event.oldValue().value(), |
| 175 | event.newValue().value())); |
| 176 | break; |
| 177 | case REMOVE: |
| 178 | log.debug("Kubevirt security group removed {}", event.oldValue()); |
| 179 | eventExecutor.execute(() -> |
| 180 | notifyDelegate(new KubevirtSecurityGroupEvent( |
| 181 | KUBEVIRT_SECURITY_GROUP_REMOVED, event.oldValue().value()))); |
| 182 | break; |
| 183 | default: |
| 184 | // do nothing |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | private void processUpdate(KubevirtSecurityGroup oldSg, KubevirtSecurityGroup newSg) { |
| 190 | Set<String> oldSgRuleIds = oldSg.rules().stream() |
| 191 | .map(KubevirtSecurityGroupRule::id).collect(Collectors.toSet()); |
| 192 | Set<String> newSgRuleIds = newSg.rules().stream() |
| 193 | .map(KubevirtSecurityGroupRule::id).collect(Collectors.toSet()); |
| 194 | |
| 195 | oldSg.rules().stream().filter(sgRule -> !newSgRuleIds.contains(sgRule.id())) |
| 196 | .forEach(sgRule -> notifyDelegate(new KubevirtSecurityGroupEvent( |
| 197 | KUBEVIRT_SECURITY_GROUP_RULE_REMOVED, newSg, sgRule))); |
| 198 | newSg.rules().stream().filter(sgRule -> !oldSgRuleIds.contains(sgRule.id())) |
| 199 | .forEach(sgRule -> notifyDelegate(new KubevirtSecurityGroupEvent( |
| 200 | KUBEVIRT_SECURITY_GROUP_RULE_CREATED, newSg, sgRule))); |
| 201 | } |
| 202 | } |
| 203 | } |