Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
| 30 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 31 | import org.onosproject.store.service.StorageService; |
| 32 | import org.onosproject.store.service.WallClockTimestamp; |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import static org.slf4j.LoggerFactory.getLogger; |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Simple application to test distributed primitives. |
| 40 | */ |
| 41 | @Component(immediate = true) |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 42 | @Service(value = DistributedPrimitivesTest.class) |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 43 | public 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 Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected StorageService storageService; |
| 55 | |
| 56 | private final Map<String, EventuallyConsistentMap<String, String>> maps = Maps.newConcurrentMap(); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 57 | |
| 58 | @Activate |
| 59 | protected void activate() { |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 60 | log.info("Distributed-Primitives-test app started"); |
| 61 | appId = coreService.registerApplication(APP_NAME); |
| 62 | } |
| 63 | |
| 64 | @Deactivate |
| 65 | protected void deactivate() { |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 66 | log.info("Distributed-Primitives-test app Stopped"); |
| 67 | } |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 68 | |
| 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 Halterman | dd68f62 | 2017-05-16 16:08:12 -0700 | [diff] [blame^] | 78 | .withSerializer(KryoNamespaces.API) |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 79 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 80 | .build()); |
| 81 | } |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 82 | } |