Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 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.distributedprimitives; |
| 17 | |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 18 | import java.util.Map; |
| 19 | |
| 20 | import com.google.common.collect.Maps; |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Service; |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 29 | import org.onosproject.store.serializers.KryoNamespaces; |
Jordan Halterman | 2c9b159 | 2018-01-25 17:19:30 -0800 | [diff] [blame^] | 30 | import org.onosproject.store.service.DistributedLock; |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 31 | import org.onosproject.store.service.EventuallyConsistentMap; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.LeaderElector; |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 33 | import org.onosproject.store.service.StorageService; |
| 34 | import org.onosproject.store.service.WallClockTimestamp; |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Simple application to test distributed primitives. |
| 41 | */ |
| 42 | @Component(immediate = true) |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 43 | @Service(value = DistributedPrimitivesTest.class) |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 44 | public class DistributedPrimitivesTest { |
| 45 | |
| 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
| 48 | private static final String APP_NAME = "org.onosproject.distributedprimitives"; |
| 49 | private ApplicationId appId; |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected CoreService coreService; |
| 53 | |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected StorageService storageService; |
| 56 | |
| 57 | private final Map<String, EventuallyConsistentMap<String, String>> maps = Maps.newConcurrentMap(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 58 | private final Map<String, LeaderElector> electors = Maps.newConcurrentMap(); |
Jordan Halterman | 2c9b159 | 2018-01-25 17:19:30 -0800 | [diff] [blame^] | 59 | private final Map<String, DistributedLock> locks = Maps.newConcurrentMap(); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 60 | |
| 61 | @Activate |
| 62 | protected void activate() { |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 63 | log.info("Distributed-Primitives-test app started"); |
| 64 | appId = coreService.registerApplication(APP_NAME); |
| 65 | } |
| 66 | |
| 67 | @Deactivate |
| 68 | protected void deactivate() { |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 69 | log.info("Distributed-Primitives-test app Stopped"); |
| 70 | } |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Returns an eventually consistent test map by name. |
| 74 | * |
| 75 | * @param name the test map name |
| 76 | * @return the test map |
| 77 | */ |
| 78 | public EventuallyConsistentMap<String, String> getEcMap(String name) { |
| 79 | return maps.computeIfAbsent(name, n -> storageService.<String, String>eventuallyConsistentMapBuilder() |
| 80 | .withName(name) |
Jordan Halterman | 063b882 | 2017-05-16 16:08:12 -0700 | [diff] [blame] | 81 | .withSerializer(KryoNamespaces.API) |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 82 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 83 | .build()); |
| 84 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Returns a leader elector session by name. |
| 88 | * |
| 89 | * @param name the leader elector name |
| 90 | * @return the leader elector |
| 91 | */ |
| 92 | public LeaderElector getLeaderElector(String name) { |
| 93 | return electors.computeIfAbsent(name, n -> storageService.leaderElectorBuilder() |
Jordan Halterman | 2c9b159 | 2018-01-25 17:19:30 -0800 | [diff] [blame^] | 94 | .withName(name) |
| 95 | .build() |
| 96 | .asLeaderElector()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns a lock instance by name. |
| 101 | * |
| 102 | * @param name the lock name |
| 103 | * @return the lock |
| 104 | */ |
| 105 | public DistributedLock getLock(String name) { |
| 106 | return locks.computeIfAbsent(name, n -> storageService.lockBuilder() |
| 107 | .withName(name) |
| 108 | .build() |
| 109 | .asLock()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 110 | } |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 111 | } |