Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.kubevirtnode.impl; |
| 17 | |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
| 19 | import org.onosproject.cluster.ClusterService; |
| 20 | import org.onosproject.cluster.LeadershipService; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.event.ListenerRegistry; |
| 24 | import org.onosproject.kubevirtnode.api.KubevirtApiConfig; |
| 25 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigAdminService; |
| 26 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent; |
| 27 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigListener; |
| 28 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigService; |
| 29 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigStore; |
| 30 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigStoreDelegate; |
| 31 | import org.onosproject.store.service.StorageService; |
| 32 | import org.osgi.service.component.annotations.Activate; |
| 33 | import org.osgi.service.component.annotations.Component; |
| 34 | import org.osgi.service.component.annotations.Deactivate; |
| 35 | import org.osgi.service.component.annotations.Reference; |
| 36 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | import java.util.concurrent.ExecutorService; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkArgument; |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
| 43 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 44 | import static org.onlab.util.Tools.groupedThreads; |
| 45 | import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.endpoint; |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
Jian Li | aaf44b5 | 2020-12-27 23:22:46 +0900 | [diff] [blame^] | 48 | /** |
| 49 | * Service administering the inventory of KubeVirt API configs. |
| 50 | */ |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 51 | @Component( |
| 52 | immediate = true, |
| 53 | service = {KubevirtApiConfigService.class, KubevirtApiConfigAdminService.class } |
| 54 | ) |
| 55 | public class KubevirtApiConfigManager |
| 56 | extends ListenerRegistry<KubevirtApiConfigEvent, KubevirtApiConfigListener> |
| 57 | implements KubevirtApiConfigService, KubevirtApiConfigAdminService { |
| 58 | |
| 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
| 61 | private static final String MSG_CONFIG = "KubeVirt API config %s %s"; |
| 62 | private static final String MSG_CREATED = "created"; |
| 63 | private static final String MSG_UPDATED = "updated"; |
| 64 | private static final String MSG_REMOVED = "removed"; |
| 65 | |
| 66 | private static final String ERR_NULL_CONFIG = "KubeVirt API config cannot be null"; |
| 67 | private static final String ERR_NULL_ENDPOINT = "KubeVirt API endpoint cannot be null"; |
| 68 | private static final String ERR_UNIQUE_CONFIG = "KubeVirt API config should be unique"; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 71 | protected CoreService coreService; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 74 | protected ClusterService clusterService; |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 77 | protected LeadershipService leadershipService; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 80 | protected StorageService storageService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 83 | protected KubevirtApiConfigStore configStore; |
| 84 | |
| 85 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 86 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 87 | |
| 88 | private final KubevirtApiConfigStoreDelegate delegate = new InternalApiConfigStoreDelegate(); |
| 89 | |
| 90 | private ApplicationId appId; |
| 91 | |
| 92 | @Activate |
| 93 | protected void activate() { |
| 94 | appId = coreService.registerApplication(APP_ID); |
| 95 | configStore.setDelegate(delegate); |
| 96 | |
| 97 | leadershipService.runForLeadership(appId.name()); |
| 98 | |
| 99 | log.info("Started"); |
| 100 | } |
| 101 | |
| 102 | @Deactivate |
| 103 | protected void deactivate() { |
| 104 | configStore.unsetDelegate(delegate); |
| 105 | |
| 106 | leadershipService.withdraw(appId.name()); |
| 107 | eventExecutor.shutdown(); |
| 108 | |
| 109 | log.info("Stopped"); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void createApiConfig(KubevirtApiConfig config) { |
| 114 | checkNotNull(config, ERR_NULL_CONFIG); |
| 115 | checkArgument(configStore.apiConfigs().size() == 0, ERR_UNIQUE_CONFIG); |
| 116 | |
| 117 | configStore.createApiConfig(config); |
| 118 | log.info(String.format(MSG_CONFIG, endpoint(config), MSG_CREATED)); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void updateApiConfig(KubevirtApiConfig 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 KubevirtApiConfig removeApiConfig(String endpoint) { |
| 130 | checkArgument(!Strings.isNullOrEmpty(endpoint), ERR_NULL_ENDPOINT); |
| 131 | KubevirtApiConfig config = configStore.removeApiConfig(endpoint); |
| 132 | log.info(String.format(MSG_CONFIG, endpoint, MSG_REMOVED)); |
| 133 | return config; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public KubevirtApiConfig apiConfig() { |
| 138 | return configStore.apiConfigs().stream().findAny().orElse(null); |
| 139 | } |
| 140 | |
| 141 | private class InternalApiConfigStoreDelegate implements KubevirtApiConfigStoreDelegate { |
| 142 | |
| 143 | @Override |
| 144 | public void notify(KubevirtApiConfigEvent event) { |
| 145 | if (event != null) { |
| 146 | log.trace("send KubeVirt API config event {}", event); |
| 147 | process(event); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |