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