blob: fd18c7e69092d72c10143a4006b3d671636a2c88 [file] [log] [blame]
Jian Li965de272019-02-19 15:35:55 +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.Service;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.core.CoreService;
23import org.onosproject.event.ListenerRegistry;
24import org.onosproject.k8snetworking.api.K8sServiceAdminService;
25import org.onosproject.k8snetworking.api.K8sServiceEvent;
26import org.onosproject.k8snetworking.api.K8sServiceListener;
27import org.onosproject.k8snetworking.api.K8sServiceService;
28import org.onosproject.k8snetworking.api.K8sServiceStore;
29import org.onosproject.k8snetworking.api.K8sServiceStoreDelegate;
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 service.
46 */
47@Component(
48 immediate = true,
49 service = {K8sServiceAdminService.class, K8sServiceService.class}
50)
51public class K8sServiceManager
52 extends ListenerRegistry<K8sServiceEvent, K8sServiceListener>
53 implements K8sServiceAdminService, K8sServiceService {
54
55 protected final Logger log = getLogger(getClass());
56
57 private static final String MSG_SERVICE = "Kubernetes service %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 ERR_NULL_SERVICE = "Kubernetes service cannot be null";
63 private static final String ERR_NULL_SERVICE_UID = "Kubernetes service UID cannot be null";
64
65 private static final String ERR_IN_USE = " still in use";
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY)
68 protected CoreService coreService;
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY)
71 protected K8sServiceStore k8sServiceStore;
72
73 private final K8sServiceStoreDelegate
74 delegate = new InternalServiceStorageDelegate();
75
76 private ApplicationId appId;
77
78 @Activate
79 protected void activate() {
80 appId = coreService.registerApplication(K8S_NETWORKING_APP_ID);
81
82 k8sServiceStore.setDelegate(delegate);
83 log.info("Started");
84 }
85
86 @Deactivate
87 protected void deactivate() {
88 k8sServiceStore.unsetDelegate(delegate);
89 log.info("Stopped");
90 }
91
92 @Override
93 public void createService(Service service) {
94 checkNotNull(service, ERR_NULL_SERVICE);
95 checkArgument(!Strings.isNullOrEmpty(service.getMetadata().getUid()),
96 ERR_NULL_SERVICE_UID);
97
98 k8sServiceStore.createService(service);
99
100 log.info(String.format(MSG_SERVICE, service.getMetadata().getName(), MSG_CREATED));
101 }
102
103 @Override
104 public void updateService(Service service) {
105 checkNotNull(service, ERR_NULL_SERVICE);
106 checkArgument(!Strings.isNullOrEmpty(service.getMetadata().getUid()),
107 ERR_NULL_SERVICE_UID);
108
109 k8sServiceStore.updateService(service);
110
111 log.info(String.format(MSG_SERVICE, service.getMetadata().getName(), MSG_UPDATED));
112 }
113
114 @Override
115 public void removeService(String uid) {
116 checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_SERVICE_UID);
117
118 synchronized (this) {
119 if (isServiceInUse(uid)) {
120 final String error = String.format(MSG_SERVICE, uid, ERR_IN_USE);
121 throw new IllegalStateException(error);
122 }
123
124 Service service = k8sServiceStore.removeService(uid);
125
126 if (service != null) {
127 log.info(String.format(MSG_SERVICE,
128 service.getMetadata().getName(), MSG_REMOVED));
129 }
130 }
131 }
132
133 @Override
134 public void clear() {
135 k8sServiceStore.clear();
136 }
137
138 @Override
139 public Service service(String uid) {
140 checkArgument(!Strings.isNullOrEmpty(uid), ERR_NULL_SERVICE_UID);
141 return k8sServiceStore.service(uid);
142 }
143
144 @Override
145 public Set<Service> services() {
146 return ImmutableSet.copyOf(k8sServiceStore.services());
147 }
148
149 private boolean isServiceInUse(String uid) {
150 return false;
151 }
152
153 private class InternalServiceStorageDelegate implements K8sServiceStoreDelegate {
154
155 @Override
156 public void notify(K8sServiceEvent event) {
157 if (event != null) {
158 log.trace("send kubernetes service event {}", event);
159 process(event);
160 }
161 }
162 }
163}