blob: 3a1e10c1421acbf2a822c9c13a35651d4519bcf2 [file] [log] [blame]
Jian Li324d6dc2019-07-10 10:55:15 +09001/*
2 * Copyright 2019-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.k8snetworking.impl;
17
18import com.google.common.base.Strings;
19import com.google.common.collect.ImmutableSet;
20import io.fabric8.kubernetes.api.model.Namespace;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.core.CoreService;
23import org.onosproject.event.ListenerRegistry;
24import org.onosproject.k8snetworking.api.K8sNamespaceAdminService;
25import org.onosproject.k8snetworking.api.K8sNamespaceEvent;
26import org.onosproject.k8snetworking.api.K8sNamespaceListener;
27import org.onosproject.k8snetworking.api.K8sNamespaceService;
28import org.onosproject.k8snetworking.api.K8sNamespaceStore;
29import org.onosproject.k8snetworking.api.K8sNamespaceStoreDelegate;
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.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
42import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * Provides implementation of administering and interfacing kubernetes namespace.
46 */
47@Component(
48 immediate = true,
49 service = {K8sNamespaceAdminService.class, K8sNamespaceService.class}
50)
51public class K8sNamespaceManager
52 extends ListenerRegistry<K8sNamespaceEvent, K8sNamespaceListener>
53 implements K8sNamespaceAdminService, K8sNamespaceService {
54
55 protected final Logger log = getLogger(getClass());
56
57 private static final String MSG_NAMESPACE = "Kubernetes namespace %s %s";
58 private static final String MSG_CREATED = "created";
59 private static final String MSG_UPDATED = "updated";
60 private static final String MSG_REMOVED = "removed";
61
62 private static final String
63 ERR_NULL_NAMESPACE = "Kubernetes namespace cannot be null";
64 private static final String
65 ERR_NULL_NAMESPACE_UID = "Kubernetes namespace UID cannot be null";
66
67 private static final String ERR_IN_USE = " still in use";
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY)
70 protected CoreService coreService;
71
72 @Reference(cardinality = ReferenceCardinality.MANDATORY)
73 protected K8sNamespaceStore k8sNamespaceStore;
74
75 private final K8sNamespaceStoreDelegate delegate =
76 new InternalNamespaceStorageDelegate();
77
78 private ApplicationId appId;
79
80 @Activate
81 protected void activate() {
82 appId = coreService.registerApplication(K8S_NETWORKING_APP_ID);
83
84 k8sNamespaceStore.setDelegate(delegate);
85 log.info("Started");
86 }
87
88 @Deactivate
89 protected void deactivate() {
90 k8sNamespaceStore.unsetDelegate(delegate);
91 log.info("Stopped");
92 }
93
94 @Override
95 public void createNamespace(Namespace namespace) {
96 checkNotNull(namespace, ERR_NULL_NAMESPACE);
97 checkArgument(!Strings.isNullOrEmpty(namespace.getMetadata().getUid()),
98 ERR_NULL_NAMESPACE_UID);
99
100 k8sNamespaceStore.createNamespace(namespace);
101
102 log.info(String.format(MSG_NAMESPACE,
103 namespace.getMetadata().getName(), MSG_CREATED));
104 }
105
106 @Override
107 public void updateNamespace(Namespace namespace) {
108 checkNotNull(namespace, ERR_NULL_NAMESPACE);
109 checkArgument(!Strings.isNullOrEmpty(namespace.getMetadata().getUid()),
110 ERR_NULL_NAMESPACE_UID);
111
112 k8sNamespaceStore.updateNamespace(namespace);
113
114 log.info(String.format(MSG_NAMESPACE,
115 namespace.getMetadata().getName(), MSG_UPDATED));
116 }
117
118 @Override
119 public void removeNamespace(String uid) {
120 checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_NAMESPACE_UID);
121
122 synchronized (this) {
123 if (isNamespaceInUse(uid)) {
124 final String error = String.format(MSG_NAMESPACE, uid, ERR_IN_USE);
125 throw new IllegalStateException(error);
126 }
127 }
128
129 Namespace namespace = k8sNamespaceStore.removeNamespace(uid);
130
131 if (namespace != null) {
132 log.info(String.format(MSG_NAMESPACE,
133 namespace.getMetadata().getName(), MSG_REMOVED));
134 }
135 }
136
137 @Override
138 public void clear() {
139 k8sNamespaceStore.clear();
140 }
141
142 @Override
143 public Namespace namespace(String uid) {
144 checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_NAMESPACE_UID);
145 return k8sNamespaceStore.namespace(uid);
146 }
147
148 @Override
149 public Set<Namespace> namespaces() {
150 return ImmutableSet.copyOf(k8sNamespaceStore.namespaces());
151 }
152
153 private boolean isNamespaceInUse(String uid) {
154 return false;
155 }
156
157 private class InternalNamespaceStorageDelegate
158 implements K8sNamespaceStoreDelegate {
159
160 @Override
161 public void notify(K8sNamespaceEvent event) {
162 if (event != null) {
163 log.trace("send kubernetes namespace event {}", event);
164 process(event);
165 }
166 }
167 }
168}