blob: 1de17ecfaffe7f2533813ecebdbed16c78aad67f [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
utkarsh663d0eb2018-04-10 14:52:06 +090021import org.onlab.util.KryoNamespace;
utkarsh663d0eb2018-04-10 14:52:06 +090022import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070024import org.onosproject.store.serializers.KryoNamespaces;
utkarsh663d0eb2018-04-10 14:52:06 +090025import org.onosproject.store.service.ConsistentMap;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070026import org.onosproject.store.service.Serializer;
utkarsh663d0eb2018-04-10 14:52:06 +090027import org.onosproject.store.service.StorageException;
28import org.onosproject.store.service.StorageService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070029import org.osgi.service.component.annotations.Activate;
30import org.osgi.service.component.annotations.Component;
31import org.osgi.service.component.annotations.Deactivate;
32import org.osgi.service.component.annotations.Reference;
33import org.osgi.service.component.annotations.ReferenceCardinality;
utkarsh663d0eb2018-04-10 14:52:06 +090034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37import java.util.ArrayList;
38import java.util.List;
39
40
41/**
42 * Cluster HA Test.
43 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044@Component(immediate = true, service = ClusterHATest.class)
utkarsh663d0eb2018-04-10 14:52:06 +090045public class ClusterHATest {
46
47 protected final Logger log = LoggerFactory.getLogger(getClass());
48
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY)
utkarsh663d0eb2018-04-10 14:52:06 +090050 protected CoreService coreService;
51
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY)
utkarsh663d0eb2018-04-10 14:52:06 +090053 protected StorageService storageService;
54
55 private ApplicationId appId;
56 private List<ConsistentMap<String, String>> stringStoreList;
57 private int numStores = 10;
58
59 @Activate
60 protected void activate() {
61 appId = coreService.registerApplication("org.onosproject.cluster");
62 stringStoreList = new ArrayList<ConsistentMap<String, String>>();
63 for (int i = 0; i < numStores; i++) {
64 ConsistentMap<String, String> stringStore = storageService.<String, String>consistentMapBuilder()
65 .withName("cluster-ha-store" + i)
66 .withApplicationId(appId)
67 .withSerializer(Serializer.using(new KryoNamespace.Builder()
68 .register(KryoNamespaces.API)
69 .register(String.class)
70 .build("string")))
71 .build();
72 stringStoreList.add(stringStore);
73 }
74 log.info("Started");
75 }
76
77 @Deactivate
78 protected void deactivate() {
79 for (ConsistentMap<String, String> stringStore: stringStoreList) {
80 stringStore.clear();
81 }
82 log.info("Stopped");
83 }
84
85 public void addToStringStore(int key, String str) {
86 try {
87 for (ConsistentMap<String, String> stringStore: stringStoreList) {
88 stringStore.put(String.valueOf(key), str);
89 }
90 } catch (StorageException e) {
91 log.error("StringStore - put got exception {} , for the key {} string {}", e, key, str);
92 }
93 }
94
95 public void removeStringFromStore(int key) {
96 try {
97 for (ConsistentMap<String, String> stringStore: stringStoreList) {
98 stringStore.remove(String.valueOf(key));
99 }
100 } catch (StorageException e) {
101 log.error("StringStore - put got exception {} , for the key {}", e, key);
102 }
103 }
104}