Jian Li | 3defa84 | 2019-02-12 00:31:35 +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.k8snode.impl; |
| 17 | |
| 18 | import com.google.common.base.Strings; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onosproject.cluster.ClusterService; |
| 21 | import org.onosproject.cluster.LeadershipService; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.event.ListenerRegistry; |
| 25 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 26 | import org.onosproject.k8snode.api.K8sApiConfig.Scheme; |
| 27 | import org.onosproject.k8snode.api.K8sApiConfigAdminService; |
| 28 | import org.onosproject.k8snode.api.K8sApiConfigEvent; |
| 29 | import org.onosproject.k8snode.api.K8sApiConfigListener; |
| 30 | import org.onosproject.k8snode.api.K8sApiConfigService; |
| 31 | import org.onosproject.k8snode.api.K8sApiConfigStore; |
| 32 | import org.onosproject.k8snode.api.K8sApiConfigStoreDelegate; |
| 33 | import org.onosproject.store.service.StorageService; |
| 34 | import org.osgi.service.component.annotations.Activate; |
| 35 | import org.osgi.service.component.annotations.Component; |
| 36 | import org.osgi.service.component.annotations.Deactivate; |
| 37 | import org.osgi.service.component.annotations.Reference; |
| 38 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 39 | import org.slf4j.Logger; |
| 40 | |
| 41 | import java.util.Set; |
| 42 | import java.util.concurrent.ExecutorService; |
| 43 | |
| 44 | import static com.google.common.base.Preconditions.checkArgument; |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 47 | import static org.onlab.util.Tools.groupedThreads; |
| 48 | import static org.onosproject.k8snode.util.K8sNodeUtil.endpoint; |
| 49 | import static org.slf4j.LoggerFactory.getLogger; |
| 50 | |
| 51 | /** |
| 52 | * Service administering the inventory of kubernetes API configs. |
| 53 | */ |
| 54 | @Component( |
| 55 | immediate = true, |
| 56 | service = { K8sApiConfigService.class, K8sApiConfigAdminService.class } |
| 57 | ) |
| 58 | public class K8sApiConfigManager |
| 59 | extends ListenerRegistry<K8sApiConfigEvent, K8sApiConfigListener> |
| 60 | implements K8sApiConfigService, K8sApiConfigAdminService { |
| 61 | |
| 62 | private final Logger log = getLogger(getClass()); |
| 63 | |
| 64 | private static final String MSG_CONFIG = "Kubernetes API config %s %s"; |
| 65 | private static final String MSG_CREATED = "created"; |
| 66 | private static final String MSG_UPDATED = "updated"; |
| 67 | private static final String MSG_REMOVED = "removed"; |
| 68 | |
| 69 | private static final String ERR_NULL_CONFIG = "Kubernetes API config cannot be null"; |
| 70 | private static final String ERR_NULL_ENDPOINT = "Kubernetes API endpoint cannot be null"; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 73 | protected K8sApiConfigStore configStore; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 76 | protected CoreService coreService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 79 | protected ClusterService clusterService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 82 | protected LeadershipService leadershipService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 85 | protected StorageService storageService; |
| 86 | |
| 87 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 88 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 89 | |
| 90 | private final K8sApiConfigStoreDelegate delegate = new InternalApiConfigStoreDelegate(); |
| 91 | |
| 92 | private ApplicationId appId; |
| 93 | |
| 94 | @Activate |
| 95 | protected void activate() { |
| 96 | appId = coreService.registerApplication(APP_ID); |
| 97 | configStore.setDelegate(delegate); |
| 98 | |
| 99 | leadershipService.runForLeadership(appId.name()); |
| 100 | |
| 101 | log.info("Started"); |
| 102 | } |
| 103 | |
| 104 | @Deactivate |
| 105 | protected void deactivate() { |
| 106 | configStore.unsetDelegate(delegate); |
| 107 | |
| 108 | leadershipService.withdraw(appId.name()); |
| 109 | eventExecutor.shutdown(); |
| 110 | |
| 111 | log.info("Stopped"); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void createApiConfig(K8sApiConfig config) { |
| 116 | checkNotNull(config, ERR_NULL_CONFIG); |
| 117 | configStore.createApiConfig(config); |
| 118 | log.info(String.format(MSG_CONFIG, endpoint(config), MSG_CREATED)); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void updateApiConfig(K8sApiConfig config) { |
| 123 | checkNotNull(config, ERR_NULL_CONFIG); |
| 124 | configStore.updateApiConfig(config); |
| 125 | log.info(String.format(MSG_CONFIG, endpoint(config), MSG_UPDATED)); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public K8sApiConfig removeApiConfig(String endpoint) { |
| 130 | checkArgument(!Strings.isNullOrEmpty(endpoint), ERR_NULL_ENDPOINT); |
| 131 | K8sApiConfig config = configStore.removeApiConfig(endpoint); |
| 132 | log.info(String.format(MSG_CONFIG, endpoint, MSG_REMOVED)); |
| 133 | return config; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public K8sApiConfig removeApiConfig(Scheme scheme, |
| 138 | IpAddress ipAddress, int port) { |
| 139 | return removeApiConfig(endpoint(scheme, ipAddress, port)); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Set<K8sApiConfig> apiConfigs() { |
| 144 | return configStore.apiConfigs(); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public K8sApiConfig apiConfig(String endpoint) { |
| 149 | return configStore.apiConfig(endpoint); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public K8sApiConfig apiConfig(Scheme scheme, IpAddress ipAddress, int port) { |
| 154 | return apiConfig(endpoint(scheme, ipAddress, port)); |
| 155 | } |
| 156 | |
| 157 | private class InternalApiConfigStoreDelegate implements K8sApiConfigStoreDelegate { |
| 158 | |
| 159 | @Override |
| 160 | public void notify(K8sApiConfigEvent event) { |
| 161 | if (event != null) { |
| 162 | log.trace("send kubernetes API config event {}", event); |
| 163 | process(event); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |