Jian Li | 965de27 | 2019-02-19 15:35:55 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.k8snetworking.impl; |
| 17 | |
| 18 | import com.google.common.base.Strings; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import io.fabric8.kubernetes.api.model.Service; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.event.ListenerRegistry; |
| 24 | import org.onosproject.k8snetworking.api.K8sServiceAdminService; |
| 25 | import org.onosproject.k8snetworking.api.K8sServiceEvent; |
| 26 | import org.onosproject.k8snetworking.api.K8sServiceListener; |
| 27 | import org.onosproject.k8snetworking.api.K8sServiceService; |
| 28 | import org.onosproject.k8snetworking.api.K8sServiceStore; |
| 29 | import org.onosproject.k8snetworking.api.K8sServiceStoreDelegate; |
| 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import java.util.Set; |
| 38 | |
| 39 | import static com.google.common.base.Preconditions.checkArgument; |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID; |
| 42 | import 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 | ) |
| 51 | public 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 | } |