blob: f5c9eec532305b464146e4f0a309a34e51d22593 [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;
31import org.onosproject.store.service.StorageService;
32import org.onosproject.store.service.WallClockTimestamp;
Jon Hall6b687cd2015-04-23 20:04:59 -070033import org.slf4j.Logger;
34
35import static org.slf4j.LoggerFactory.getLogger;
36
37
38/**
39 * Simple application to test distributed primitives.
40 */
41@Component(immediate = true)
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070042@Service(value = DistributedPrimitivesTest.class)
Jon Hall6b687cd2015-04-23 20:04:59 -070043public class DistributedPrimitivesTest {
44
45 private final Logger log = getLogger(getClass());
46
47 private static final String APP_NAME = "org.onosproject.distributedprimitives";
48 private ApplicationId appId;
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected CoreService coreService;
52
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070053 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected StorageService storageService;
55
56 private final Map<String, EventuallyConsistentMap<String, String>> maps = Maps.newConcurrentMap();
Jon Hall6b687cd2015-04-23 20:04:59 -070057
58 @Activate
59 protected void activate() {
Jon Hall6b687cd2015-04-23 20:04:59 -070060 log.info("Distributed-Primitives-test app started");
61 appId = coreService.registerApplication(APP_NAME);
62 }
63
64 @Deactivate
65 protected void deactivate() {
Jon Hall6b687cd2015-04-23 20:04:59 -070066 log.info("Distributed-Primitives-test app Stopped");
67 }
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070068
69 /**
70 * Returns an eventually consistent test map by name.
71 *
72 * @param name the test map name
73 * @return the test map
74 */
75 public EventuallyConsistentMap<String, String> getEcMap(String name) {
76 return maps.computeIfAbsent(name, n -> storageService.<String, String>eventuallyConsistentMapBuilder()
77 .withName(name)
Jordan Halterman063b8822017-05-16 16:08:12 -070078 .withSerializer(KryoNamespaces.API)
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070079 .withTimestampProvider((k, v) -> new WallClockTimestamp())
80 .build());
81 }
Jon Hall6b687cd2015-04-23 20:04:59 -070082}