blob: 16a816e5f1d91f46f6105e9405c6d89e32552de7 [file] [log] [blame]
Daniel Park5ff76b72022-09-26 22:58:53 +09001/*
2 * Copyright 2022-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.kubevirtnode.impl;
17
18import com.google.common.collect.ImmutableSet;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onlab.util.KryoNamespace;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
24import org.onosproject.kubevirtnode.api.DefaultKubernetesExternalLbConfig;
25import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfig;
26import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent;
27import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigStore;
28import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigStoreDelegate;
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;
47
48import static com.google.common.base.Preconditions.checkArgument;
49import static java.util.concurrent.Executors.newSingleThreadExecutor;
50import static org.onlab.util.Tools.groupedThreads;
51import static org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent.Type
52 .KUBERNETES_EXTERNAL_LB_CONFIG_CREATED;
53import static org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent.Type
54 .KUBERNETES_EXTERNAL_LB_CONFIG_REMOVED;
55import static org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent.Type
56 .KUBERNETES_EXTERNAL_LB_CONFIG_UPDATED;
57import static org.slf4j.LoggerFactory.getLogger;
58
59/**
60 * Implementation of kubernetes external lb config store using consistent map.
61 */
62@Component(immediate = true, service = KubernetesExternalLbConfigStore.class)
63public class DistributedKubernetesExternalLbConfigStore
64 extends AbstractStore<KubernetesExternalLbConfigEvent, KubernetesExternalLbConfigStoreDelegate>
65 implements KubernetesExternalLbConfigStore {
66
67 private 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 private static final String APP_ID = "org.onosproject.kubevirtnode";
72
73 private static final KryoNamespace
74 SERIALIZER_KUBERNETES_EXTERNAL_LB_CONFIG = KryoNamespace.newBuilder()
75 .register(KryoNamespaces.API)
76 .register(KubernetesExternalLbConfig.class)
77 .register(DefaultKubernetesExternalLbConfig.class)
78 .register(IpAddress.class)
79 .register(MacAddress.class)
80 .register(Collection.class)
81 .build();
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY)
84 protected CoreService coreService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY)
87 protected StorageService storageService;
88
89 private final ExecutorService eventExecutor = newSingleThreadExecutor(
90 groupedThreads(this.getClass().getSimpleName(), "event-handler", log));
91
92 private final MapEventListener<String, KubernetesExternalLbConfig> lbConfigMapEventListener =
93 new KubernetesExternalLbConfigMapListener();
94
95 private ConsistentMap<String, KubernetesExternalLbConfig> lbConfigStore;
96
97 @Activate
98 protected void activate() {
99 ApplicationId appId = coreService.registerApplication(APP_ID);
100 lbConfigStore = storageService.<String, KubernetesExternalLbConfig>consistentMapBuilder()
101 .withSerializer(Serializer.using(SERIALIZER_KUBERNETES_EXTERNAL_LB_CONFIG))
102 .withName("kubernetes-lbconfigstore")
103 .withApplicationId(appId)
104 .build();
105
106 lbConfigStore.addListener(lbConfigMapEventListener);
107 log.info("Started");
108 }
109
110 @Deactivate
111 protected void deactivate() {
112 lbConfigStore.removeListener(lbConfigMapEventListener);
113 eventExecutor.shutdown();
114 log.info("Stopped");
115 }
116
117 @Override
118 public void createExternalLbConfig(KubernetesExternalLbConfig lbConfig) {
119 lbConfigStore.compute(lbConfig.configName(), (configName, existing) -> {
120 final String error = lbConfig.configName() + ERR_DUPLICATE;
121 checkArgument(existing == null, error);
122 return lbConfig;
123 });
124 }
125
126 @Override
127 public void updateExternalLbConfig(KubernetesExternalLbConfig lbConfig) {
128 lbConfigStore.compute(lbConfig.configName(), (configName, existing) -> {
129 final String error = lbConfig.configName() + ERR_NOT_FOUND;
130 checkArgument(existing != null, error);
131 return lbConfig;
132 });
133 }
134
135 @Override
136 public KubernetesExternalLbConfig removeExternalLbConfig(String configName) {
137
138 Versioned<KubernetesExternalLbConfig> lbConfig = lbConfigStore.remove(configName);
139
140 if (lbConfig == null) {
141 final String error = configName + ERR_NOT_FOUND;
142 throw new IllegalArgumentException(error);
143 }
144
145 return lbConfig.value();
146 }
147
148 @Override
149 public KubernetesExternalLbConfig externalLbConfig(String configName) {
150 return lbConfigStore.asJavaMap().get(configName);
151 }
152
153 @Override
154 public Set<KubernetesExternalLbConfig> externalLbConfigs() {
155
156 return ImmutableSet.copyOf(lbConfigStore.asJavaMap().values());
157 }
158
159 @Override
160 public void clear() {
161 lbConfigStore.clear();
162 }
163
164 private class KubernetesExternalLbConfigMapListener
165 implements MapEventListener<String, KubernetesExternalLbConfig> {
166
167 @Override
168 public void event(MapEvent<String, KubernetesExternalLbConfig> event) {
169 switch (event.type()) {
170 case INSERT:
171 eventExecutor.execute(() ->
172 notifyDelegate(new KubernetesExternalLbConfigEvent(
173 KUBERNETES_EXTERNAL_LB_CONFIG_CREATED, event.newValue().value())));
174 break;
175 case UPDATE:
176 eventExecutor.execute(() ->
177 notifyDelegate(new KubernetesExternalLbConfigEvent(
178 KUBERNETES_EXTERNAL_LB_CONFIG_UPDATED, event.newValue().value())));
179 break;
180 case REMOVE:
181 eventExecutor.execute(() ->
182 notifyDelegate(new KubernetesExternalLbConfigEvent(
183 KUBERNETES_EXTERNAL_LB_CONFIG_REMOVED, event.oldValue().value())));
184 break;
185 default:
186 //do nothing
187 break;
188 }
189 }
190 }
191}