Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.openstacktelemetry.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.onlab.util.KryoNamespace; |
| 20 | import org.onosproject.cluster.ClusterService; |
| 21 | import org.onosproject.cluster.LeadershipService; |
| 22 | import org.onosproject.cluster.NodeId; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.event.ListenerRegistry; |
Jian Li | 7fe7eaf | 2018-12-31 17:00:33 +0900 | [diff] [blame^] | 26 | import org.onosproject.openstacktelemetry.api.DefaultTelemetryConfig; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 27 | import org.onosproject.openstacktelemetry.api.TelemetryConfigAdminService; |
| 28 | import org.onosproject.openstacktelemetry.api.TelemetryConfigEvent; |
| 29 | import org.onosproject.openstacktelemetry.api.TelemetryConfigListener; |
| 30 | import org.onosproject.openstacktelemetry.api.TelemetryConfigProvider; |
| 31 | import org.onosproject.openstacktelemetry.api.TelemetryConfigService; |
| 32 | import org.onosproject.openstacktelemetry.api.TelemetryConfigStore; |
| 33 | import org.onosproject.openstacktelemetry.api.TelemetryConfigStoreDelegate; |
| 34 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 35 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig.ConfigType; |
| 36 | import org.onosproject.store.serializers.KryoNamespaces; |
| 37 | import org.onosproject.store.service.ConsistentMap; |
| 38 | import org.onosproject.store.service.Serializer; |
| 39 | import org.onosproject.store.service.StorageService; |
| 40 | import org.osgi.service.component.annotations.Activate; |
| 41 | import org.osgi.service.component.annotations.Component; |
| 42 | import org.osgi.service.component.annotations.Deactivate; |
| 43 | import org.osgi.service.component.annotations.Reference; |
| 44 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
| 48 | import java.util.Objects; |
| 49 | import java.util.Set; |
| 50 | |
| 51 | import static com.google.common.base.Preconditions.checkNotNull; |
| 52 | import static org.onlab.util.Tools.nullIsNotFound; |
| 53 | import static org.onosproject.openstacktelemetry.api.Constants.OPENSTACK_TELEMETRY_APP_ID; |
| 54 | |
| 55 | /** |
| 56 | * Provides implementation of administering and interfacing telemetry configs. |
| 57 | * It also provides telemetry config events for the various exporters or connectors. |
| 58 | */ |
| 59 | @Component( |
| 60 | immediate = true, |
| 61 | service = { |
| 62 | TelemetryConfigService.class, TelemetryConfigAdminService.class |
| 63 | } |
| 64 | ) |
| 65 | public class TelemetryConfigManager |
| 66 | extends ListenerRegistry<TelemetryConfigEvent, TelemetryConfigListener> |
| 67 | implements TelemetryConfigService, TelemetryConfigAdminService { |
| 68 | |
| 69 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 70 | |
| 71 | private static final String MSG_TELEMETRY_CONFIG = "Telemetry config %s %s"; |
| 72 | private static final String MSG_CREATED = "created"; |
| 73 | private static final String MSG_UPDATED = "updated"; |
| 74 | private static final String MSG_REMOVED = "removed"; |
| 75 | |
| 76 | private static final String ERR_NULL_CONFIG = "Telemetry config cannot be null"; |
| 77 | private static final String NO_CONFIG = "Telemetry config not found"; |
Jian Li | 7fe7eaf | 2018-12-31 17:00:33 +0900 | [diff] [blame^] | 78 | private static final String ERR_NULL_CONFIG_NAME = "Telemetry config name cannot be null"; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 79 | |
| 80 | private static final KryoNamespace SERIALIZER_TELEMETRY_CONFIG = |
| 81 | KryoNamespace.newBuilder() |
| 82 | .register(KryoNamespaces.API) |
| 83 | .register(TelemetryConfigProvider.class) |
| 84 | .register(DefaultTelemetryConfigProvider.class) |
| 85 | .register(TelemetryConfig.class) |
| 86 | .register(TelemetryConfig.ConfigType.class) |
| 87 | .register(DefaultTelemetryConfig.class) |
| 88 | .build(); |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 91 | protected CoreService coreService; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 94 | protected StorageService storageService; |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 97 | protected ClusterService clusterService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 100 | protected LeadershipService leadershipService; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 103 | protected TelemetryConfigStore telemetryConfigStore; |
| 104 | |
| 105 | private final TelemetryConfigStoreDelegate |
| 106 | delegate = new InternalTelemetryConfigStoreDelegate(); |
| 107 | |
| 108 | private ConsistentMap<String, TelemetryConfigProvider> providerMap; |
| 109 | |
| 110 | private ApplicationId appId; |
| 111 | private NodeId localNodeId; |
| 112 | |
| 113 | @Activate |
| 114 | protected void activate() { |
| 115 | appId = coreService.registerApplication(OPENSTACK_TELEMETRY_APP_ID); |
| 116 | localNodeId = clusterService.getLocalNode().id(); |
| 117 | telemetryConfigStore.setDelegate(delegate); |
| 118 | leadershipService.runForLeadership(appId.name()); |
| 119 | |
| 120 | providerMap = storageService.<String, TelemetryConfigProvider>consistentMapBuilder() |
| 121 | .withSerializer(Serializer.using(SERIALIZER_TELEMETRY_CONFIG)) |
| 122 | .withName("openstack-telemetry-config-provider") |
| 123 | .withApplicationId(appId) |
| 124 | .build(); |
| 125 | |
| 126 | log.info("Started"); |
| 127 | } |
| 128 | |
| 129 | @Deactivate |
| 130 | protected void deactivate() { |
| 131 | telemetryConfigStore.unsetDelegate(delegate); |
| 132 | leadershipService.withdraw(appId.name()); |
| 133 | providerMap.clear(); |
| 134 | |
| 135 | log.info("Stopped"); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Set<TelemetryConfigProvider> getProviders() { |
| 140 | ImmutableSet.Builder<TelemetryConfigProvider> builder = ImmutableSet.builder(); |
| 141 | providerMap.asJavaMap().values().forEach(builder::add); |
| 142 | return builder.build(); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void registerProvider(TelemetryConfigProvider provider) { |
| 147 | if (isLeader()) { |
| 148 | StringBuilder nameBuilder = new StringBuilder(); |
| 149 | provider.getTelemetryConfigs().forEach(config -> { |
| 150 | nameBuilder.append(config.name()); |
| 151 | telemetryConfigStore.createTelemetryConfig(config); |
| 152 | log.info(String.format(MSG_TELEMETRY_CONFIG, config.name(), MSG_CREATED)); |
| 153 | }); |
| 154 | providerMap.put(nameBuilder.toString(), provider); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void unregisterProvider(TelemetryConfigProvider provider) { |
| 160 | if (isLeader()) { |
| 161 | StringBuilder nameBuilder = new StringBuilder(); |
| 162 | provider.getTelemetryConfigs().forEach(config -> { |
| 163 | nameBuilder.append(config.name()); |
| 164 | telemetryConfigStore.removeTelemetryConfig(config.name()); |
| 165 | log.info(String.format(MSG_TELEMETRY_CONFIG, config.name(), MSG_REMOVED)); |
| 166 | }); |
| 167 | providerMap.remove(nameBuilder.toString()); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void updateTelemetryConfig(TelemetryConfig config) { |
| 173 | checkNotNull(config, ERR_NULL_CONFIG); |
| 174 | |
| 175 | telemetryConfigStore.updateTelemetryConfig(config); |
| 176 | log.info(String.format(MSG_TELEMETRY_CONFIG, config.name(), MSG_UPDATED)); |
| 177 | } |
| 178 | |
| 179 | @Override |
Jian Li | 7fe7eaf | 2018-12-31 17:00:33 +0900 | [diff] [blame^] | 180 | public void removeTelemetryConfig(String name) { |
| 181 | checkNotNull(name, ERR_NULL_CONFIG_NAME); |
| 182 | |
| 183 | telemetryConfigStore.removeTelemetryConfig(name); |
| 184 | log.info(String.format(MSG_TELEMETRY_CONFIG, name, MSG_REMOVED)); |
| 185 | } |
| 186 | |
| 187 | @Override |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 188 | public TelemetryConfig getConfig(String name) { |
| 189 | return nullIsNotFound(telemetryConfigStore.telemetryConfig(name), NO_CONFIG); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Set<TelemetryConfig> getConfigsByType(ConfigType type) { |
| 194 | return ImmutableSet.copyOf(telemetryConfigStore.telemetryConfigsByType(type)); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public Set<TelemetryConfig> getConfigs() { |
| 199 | return ImmutableSet.copyOf(telemetryConfigStore.telemetryConfigs()); |
| 200 | } |
| 201 | |
| 202 | private boolean isLeader() { |
| 203 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 204 | } |
| 205 | |
| 206 | private class InternalTelemetryConfigStoreDelegate implements TelemetryConfigStoreDelegate { |
| 207 | |
| 208 | @Override |
| 209 | public void notify(TelemetryConfigEvent event) { |
| 210 | if (event != null) { |
| 211 | log.trace("send telemetry config event {}", event); |
| 212 | process(event); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |