blob: f337b097ac099b18d8bc094672250e24fcd86fea [file] [log] [blame]
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +00001/*
2 * Copyright 2017-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 */
16package org.opencord.igmpproxy.impl.store.machine;
17
18import org.onlab.util.KryoNamespace;
19import org.onosproject.store.serializers.KryoNamespaces;
20import org.onosproject.store.service.AtomicCounterMap;
21import org.onosproject.store.service.ConsistentMap;
22import org.onosproject.store.service.Serializer;
23import org.onosproject.store.service.StorageService;
24import org.opencord.igmpproxy.impl.SingleStateMachine;
25import org.opencord.igmpproxy.impl.SingleStateMachineSerializer;
26import org.opencord.igmpproxy.statemachine.StateMachine;
27import org.opencord.igmpproxy.statemachine.StateMachineId;
28import org.opencord.igmpproxy.statemachine.StateMachineIdSerializer;
29import org.osgi.service.component.annotations.Component;
30import org.osgi.service.component.annotations.ReferenceCardinality;
31import org.osgi.service.component.annotations.Reference;
32import org.osgi.service.component.annotations.Activate;
33import org.osgi.service.component.annotations.Deactivate;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37/**
38 * State machine store based on distributed storage.
39 */
40@Component(service = StateMachineStore.class)
41public class DistributedStateMachineStore extends AbstractStateMachineStore {
42 private final Logger log = LoggerFactory.getLogger(getClass());
43 private static final String STATE_MACHINE_COUNTER_STORE = "onos-state-machine-counter-store";
44 private static final String STATE_MACHINE_MAP_NAME = "onos-state-machine-store";
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY)
47 protected StorageService storageService;
48
49 private AtomicCounterMap<StateMachineId> stateMachineCounters;
50
51 private ConsistentMap<StateMachineId, StateMachine> consistentMap;
52
53 public DistributedStateMachineStore() {
54 super();
55 }
56
57 @Activate
58 public void activate() {
59 KryoNamespace.Builder stateMachineKryoBuilder = KryoNamespace.newBuilder()
60 .register(KryoNamespaces.API)
61 .register(new StateMachineIdSerializer(), StateMachineId.class);
62
63 stateMachineCounters = storageService.<StateMachineId>atomicCounterMapBuilder()
64 .withName(STATE_MACHINE_COUNTER_STORE)
65 .withSerializer(Serializer.using(stateMachineKryoBuilder.build())).build();
66
67 stateMachineKryoBuilder
68 .register(new SingleStateMachineSerializer(), SingleStateMachine.class);
69 this.consistentMap = storageService.<StateMachineId, StateMachine>consistentMapBuilder()
70 .withName(STATE_MACHINE_MAP_NAME)
71 .withSerializer(Serializer.using(stateMachineKryoBuilder.build()))
72 .build();
73 super.stateMachineMap = consistentMap.asJavaMap();
74
75
76 log.info("Started.");
77 }
78
79 @Deactivate
80 public void deactivate() {
81 stateMachineMap.clear();
82 stateMachineMap = null;
83 consistentMap.destroy();
84 stateMachineCounters.clear();
85 stateMachineCounters.destroy();
86 log.info("Stopped.");
87 }
88
89 @Override
90 public long increaseAndGetCounter(StateMachineId stateMachineId) {
91 return stateMachineCounters.incrementAndGet(stateMachineId);
92 }
93
94 @Override
95 public long decreaseAndGetCounter(StateMachineId stateMachineId) {
96 if (stateMachineCounters.get(stateMachineId) > 0) {
97 return stateMachineCounters.decrementAndGet(stateMachineId);
98 } else {
99 return stateMachineCounters.get(stateMachineId);
100 }
101 }
102
103 @Override
104 public long getCounter(StateMachineId stateMachineId) {
105 return stateMachineCounters.get(stateMachineId);
106 }
107
108 @Override
109 public boolean removeCounter(StateMachineId stateMachineId) {
110 stateMachineCounters.remove(stateMachineId);
111 return true;
112 }
113
114 @Override
115 public void clearAllStateMachines() {
116 super.clearAllStateMachines();
117 stateMachineCounters.clear();
118 }
119}