blob: edbd422469f696a3180db4fd1d5d1bb5046fcdc0 [file] [log] [blame]
sangho6a9ff0d2017-03-27 11:23:37 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
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 */
16package org.onosproject.openstacknetworking.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
24import org.onlab.util.KryoNamespace;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent;
28import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStore;
29import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStoreDelegate;
30import org.onosproject.store.AbstractStore;
31import org.onosproject.store.serializers.KryoNamespaces;
32import org.onosproject.store.service.ConsistentMap;
33import org.onosproject.store.service.MapEvent;
34import org.onosproject.store.service.MapEventListener;
35import org.onosproject.store.service.Serializer;
36import org.onosproject.store.service.StorageService;
37import org.onosproject.store.service.Versioned;
38import org.openstack4j.model.network.SecurityGroup;
39import org.openstack4j.model.network.SecurityGroupRule;
40import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
41import org.openstack4j.openstack.networking.domain.NeutronSecurityGroupRule;
42import org.slf4j.Logger;
43
44import java.util.concurrent.ExecutorService;
45
46import static com.google.common.base.Preconditions.checkArgument;
47import static java.util.concurrent.Executors.newSingleThreadExecutor;
48import static org.onlab.util.Tools.groupedThreads;
49import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID;
50import static org.slf4j.LoggerFactory.getLogger;
51
52/**
53 * Manages the inventory of OpenStack security group using a {@code ConsistentMap}.
54 *
55 */
56@Service
57@Component(immediate = true)
58public class DistributedSecurityGroupStore
59 extends AbstractStore<OpenstackSecurityGroupEvent, OpenstackSecurityGroupStoreDelegate>
60 implements OpenstackSecurityGroupStore {
61
62 protected final Logger log = getLogger(getClass());
63
64 private static final String ERR_NOT_FOUND = " does not exist";
65 private static final String ERR_DUPLICATE = " already exists";
66
67 private static final KryoNamespace SERIALIZER_SECURITY_GROUP = KryoNamespace.newBuilder()
68 .register(KryoNamespaces.API)
69 .register(SecurityGroup.class)
70 .register(SecurityGroupRule.class)
71 .register(NeutronSecurityGroupRule.class)
72 .register(NeutronSecurityGroup.class)
73 .build();
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected CoreService coreService;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected StorageService storageService;
80
81 private final ExecutorService eventExecutor = newSingleThreadExecutor(
82 groupedThreads(this.getClass().getSimpleName(), "event-handler", log));
83
84 private final MapEventListener<String, SecurityGroup> securityGroupMapListener =
85 new OpenstackSecurityGroupMapListener();
86 private final MapEventListener<String, SecurityGroupRule> securityGroupRuleMapListener =
87 new OpenstackSecurityGroupRuleMapListener();
88
89 private ConsistentMap<String, SecurityGroup> osSecurityGroupStore;
90 private ConsistentMap<String, SecurityGroupRule> osSecurityGroupRuleStore;
91
92 @Activate
93 protected void activate() {
94 ApplicationId appId = coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID);
95
96 osSecurityGroupStore = storageService.<String, SecurityGroup>consistentMapBuilder()
97 .withSerializer(Serializer.using(SERIALIZER_SECURITY_GROUP))
98 .withName("openstack-securitygroupstore")
99 .withApplicationId(appId)
100 .build();
101 osSecurityGroupStore.addListener(securityGroupMapListener);
102
103 osSecurityGroupRuleStore = storageService.<String, SecurityGroupRule>consistentMapBuilder()
104 .withSerializer(Serializer.using(SERIALIZER_SECURITY_GROUP))
105 .withName("openstack-securitygrouprulestore")
106 .withApplicationId(appId)
107 .build();
108 osSecurityGroupRuleStore.addListener(securityGroupRuleMapListener);
109
110 log.info("Started");
111 }
112
113 @Deactivate
114 protected void deactivate() {
115 osSecurityGroupStore.removeListener(securityGroupMapListener);
116 osSecurityGroupRuleStore.removeListener(securityGroupRuleMapListener);
117 eventExecutor.shutdown();
118
119 log.info("Stopped");
120 }
121
122 @Override
123 public void createSecurityGroup(SecurityGroup sg) {
124 osSecurityGroupStore.compute(sg.getId(), (id, existing) -> {
125 final String error = sg.getName() + ERR_DUPLICATE;
126 checkArgument(existing == null, error);
127 return sg;
128 });
129 }
130
131 @Override
132 public SecurityGroup updateSecurityGroup(String sgId, SecurityGroup newSg) {
133 Versioned<SecurityGroup> sg = osSecurityGroupStore.replace(sgId, newSg);
134 return sg == null ? null : sg.value();
135 }
136
137 @Override
138 public SecurityGroup removeSecurityGroup(String sgId) {
139 Versioned<SecurityGroup> sg = osSecurityGroupStore.remove(sgId);
140 return sg == null ? null : sg.value();
141 }
142
143 @Override
144 public void createSecurityGroupRule(SecurityGroupRule sgRule) {
145 osSecurityGroupRuleStore.compute(sgRule.getId(), (id, existing) -> {
146 final String error = sgRule.getId() + ERR_DUPLICATE;
147 checkArgument(existing == null, error);
148 return sgRule;
149 });
150 }
151
152 @Override
153 public SecurityGroupRule removeSecurityGroupRule(String sgRuleId) {
154 Versioned<SecurityGroupRule> sgRule = osSecurityGroupRuleStore.remove(sgRuleId);
155 return sgRule == null ? null : sgRule.value();
156 }
157
158 @Override
159 public SecurityGroup securityGroup(String sgId) {
160 Versioned<SecurityGroup> osSg = osSecurityGroupStore.get(sgId);
161 return osSg == null ? null : osSg.value();
162 }
163
164 @Override
165 public SecurityGroupRule securityGroupRule(String sgRuleId) {
166 Versioned<SecurityGroupRule> osSgRule = osSecurityGroupRuleStore.get(sgRuleId);
167 return osSgRule == null ? null : osSgRule.value();
168 }
169
170 private class OpenstackSecurityGroupMapListener implements MapEventListener<String, SecurityGroup> {
171
172 @Override
173 public void event(MapEvent<String, SecurityGroup> event) {
174 switch (event.type()) {
175 case INSERT:
176 log.debug("Openstack Security Group created {}", event.newValue());
177 eventExecutor.execute(() ->
178 notifyDelegate(new OpenstackSecurityGroupEvent(
179 OpenstackSecurityGroupEvent.Type.OPENSTACK_SECURITY_GROUP_CREATED,
180 securityGroup(event.newValue().value().getId()))));
181 break;
182
183 case REMOVE:
184 log.debug("Openstack Security Group removed {}", event.newValue());
185 eventExecutor.execute(() ->
186 notifyDelegate(new OpenstackSecurityGroupEvent(
187 OpenstackSecurityGroupEvent.Type.OPENSTACK_SECURITY_GROUP_REMOVED,
188 event.oldValue().value())));
189 break;
190 default:
191 }
192 }
193 }
194
195 private class OpenstackSecurityGroupRuleMapListener implements MapEventListener<String, SecurityGroupRule> {
196
197 @Override
198 public void event(MapEvent<String, SecurityGroupRule> event) {
199 switch (event.type()) {
200 case INSERT:
201 log.debug("Openstack Security Group Rule created {}", event.newValue());
202 eventExecutor.execute(() ->
203 notifyDelegate(new OpenstackSecurityGroupEvent(
204 OpenstackSecurityGroupEvent.Type.OPENSTACK_SECURITY_GROUP_RULE_CREATED,
205 securityGroupRule(event.newValue().value().getId()))));
206 break;
207
208 case REMOVE:
209 log.debug("Openstack Security Group Rule removed {}", event.oldValue());
210 eventExecutor.execute(() ->
211 notifyDelegate(new OpenstackSecurityGroupEvent(
212 OpenstackSecurityGroupEvent.Type.OPENSTACK_SECURITY_GROUP_RULE_REMOVED,
213 event.oldValue().value())));
214 break;
215 default:
216 }
217 }
218 }
219}