utkarsh | 663d0eb | 2018-04-10 14:52:06 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 17 | |
| 18 | package org.onosproject.clusterha; |
| 19 | |
| 20 | |
| 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; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.util.KryoNamespace; |
| 28 | import org.onosproject.store.serializers.KryoNamespaces; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.store.service.Serializer; |
| 32 | import org.onosproject.store.service.ConsistentMap; |
| 33 | import org.onosproject.store.service.StorageException; |
| 34 | import org.onosproject.store.service.StorageService; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.List; |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Cluster HA Test. |
| 44 | */ |
| 45 | @Component(immediate = true) |
| 46 | @Service(value = ClusterHATest.class) |
| 47 | public class ClusterHATest { |
| 48 | |
| 49 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected CoreService coreService; |
| 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected StorageService storageService; |
| 56 | |
| 57 | private ApplicationId appId; |
| 58 | private List<ConsistentMap<String, String>> stringStoreList; |
| 59 | private int numStores = 10; |
| 60 | |
| 61 | @Activate |
| 62 | protected void activate() { |
| 63 | appId = coreService.registerApplication("org.onosproject.cluster"); |
| 64 | stringStoreList = new ArrayList<ConsistentMap<String, String>>(); |
| 65 | for (int i = 0; i < numStores; i++) { |
| 66 | ConsistentMap<String, String> stringStore = storageService.<String, String>consistentMapBuilder() |
| 67 | .withName("cluster-ha-store" + i) |
| 68 | .withApplicationId(appId) |
| 69 | .withSerializer(Serializer.using(new KryoNamespace.Builder() |
| 70 | .register(KryoNamespaces.API) |
| 71 | .register(String.class) |
| 72 | .build("string"))) |
| 73 | .build(); |
| 74 | stringStoreList.add(stringStore); |
| 75 | } |
| 76 | log.info("Started"); |
| 77 | } |
| 78 | |
| 79 | @Deactivate |
| 80 | protected void deactivate() { |
| 81 | for (ConsistentMap<String, String> stringStore: stringStoreList) { |
| 82 | stringStore.clear(); |
| 83 | } |
| 84 | log.info("Stopped"); |
| 85 | } |
| 86 | |
| 87 | public void addToStringStore(int key, String str) { |
| 88 | try { |
| 89 | for (ConsistentMap<String, String> stringStore: stringStoreList) { |
| 90 | stringStore.put(String.valueOf(key), str); |
| 91 | } |
| 92 | } catch (StorageException e) { |
| 93 | log.error("StringStore - put got exception {} , for the key {} string {}", e, key, str); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public void removeStringFromStore(int key) { |
| 98 | try { |
| 99 | for (ConsistentMap<String, String> stringStore: stringStoreList) { |
| 100 | stringStore.remove(String.valueOf(key)); |
| 101 | } |
| 102 | } catch (StorageException e) { |
| 103 | log.error("StringStore - put got exception {} , for the key {}", e, key); |
| 104 | } |
| 105 | } |
| 106 | } |