blob: 2a6890e00da8e3cc982362c30bd2dd499353b618 [file] [log] [blame]
Jian Li7e8f57e2019-01-24 18:31:03 +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 org.onosproject.core.ApplicationId;
21import org.onosproject.core.CoreService;
22import org.onosproject.event.ListenerRegistry;
23import org.onosproject.k8snetworking.api.K8sNetwork;
24import org.onosproject.k8snetworking.api.K8sNetwork.Type;
25import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
26import org.onosproject.k8snetworking.api.K8sNetworkEvent;
27import org.onosproject.k8snetworking.api.K8sNetworkListener;
28import org.onosproject.k8snetworking.api.K8sNetworkService;
29import org.onosproject.k8snetworking.api.K8sNetworkStore;
30import org.onosproject.k8snetworking.api.K8sNetworkStoreDelegate;
31import org.onosproject.k8snetworking.api.K8sPort;
32import org.onosproject.k8snetworking.api.K8sPort.State;
33import org.osgi.service.component.annotations.Activate;
34import org.osgi.service.component.annotations.Component;
35import org.osgi.service.component.annotations.Deactivate;
36import org.osgi.service.component.annotations.Reference;
37import org.osgi.service.component.annotations.ReferenceCardinality;
38import org.slf4j.Logger;
39
40import java.util.Set;
41import java.util.stream.Collectors;
42
43import static com.google.common.base.Preconditions.checkArgument;
44import static com.google.common.base.Preconditions.checkNotNull;
45import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
46import static org.slf4j.LoggerFactory.getLogger;
47
48/**
49 * Provides implementation of administering and interfacing kubernetes network,
50 * and port.
51 */
52@Component(
53 immediate = true,
54 service = {K8sNetworkAdminService.class, K8sNetworkService.class }
55)
56public class K8sNetworkManager
57 extends ListenerRegistry<K8sNetworkEvent, K8sNetworkListener>
58 implements K8sNetworkAdminService, K8sNetworkService {
59
60 protected final Logger log = getLogger(getClass());
61
62 private static final String MSG_NETWORK = "Kubernetes network %s %s";
63 private static final String MSG_PORT = "Kubernetes port %s %s";
64 private static final String MSG_CREATED = "created";
65 private static final String MSG_UPDATED = "updated";
66 private static final String MSG_REMOVED = "removed";
67
68 private static final String ERR_NULL_NETWORK = "Kubernetes network cannot be null";
69 private static final String ERR_NULL_NETWORK_ID = "Kubernetes network ID cannot be null";
70 private static final String ERR_NULL_PORT = "Kubernetes port cannot be null";
71 private static final String ERR_NULL_PORT_ID = "Kubernetes port ID cannot be null";
72 private static final String ERR_NULL_PORT_NET_ID = "Kubernetes port network ID cannot be null";
73
74 private static final String ERR_IN_USE = " still in use";
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY)
77 protected CoreService coreService;
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY)
80 protected K8sNetworkStore k8sNetworkStore;
81
82 private final K8sNetworkStoreDelegate
83 delegate = new InternalNetworkStorageDelegate();
84
85 private ApplicationId appId;
86
87 @Activate
88 protected void activate() {
89 appId = coreService.registerApplication(K8S_NETWORKING_APP_ID);
90
91 k8sNetworkStore.setDelegate(delegate);
92 log.info("Started");
93 }
94
95 @Deactivate
96 protected void deactivate() {
97 k8sNetworkStore.unsetDelegate(delegate);
98 log.info("Stopped");
99 }
100
101 @Override
102 public void createNetwork(K8sNetwork network) {
103 checkNotNull(network, ERR_NULL_NETWORK);
104 checkArgument(!Strings.isNullOrEmpty(network.networkId()), ERR_NULL_NETWORK_ID);
105
106 k8sNetworkStore.createNetwork(network);
107
108 log.info(String.format(MSG_NETWORK, network.name(), MSG_CREATED));
109 }
110
111 @Override
112 public void updateNetwork(K8sNetwork network) {
113 checkNotNull(network, ERR_NULL_NETWORK);
114 checkArgument(!Strings.isNullOrEmpty(network.networkId()), ERR_NULL_NETWORK_ID);
115
116 k8sNetworkStore.updateNetwork(network);
117
118 log.info(String.format(MSG_NETWORK, network.networkId(), MSG_UPDATED));
119 }
120
121 @Override
122 public void removeNetwork(String networkId) {
123 checkArgument(!Strings.isNullOrEmpty(networkId), ERR_NULL_NETWORK_ID);
124
125 synchronized (this) {
126 if (isNetworkInUse(networkId)) {
127 final String error = String.format(MSG_NETWORK, networkId, ERR_IN_USE);
128 throw new IllegalStateException(error);
129 }
130 K8sNetwork network = k8sNetworkStore.removeNetwork(networkId);
131
132 if (network != null) {
133 log.info(String.format(MSG_NETWORK, network.name(), MSG_REMOVED));
134 }
135 }
136 }
137
138 @Override
139 public K8sNetwork network(String networkId) {
140 checkArgument(!Strings.isNullOrEmpty(networkId), ERR_NULL_NETWORK_ID);
141 return k8sNetworkStore.network(networkId);
142 }
143
144 @Override
145 public Set<K8sNetwork> networks() {
146 return ImmutableSet.copyOf(k8sNetworkStore.networks());
147 }
148
149 @Override
150 public Set<K8sNetwork> networks(Type type) {
151 return ImmutableSet.copyOf(k8sNetworkStore.networks().stream()
152 .filter(n -> n.type() == type).collect(Collectors.toSet()));
153 }
154
155 @Override
156 public void createPort(K8sPort port) {
157 checkNotNull(port, ERR_NULL_PORT);
158 checkArgument(!Strings.isNullOrEmpty(port.portId()), ERR_NULL_PORT_ID);
159 checkArgument(!Strings.isNullOrEmpty(port.networkId()), ERR_NULL_PORT_NET_ID);
160
161 k8sNetworkStore.createPort(port);
162 log.info(String.format(MSG_PORT, port.portId(), MSG_CREATED));
163 }
164
165 @Override
166 public void updatePort(K8sPort port) {
167 checkNotNull(port, ERR_NULL_PORT);
168 checkArgument(!Strings.isNullOrEmpty(port.portId()), ERR_NULL_PORT_ID);
169 checkArgument(!Strings.isNullOrEmpty(port.networkId()), ERR_NULL_PORT_NET_ID);
170
171 k8sNetworkStore.updatePort(port);
172 log.info(String.format(MSG_PORT, port.portId(), MSG_UPDATED));
173 }
174
175 @Override
176 public void removePort(String portId) {
177 checkArgument(!Strings.isNullOrEmpty(portId), ERR_NULL_PORT_ID);
178 synchronized (this) {
179 if (isPortInUse(portId)) {
180 final String error = String.format(MSG_PORT, portId, ERR_IN_USE);
181 throw new IllegalStateException(error);
182 }
183 K8sPort port = k8sNetworkStore.removePort(portId);
184 if (port != null) {
185 log.info(String.format(MSG_PORT, port.portId(), MSG_REMOVED));
186 }
187 }
188 }
189
190 @Override
191 public K8sPort port(String portId) {
192 checkArgument(!Strings.isNullOrEmpty(portId), ERR_NULL_PORT_ID);
193 return k8sNetworkStore.port(portId);
194 }
195
196 @Override
197 public Set<K8sPort> ports() {
198 return ImmutableSet.copyOf(k8sNetworkStore.ports());
199 }
200
201 @Override
202 public Set<K8sPort> ports(State state) {
203 return ImmutableSet.copyOf(k8sNetworkStore.ports().stream()
204 .filter(p -> p.state() == state).collect(Collectors.toSet()));
205 }
206
207 @Override
208 public Set<K8sPort> ports(String networkId) {
209 checkArgument(!Strings.isNullOrEmpty(networkId), ERR_NULL_PORT_NET_ID);
210 return ImmutableSet.copyOf(k8sNetworkStore.ports().stream()
211 .filter(p -> p.networkId().equals(networkId))
212 .collect(Collectors.toSet()));
213 }
214
215 @Override
216 public void clear() {
217 k8sNetworkStore.clear();
218 }
219
220 private boolean isNetworkInUse(String networkId) {
221 return !ports(networkId).isEmpty();
222 }
223
224 private boolean isPortInUse(String portId) {
225 return false;
226 }
227
228 private class InternalNetworkStorageDelegate implements K8sNetworkStoreDelegate {
229
230 @Override
231 public void notify(K8sNetworkEvent event) {
232 if (event != null) {
233 log.trace("send kubernetes networking event {}", event);
234 process(event);
235 }
236 }
237 }
238}