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