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