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.core.ApplicationId; |
| 21 | import org.onosproject.core.CoreService; |
| 22 | import org.onosproject.openstacktelemetry.api.TelemetryConfigEvent; |
| 23 | import org.onosproject.openstacktelemetry.api.TelemetryConfigProvider; |
| 24 | import org.onosproject.openstacktelemetry.api.TelemetryConfigStore; |
| 25 | import org.onosproject.openstacktelemetry.api.TelemetryConfigStoreDelegate; |
| 26 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 27 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig.ConfigType; |
| 28 | import org.onosproject.store.AbstractStore; |
| 29 | import org.onosproject.store.serializers.KryoNamespaces; |
| 30 | import org.onosproject.store.service.ConsistentMap; |
| 31 | import org.onosproject.store.service.MapEvent; |
| 32 | import org.onosproject.store.service.MapEventListener; |
| 33 | import org.onosproject.store.service.Serializer; |
| 34 | import org.onosproject.store.service.StorageService; |
| 35 | import org.onosproject.store.service.Versioned; |
| 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.Set; |
| 44 | import java.util.concurrent.ExecutorService; |
| 45 | import java.util.stream.Collectors; |
| 46 | |
| 47 | import static com.google.common.base.Preconditions.checkArgument; |
| 48 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 49 | import static org.onlab.util.Tools.groupedThreads; |
| 50 | import static org.onosproject.openstacktelemetry.api.Constants.OPENSTACK_TELEMETRY_APP_ID; |
| 51 | import static org.onosproject.openstacktelemetry.api.TelemetryConfigEvent.Type.CONFIG_ADDED; |
| 52 | import static org.onosproject.openstacktelemetry.api.TelemetryConfigEvent.Type.CONFIG_DELETED; |
| 53 | import static org.onosproject.openstacktelemetry.api.TelemetryConfigEvent.Type.CONFIG_UPDATED; |
| 54 | import static org.slf4j.LoggerFactory.getLogger; |
| 55 | |
| 56 | /** |
| 57 | * Manages the inventory of telemetry configurations using a {@code ConsistentMap}. |
| 58 | */ |
| 59 | @Component(immediate = true, service = TelemetryConfigStore.class) |
| 60 | public class DistributedTelemetryConfigStore |
| 61 | extends AbstractStore<TelemetryConfigEvent, TelemetryConfigStoreDelegate> |
| 62 | implements TelemetryConfigStore { |
| 63 | |
| 64 | protected final Logger log = getLogger(getClass()); |
| 65 | |
| 66 | private static final String ERR_NOT_FOUND = " does not exist"; |
| 67 | private static final String ERR_DUPLICATE = " already exists"; |
| 68 | |
| 69 | private static final KryoNamespace SERIALIZER_TELEMETRY_CONFIG = |
| 70 | KryoNamespace.newBuilder() |
| 71 | .register(KryoNamespaces.API) |
| 72 | .register(TelemetryConfigProvider.class) |
| 73 | .register(DefaultTelemetryConfigProvider.class) |
| 74 | .register(TelemetryConfig.class) |
| 75 | .register(TelemetryConfig.ConfigType.class) |
| 76 | .register(DefaultTelemetryConfig.class) |
| 77 | .build(); |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 80 | protected CoreService coreService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 83 | protected StorageService storageService; |
| 84 | |
| 85 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 86 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 87 | |
| 88 | private final MapEventListener<String, TelemetryConfig> |
| 89 | telemetryConfigMapListener = new TelemetryConfigMapListener(); |
| 90 | |
| 91 | private ConsistentMap<String, TelemetryConfig> telemetryConfigStore; |
| 92 | |
| 93 | @Activate |
| 94 | protected void activate() { |
| 95 | ApplicationId appId = coreService.registerApplication(OPENSTACK_TELEMETRY_APP_ID); |
| 96 | |
| 97 | telemetryConfigStore = storageService.<String, TelemetryConfig>consistentMapBuilder() |
| 98 | .withSerializer(Serializer.using(SERIALIZER_TELEMETRY_CONFIG)) |
| 99 | .withName("telemetry-config-store") |
| 100 | .withApplicationId(appId) |
| 101 | .build(); |
| 102 | telemetryConfigStore.addListener(telemetryConfigMapListener); |
| 103 | |
| 104 | log.info("Started"); |
| 105 | } |
| 106 | |
| 107 | @Deactivate |
| 108 | protected void deactivate() { |
| 109 | telemetryConfigStore.removeListener(telemetryConfigMapListener); |
| 110 | eventExecutor.shutdown(); |
| 111 | |
| 112 | log.info("Stopped"); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void createTelemetryConfig(TelemetryConfig config) { |
| 117 | telemetryConfigStore.compute(config.name(), (name, existing) -> { |
| 118 | final String error = config.name() + ERR_DUPLICATE; |
| 119 | checkArgument(existing == null, error); |
| 120 | return config; |
| 121 | }); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void updateTelemetryConfig(TelemetryConfig config) { |
| 126 | telemetryConfigStore.compute(config.name(), (name, existing) -> { |
| 127 | final String error = config.name() + ERR_NOT_FOUND; |
| 128 | checkArgument(existing != null, error); |
| 129 | return config; |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public TelemetryConfig removeTelemetryConfig(String name) { |
| 135 | Versioned<TelemetryConfig> config = telemetryConfigStore.remove(name); |
| 136 | return config == null ? null : config.value(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public TelemetryConfig telemetryConfig(String name) { |
| 141 | return telemetryConfigStore.asJavaMap().get(name); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public Set<TelemetryConfig> telemetryConfigs() { |
| 146 | return ImmutableSet.copyOf(telemetryConfigStore.asJavaMap().values()); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public Set<TelemetryConfig> telemetryConfigsByType(ConfigType type) { |
| 151 | return ImmutableSet.copyOf(telemetryConfigStore.asJavaMap().values() |
| 152 | .stream().filter(c -> c.type() == type).collect(Collectors.toSet())); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void clear() { |
| 157 | telemetryConfigStore.clear(); |
| 158 | } |
| 159 | |
| 160 | private class TelemetryConfigMapListener |
| 161 | implements MapEventListener<String, TelemetryConfig> { |
| 162 | |
| 163 | @Override |
| 164 | public void event(MapEvent<String, TelemetryConfig> event) { |
| 165 | switch (event.type()) { |
| 166 | case INSERT: |
| 167 | eventExecutor.execute(() -> processTelemetryConfigMapInsertion(event)); |
| 168 | break; |
| 169 | case UPDATE: |
| 170 | eventExecutor.execute(() -> processTelemetryConfigMapUpdate(event)); |
| 171 | break; |
| 172 | case REMOVE: |
| 173 | eventExecutor.execute(() -> processTelemetryConfigMapRemoval(event)); |
| 174 | break; |
| 175 | default: |
| 176 | log.error("Unsupported telemetry config event type"); |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | private void processTelemetryConfigMapInsertion(MapEvent<String, |
| 182 | TelemetryConfig> event) { |
| 183 | log.debug("Telemetry config created"); |
| 184 | notifyDelegate(new TelemetryConfigEvent( |
| 185 | CONFIG_ADDED, event.newValue().value())); |
| 186 | } |
| 187 | |
| 188 | private void processTelemetryConfigMapUpdate(MapEvent<String, |
| 189 | TelemetryConfig> event) { |
| 190 | log.debug("Telemetry config updated"); |
| 191 | notifyDelegate(new TelemetryConfigEvent( |
| 192 | CONFIG_UPDATED, event.newValue().value())); |
| 193 | } |
| 194 | |
| 195 | private void processTelemetryConfigMapRemoval(MapEvent<String, |
| 196 | TelemetryConfig> event) { |
| 197 | log.debug("Telemetry config removed"); |
| 198 | notifyDelegate(new TelemetryConfigEvent( |
| 199 | CONFIG_DELETED, event.oldValue().value())); |
| 200 | } |
| 201 | } |
| 202 | } |