blob: 0e8c5a075bf488f175c800f456c7e2df27422d94 [file] [log] [blame]
Jon Hall6b687cd2015-04-23 20:04:59 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jon Hall6b687cd2015-04-23 20:04:59 -07003 *
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 */
16package org.onosproject.distributedprimitives;
17
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070018import com.google.common.collect.Maps;
Jon Hall6b687cd2015-04-23 20:04:59 -070019import org.onosproject.core.ApplicationId;
20import org.onosproject.core.CoreService;
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070021import org.onosproject.store.serializers.KryoNamespaces;
Jordan Halterman18dac852018-01-25 17:19:30 -080022import org.onosproject.store.service.DistributedLock;
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070023import org.onosproject.store.service.EventuallyConsistentMap;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070024import org.onosproject.store.service.LeaderElector;
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070025import org.onosproject.store.service.StorageService;
26import org.onosproject.store.service.WallClockTimestamp;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070027import org.osgi.service.component.annotations.Activate;
28import org.osgi.service.component.annotations.Component;
29import org.osgi.service.component.annotations.Deactivate;
30import org.osgi.service.component.annotations.Reference;
31import org.osgi.service.component.annotations.ReferenceCardinality;
Jon Hall6b687cd2015-04-23 20:04:59 -070032import org.slf4j.Logger;
33
Ray Milkeyd84f89b2018-08-17 14:54:17 -070034import java.util.Map;
35
Jon Hall6b687cd2015-04-23 20:04:59 -070036import static org.slf4j.LoggerFactory.getLogger;
37
Jon Hall6b687cd2015-04-23 20:04:59 -070038/**
39 * Simple application to test distributed primitives.
40 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Component(immediate = true, service = DistributedPrimitivesTest.class)
Jon Hall6b687cd2015-04-23 20:04:59 -070042public 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 Milkeyd84f89b2018-08-17 14:54:17 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jon Hall6b687cd2015-04-23 20:04:59 -070050 protected CoreService coreService;
51
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070053 protected StorageService storageService;
54
55 private final Map<String, EventuallyConsistentMap<String, String>> maps = Maps.newConcurrentMap();
Jordan Halterman2bf177c2017-06-29 01:49:08 -070056 private final Map<String, LeaderElector> electors = Maps.newConcurrentMap();
Jordan Halterman18dac852018-01-25 17:19:30 -080057 private final Map<String, DistributedLock> locks = Maps.newConcurrentMap();
Jon Hall6b687cd2015-04-23 20:04:59 -070058
59 @Activate
60 protected void activate() {
Jon Hall6b687cd2015-04-23 20:04:59 -070061 log.info("Distributed-Primitives-test app started");
62 appId = coreService.registerApplication(APP_NAME);
63 }
64
65 @Deactivate
66 protected void deactivate() {
Jon Hall6b687cd2015-04-23 20:04:59 -070067 log.info("Distributed-Primitives-test app Stopped");
68 }
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070069
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 Halterman063b8822017-05-16 16:08:12 -070079 .withSerializer(KryoNamespaces.API)
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070080 .withTimestampProvider((k, v) -> new WallClockTimestamp())
81 .build());
82 }
Jordan Halterman2bf177c2017-06-29 01:49:08 -070083
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 Halterman18dac852018-01-25 17:19:30 -080092 .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 Halterman2bf177c2017-06-29 01:49:08 -0700108 }
Jon Hall6b687cd2015-04-23 20:04:59 -0700109}