blob: 0ccca0a983cb80077dd9287e40c4b7b90a61be7a [file] [log] [blame]
utkarsh663d0eb2018-04-10 14:52:06 +09001/*
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
18package org.onosproject.clusterha;
19
20
21import 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;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.util.KryoNamespace;
28import org.onosproject.store.serializers.KryoNamespaces;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
31import org.onosproject.store.service.Serializer;
32import org.onosproject.store.service.ConsistentMap;
33import org.onosproject.store.service.StorageException;
34import org.onosproject.store.service.StorageService;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import java.util.ArrayList;
39import java.util.List;
40
41
42/**
43 * Cluster HA Test.
44 */
45@Component(immediate = true)
46@Service(value = ClusterHATest.class)
47public 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}