sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 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.openstacknetworking.impl; |
| 17 | |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 19 | import org.onlab.util.KryoNamespace; |
| 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.core.CoreService; |
| 22 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent; |
| 23 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStore; |
| 24 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStoreDelegate; |
| 25 | import org.onosproject.store.AbstractStore; |
| 26 | import org.onosproject.store.serializers.KryoNamespaces; |
| 27 | import org.onosproject.store.service.ConsistentMap; |
| 28 | import org.onosproject.store.service.MapEvent; |
| 29 | import org.onosproject.store.service.MapEventListener; |
| 30 | import org.onosproject.store.service.Serializer; |
| 31 | import org.onosproject.store.service.StorageService; |
| 32 | import org.onosproject.store.service.Versioned; |
| 33 | import org.openstack4j.model.network.SecurityGroup; |
| 34 | import org.openstack4j.model.network.SecurityGroupRule; |
| 35 | import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup; |
| 36 | import org.openstack4j.openstack.networking.domain.NeutronSecurityGroupRule; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 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; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 44 | import java.util.Set; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 45 | import java.util.concurrent.ExecutorService; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 46 | import java.util.stream.Collectors; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 47 | |
| 48 | import static com.google.common.base.Preconditions.checkArgument; |
| 49 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 50 | import static org.onlab.util.Tools.groupedThreads; |
| 51 | import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID; |
Jian Li | 78885a2 | 2018-03-02 11:33:02 +0900 | [diff] [blame] | 52 | import static org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent.Type.OPENSTACK_SECURITY_GROUP_CREATED; |
| 53 | import static org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent.Type.OPENSTACK_SECURITY_GROUP_REMOVED; |
| 54 | import static org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent.Type.OPENSTACK_SECURITY_GROUP_RULE_CREATED; |
| 55 | import static org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent.Type.OPENSTACK_SECURITY_GROUP_RULE_REMOVED; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 56 | import static org.slf4j.LoggerFactory.getLogger; |
| 57 | |
| 58 | /** |
| 59 | * Manages the inventory of OpenStack security group using a {@code ConsistentMap}. |
| 60 | * |
| 61 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | @Component(immediate = true, service = OpenstackSecurityGroupStore.class) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 63 | public class DistributedSecurityGroupStore |
| 64 | extends AbstractStore<OpenstackSecurityGroupEvent, OpenstackSecurityGroupStoreDelegate> |
| 65 | implements OpenstackSecurityGroupStore { |
| 66 | |
| 67 | protected final Logger log = getLogger(getClass()); |
| 68 | |
| 69 | private static final String ERR_NOT_FOUND = " does not exist"; |
| 70 | private static final String ERR_DUPLICATE = " already exists"; |
| 71 | |
| 72 | private static final KryoNamespace SERIALIZER_SECURITY_GROUP = KryoNamespace.newBuilder() |
| 73 | .register(KryoNamespaces.API) |
| 74 | .register(SecurityGroup.class) |
| 75 | .register(SecurityGroupRule.class) |
| 76 | .register(NeutronSecurityGroupRule.class) |
| 77 | .register(NeutronSecurityGroup.class) |
| 78 | .build(); |
| 79 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 81 | protected CoreService coreService; |
| 82 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 84 | protected StorageService storageService; |
| 85 | |
| 86 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 87 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 88 | |
| 89 | private final MapEventListener<String, SecurityGroup> securityGroupMapListener = |
| 90 | new OpenstackSecurityGroupMapListener(); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 91 | |
| 92 | private ConsistentMap<String, SecurityGroup> osSecurityGroupStore; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 93 | |
| 94 | @Activate |
| 95 | protected void activate() { |
| 96 | ApplicationId appId = coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID); |
| 97 | |
| 98 | osSecurityGroupStore = storageService.<String, SecurityGroup>consistentMapBuilder() |
| 99 | .withSerializer(Serializer.using(SERIALIZER_SECURITY_GROUP)) |
| 100 | .withName("openstack-securitygroupstore") |
| 101 | .withApplicationId(appId) |
| 102 | .build(); |
| 103 | osSecurityGroupStore.addListener(securityGroupMapListener); |
| 104 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 105 | log.info("Started"); |
| 106 | } |
| 107 | |
| 108 | @Deactivate |
| 109 | protected void deactivate() { |
| 110 | osSecurityGroupStore.removeListener(securityGroupMapListener); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 111 | eventExecutor.shutdown(); |
| 112 | |
| 113 | log.info("Stopped"); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void createSecurityGroup(SecurityGroup sg) { |
| 118 | osSecurityGroupStore.compute(sg.getId(), (id, existing) -> { |
| 119 | final String error = sg.getName() + ERR_DUPLICATE; |
| 120 | checkArgument(existing == null, error); |
| 121 | return sg; |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | @Override |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 126 | public void updateSecurityGroup(SecurityGroup sg) { |
| 127 | osSecurityGroupStore.compute(sg.getId(), (id, existing) -> { |
| 128 | final String error = sg.getName() + ERR_NOT_FOUND; |
| 129 | checkArgument(existing != null, error); |
| 130 | return sg; |
| 131 | }); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public SecurityGroup removeSecurityGroup(String sgId) { |
| 136 | Versioned<SecurityGroup> sg = osSecurityGroupStore.remove(sgId); |
| 137 | return sg == null ? null : sg.value(); |
| 138 | } |
| 139 | |
| 140 | @Override |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 141 | public SecurityGroup securityGroup(String sgId) { |
Jian Li | 78885a2 | 2018-03-02 11:33:02 +0900 | [diff] [blame] | 142 | return osSecurityGroupStore.asJavaMap().get(sgId); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 146 | public Set<SecurityGroup> securityGroups() { |
Jian Li | 78885a2 | 2018-03-02 11:33:02 +0900 | [diff] [blame] | 147 | return ImmutableSet.copyOf(osSecurityGroupStore.asJavaMap().values()); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void clear() { |
| 152 | osSecurityGroupStore.clear(); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 153 | } |
| 154 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame^] | 155 | private class OpenstackSecurityGroupMapListener |
| 156 | implements MapEventListener<String, SecurityGroup> { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 157 | |
| 158 | @Override |
| 159 | public void event(MapEvent<String, SecurityGroup> event) { |
| 160 | switch (event.type()) { |
| 161 | case INSERT: |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 162 | log.debug("OpenStack security group created {}", event.newValue()); |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame^] | 163 | eventExecutor.execute(() -> |
| 164 | notifyDelegate(new OpenstackSecurityGroupEvent( |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 165 | OPENSTACK_SECURITY_GROUP_CREATED, |
| 166 | event.newValue().value()))); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 167 | break; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 168 | case UPDATE: |
| 169 | log.debug("OpenStack security group updated {}", event.newValue()); |
| 170 | eventExecutor.execute(() -> processUpdate( |
Jian Li | 6a47fd0 | 2018-11-27 21:51:03 +0900 | [diff] [blame] | 171 | event.oldValue().value(), |
| 172 | event.newValue().value())); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 173 | break; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 174 | case REMOVE: |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 175 | log.debug("OpenStack security group removed {}", event.oldValue()); |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame^] | 176 | eventExecutor.execute(() -> |
| 177 | notifyDelegate(new OpenstackSecurityGroupEvent( |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 178 | OPENSTACK_SECURITY_GROUP_REMOVED, |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 179 | event.oldValue().value()))); |
| 180 | break; |
| 181 | default: |
| 182 | } |
| 183 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 184 | |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 185 | private void processUpdate(SecurityGroup oldSg, SecurityGroup newSg) { |
| 186 | Set<String> oldSgRuleIds = oldSg.getRules().stream() |
| 187 | .map(SecurityGroupRule::getId).collect(Collectors.toSet()); |
| 188 | Set<String> newSgRuleIds = newSg.getRules().stream() |
| 189 | .map(SecurityGroupRule::getId).collect(Collectors.toSet()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 190 | |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 191 | oldSg.getRules().stream().filter(sgRule -> !newSgRuleIds.contains(sgRule.getId())) |
| 192 | .forEach(sgRule -> notifyDelegate(new OpenstackSecurityGroupEvent( |
| 193 | OPENSTACK_SECURITY_GROUP_RULE_REMOVED, newSg, sgRule) |
| 194 | )); |
| 195 | newSg.getRules().stream().filter(sgRule -> !oldSgRuleIds.contains(sgRule.getId())) |
| 196 | .forEach(sgRule -> notifyDelegate(new OpenstackSecurityGroupEvent( |
| 197 | OPENSTACK_SECURITY_GROUP_RULE_CREATED, newSg, sgRule) |
| 198 | )); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |