Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-present Open Networking Laboratory |
| 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.store.core.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 30 | import org.apache.felix.scr.annotations.Service; |
| 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.ApplicationIdStore; |
| 33 | import org.onosproject.core.DefaultApplicationId; |
| 34 | import org.onosproject.store.serializers.KryoNamespaces; |
| 35 | import org.onosproject.store.service.AtomicCounter; |
| 36 | import org.onosproject.store.service.ConsistentMap; |
| 37 | import org.onosproject.store.service.MapEvent; |
| 38 | import org.onosproject.store.service.MapEventListener; |
| 39 | import org.onosproject.store.service.Serializer; |
| 40 | import org.onosproject.store.service.StorageService; |
| 41 | import org.onosproject.store.service.Versioned; |
| 42 | import org.slf4j.Logger; |
| 43 | |
| 44 | |
| 45 | import com.google.common.collect.ImmutableSet; |
| 46 | import com.google.common.collect.Maps; |
| 47 | |
| 48 | /** |
| 49 | * ApplicationIdStore implementation on top of {@code AtomicCounter} |
| 50 | * and {@code ConsistentMap} primitives. |
| 51 | */ |
| 52 | @Component(immediate = true, enabled = true) |
| 53 | @Service |
| 54 | public class DistributedApplicationIdStore implements ApplicationIdStore { |
| 55 | |
| 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected StorageService storageService; |
| 60 | |
| 61 | private AtomicCounter appIdCounter; |
| 62 | private ConsistentMap<String, ApplicationId> registeredIds; |
| 63 | private Map<Short, ApplicationId> idToAppIdCache = Maps.newConcurrentMap(); |
| 64 | private MapEventListener<String, ApplicationId> mapEventListener = event -> { |
| 65 | if (event.type() == MapEvent.Type.INSERT) { |
| 66 | idToAppIdCache.put(event.newValue().value().id(), event.newValue().value()); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | @Activate |
| 71 | public void activate() { |
| 72 | appIdCounter = storageService.getAtomicCounter("onos-app-id-counter"); |
| 73 | |
| 74 | registeredIds = storageService.<String, ApplicationId>consistentMapBuilder() |
| 75 | .withName("onos-app-ids") |
| 76 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 77 | .withRelaxedReadConsistency() |
| 78 | .build(); |
| 79 | |
| 80 | primeIdToAppIdCache(); |
| 81 | registeredIds.addListener(mapEventListener); |
| 82 | |
| 83 | log.info("Started"); |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | public void deactivate() { |
| 88 | registeredIds.removeListener(mapEventListener); |
| 89 | log.info("Stopped"); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public Set<ApplicationId> getAppIds() { |
| 94 | return ImmutableSet.copyOf(registeredIds.asJavaMap().values()); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public ApplicationId getAppId(Short id) { |
| 99 | if (!idToAppIdCache.containsKey(id)) { |
| 100 | primeIdToAppIdCache(); |
| 101 | } |
| 102 | return idToAppIdCache.get(id); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public ApplicationId getAppId(String name) { |
| 107 | return registeredIds.asJavaMap().get(name); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public ApplicationId registerApplication(String name) { |
| 112 | return Versioned.valueOrNull(registeredIds.computeIfAbsent(name, |
| 113 | key -> new DefaultApplicationId((int) appIdCounter.incrementAndGet(), name))); |
| 114 | } |
| 115 | |
| 116 | private void primeIdToAppIdCache() { |
| 117 | registeredIds.asJavaMap() |
| 118 | .values() |
| 119 | .forEach(appId -> { |
| 120 | idToAppIdCache.putIfAbsent(appId.id(), appId); |
| 121 | }); |
| 122 | } |
| 123 | } |