blob: 96bbb16f3374c23d19664126296e7878084e3883 [file] [log] [blame]
Jian Li47e7af72021-03-05 01:32:04 +09001/*
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 */
16package org.onosproject.kubevirtnetworking.impl;
17
18import com.google.common.collect.ImmutableSet;
19import org.onlab.util.KryoNamespace;
20import org.onosproject.core.ApplicationId;
21import org.onosproject.core.CoreService;
22import org.onosproject.kubevirtnetworking.api.DefaultKubevirtSecurityGroup;
23import org.onosproject.kubevirtnetworking.api.DefaultKubevirtSecurityGroupRule;
24import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroup;
25import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupEvent;
26import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupRule;
27import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupStore;
28import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupStoreDelegate;
29import org.onosproject.store.AbstractStore;
30import org.onosproject.store.serializers.KryoNamespaces;
31import org.onosproject.store.service.ConsistentMap;
32import org.onosproject.store.service.MapEvent;
33import org.onosproject.store.service.MapEventListener;
34import org.onosproject.store.service.Serializer;
35import org.onosproject.store.service.StorageService;
36import org.onosproject.store.service.Versioned;
37import org.osgi.service.component.annotations.Activate;
38import org.osgi.service.component.annotations.Component;
39import org.osgi.service.component.annotations.Deactivate;
40import org.osgi.service.component.annotations.Reference;
41import org.osgi.service.component.annotations.ReferenceCardinality;
42import org.slf4j.Logger;
43
44import java.util.Collection;
45import java.util.Set;
46import java.util.concurrent.ExecutorService;
47import java.util.stream.Collectors;
48
49import static com.google.common.base.Preconditions.checkArgument;
50import static java.util.concurrent.Executors.newSingleThreadExecutor;
51import static org.onlab.util.Tools.groupedThreads;
52import static org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupEvent.Type.KUBEVIRT_SECURITY_GROUP_CREATED;
53import static org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupEvent.Type.KUBEVIRT_SECURITY_GROUP_REMOVED;
54import static org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupEvent.Type.KUBEVIRT_SECURITY_GROUP_RULE_CREATED;
55import static org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupEvent.Type.KUBEVIRT_SECURITY_GROUP_RULE_REMOVED;
56import 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)
62public 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}