blob: 0562ee91ba73064efa92fedb5fba0eb2e2172ede [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.base.Strings;
19import com.google.common.collect.ImmutableSet;
20import org.onosproject.core.ApplicationId;
21import org.onosproject.core.CoreService;
22import org.onosproject.event.ListenerRegistry;
23import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfig;
24import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigAdminService;
25import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigEvent;
26import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigListener;
27import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigService;
28import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigStore;
29import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigStoreDelegate;
30import org.osgi.service.component.annotations.Activate;
31import org.osgi.service.component.annotations.Component;
32import org.osgi.service.component.annotations.Deactivate;
33import org.osgi.service.component.annotations.Reference;
34import org.osgi.service.component.annotations.ReferenceCardinality;
35import org.slf4j.Logger;
36
37import java.util.Set;
38
39import static com.google.common.base.Preconditions.checkArgument;
40import static com.google.common.base.Preconditions.checkNotNull;
41import static org.slf4j.LoggerFactory.getLogger;
42
43/**
44 * Provides implementation of administrating and interfacing kubernetes external lb config.
45 */
46@Component(
47 immediate = true,
48 service = {KubernetesExternalLbConfigAdminService.class, KubernetesExternalLbConfigService.class}
49)
50public class KubernetesExternalLbConfigManager
51 extends ListenerRegistry<KubernetesExternalLbConfigEvent, KubernetesExternalLbConfigListener>
52 implements KubernetesExternalLbConfigAdminService, KubernetesExternalLbConfigService {
53 protected final Logger log = getLogger(getClass());
54
55 private static final String MSG_LOAD_BALANCER_CONFIG = "Kubernetes external lb config %s %s";
56 private static final String MSG_CREATED = "created";
57 private static final String MSG_UPDATED = "updated";
58 private static final String MSG_REMOVED = "removed";
59
60 private static final String ERR_NULL_LOAD_BALANCER_CONFIG = "Kubernetes external lb config cannot be null";
61 private static final String ERR_NULL_LOAD_BALANCER_CONFIG_NAME
62 = "Kubernetes external lb config name cannot be null";
63 private static final String ERR_IN_USE = " still in use";
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY)
66 protected CoreService coreService;
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY)
69 protected KubernetesExternalLbConfigStore lbConfigStore;
70
71 private final InternalKubernetesExternalLbConfigStorageDelegate delegate =
72 new InternalKubernetesExternalLbConfigStorageDelegate();
73
74 private ApplicationId appId;
75
76
77 @Activate
78 protected void activate() {
79 appId = coreService.registerApplication(APP_ID);
80
81 lbConfigStore.setDelegate(delegate);
82 log.info("Started");
83 }
84
85 @Deactivate
86 protected void deactivate() {
87 lbConfigStore.unsetDelegate(delegate);
88 log.info("Stopped");
89 }
90
91
92 @Override
93 public void createKubernetesExternalLbConfig(KubernetesExternalLbConfig lbConfig) {
94 checkNotNull(lbConfig, ERR_NULL_LOAD_BALANCER_CONFIG);
95 checkArgument(!Strings.isNullOrEmpty(lbConfig.configName()), ERR_NULL_LOAD_BALANCER_CONFIG_NAME);
96
97 lbConfigStore.createExternalLbConfig(lbConfig);
98 log.info(String.format(MSG_LOAD_BALANCER_CONFIG, lbConfig.configName(), MSG_CREATED));
99 }
100
101 @Override
102 public void updateKubernetesExternalLbConfig(KubernetesExternalLbConfig lbConfig) {
103 checkNotNull(lbConfig, ERR_NULL_LOAD_BALANCER_CONFIG);
104 checkArgument(!Strings.isNullOrEmpty(lbConfig.configName()), ERR_NULL_LOAD_BALANCER_CONFIG_NAME);
105
106 lbConfigStore.updateExternalLbConfig(lbConfig);
107 log.info(String.format(MSG_LOAD_BALANCER_CONFIG, lbConfig.configName(), MSG_UPDATED));
108 }
109
110 @Override
111 public void removeKubernetesExternalLbConfig(String configName) {
112
113 checkArgument(configName != null, ERR_NULL_LOAD_BALANCER_CONFIG_NAME);
114
115 synchronized (this) {
116 KubernetesExternalLbConfig lbConfig = lbConfigStore.removeExternalLbConfig(configName);
117
118 if (lbConfig != null) {
119 log.info(String.format(MSG_LOAD_BALANCER_CONFIG, lbConfig.configName(), MSG_REMOVED));
120 }
121 }
122 }
123
124 @Override
125 public KubernetesExternalLbConfig lbConfig(String configName) {
126 checkArgument(configName != null, ERR_NULL_LOAD_BALANCER_CONFIG_NAME);
127
128 return lbConfigStore.externalLbConfig(configName);
129 }
130
131 @Override
132 public Set<KubernetesExternalLbConfig> lbConfigs() {
133 return ImmutableSet.copyOf(lbConfigStore.externalLbConfigs());
134 }
135
136 private class InternalKubernetesExternalLbConfigStorageDelegate
137 implements KubernetesExternalLbConfigStoreDelegate {
138
139 @Override
140 public void notify(KubernetesExternalLbConfigEvent event) {
141 log.trace("send kubernetes external lb config event {}", event);
142 process(event);
143 }
144 }
145}