blob: e481e819484b3ab689139ac44579135987ef2cf4 [file] [log] [blame]
Jon Hall6b687cd2015-04-23 20:04:59 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 java.util.Map;
19
20import com.google.common.collect.Maps;
Jon Hall6b687cd2015-04-23 20:04:59 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070026import org.apache.felix.scr.annotations.Service;
Jon Hall6b687cd2015-04-23 20:04:59 -070027import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070029import org.onosproject.store.serializers.KryoNamespaces;
30import org.onosproject.store.service.EventuallyConsistentMap;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070031import org.onosproject.store.service.LeaderElector;
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070032import org.onosproject.store.service.StorageService;
33import org.onosproject.store.service.WallClockTimestamp;
Jon Hall6b687cd2015-04-23 20:04:59 -070034import org.slf4j.Logger;
35
36import static org.slf4j.LoggerFactory.getLogger;
37
38
39/**
40 * Simple application to test distributed primitives.
41 */
42@Component(immediate = true)
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070043@Service(value = DistributedPrimitivesTest.class)
Jon Hall6b687cd2015-04-23 20:04:59 -070044public 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 Halterman73a6f5d2017-05-11 10:37:43 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected StorageService storageService;
56
57 private final Map<String, EventuallyConsistentMap<String, String>> maps = Maps.newConcurrentMap();
Jordan Halterman2bf177c2017-06-29 01:49:08 -070058 private final Map<String, LeaderElector> electors = Maps.newConcurrentMap();
Jon Hall6b687cd2015-04-23 20:04:59 -070059
60 @Activate
61 protected void activate() {
Jon Hall6b687cd2015-04-23 20:04:59 -070062 log.info("Distributed-Primitives-test app started");
63 appId = coreService.registerApplication(APP_NAME);
64 }
65
66 @Deactivate
67 protected void deactivate() {
Jon Hall6b687cd2015-04-23 20:04:59 -070068 log.info("Distributed-Primitives-test app Stopped");
69 }
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070070
71 /**
72 * Returns an eventually consistent test map by name.
73 *
74 * @param name the test map name
75 * @return the test map
76 */
77 public EventuallyConsistentMap<String, String> getEcMap(String name) {
78 return maps.computeIfAbsent(name, n -> storageService.<String, String>eventuallyConsistentMapBuilder()
79 .withName(name)
Jordan Halterman063b8822017-05-16 16:08:12 -070080 .withSerializer(KryoNamespaces.API)
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070081 .withTimestampProvider((k, v) -> new WallClockTimestamp())
82 .build());
83 }
Jordan Halterman2bf177c2017-06-29 01:49:08 -070084
85 /**
86 * Returns a leader elector session by name.
87 *
88 * @param name the leader elector name
89 * @return the leader elector
90 */
91 public LeaderElector getLeaderElector(String name) {
92 return electors.computeIfAbsent(name, n -> storageService.leaderElectorBuilder()
93 .withName(name)
94 .build()
95 .asLeaderElector());
96 }
Jon Hall6b687cd2015-04-23 20:04:59 -070097}