Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.opencord.igmpproxy.impl.store.machine; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import org.onosproject.store.AbstractStore; |
| 20 | import org.opencord.igmpproxy.statemachine.StateMachine; |
| 21 | import org.opencord.igmpproxy.statemachine.StateMachineId; |
| 22 | |
| 23 | import java.util.Collection; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | /** |
| 27 | * Abstract implementation of state-machine store. |
| 28 | */ |
| 29 | public abstract class AbstractStateMachineStore |
| 30 | extends AbstractStore<StateMachineEvent, StateMachineStoreDelegate> |
| 31 | implements StateMachineStore { |
| 32 | |
| 33 | protected Map<StateMachineId, StateMachine> stateMachineMap; |
| 34 | |
| 35 | protected AbstractStateMachineStore() { |
| 36 | } |
| 37 | |
| 38 | protected AbstractStateMachineStore(Map<StateMachineId, StateMachine> stateMachineMap) { |
| 39 | this.stateMachineMap = stateMachineMap; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public StateMachine putStateMachine(StateMachine stateMachine) { |
| 44 | return stateMachineMap.put(stateMachine.getStateMachineId(), stateMachine); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public StateMachine updateStateMachine(StateMachine machine) { |
| 49 | return stateMachineMap.replace(machine.getStateMachineId(), machine); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public StateMachine removeStateMachine(StateMachineId id) { |
| 54 | return stateMachineMap.remove(id); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public StateMachine getStateMachine(StateMachineId id) { |
| 59 | return stateMachineMap.get(id); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Collection<StateMachine> getAllStateMachines() { |
| 64 | return ImmutableList.copyOf(stateMachineMap.values()); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void clearAllStateMachines() { |
| 69 | stateMachineMap.clear(); |
| 70 | stateMachineMap = null; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void decreaseTimeout(StateMachineId machineId) { |
| 75 | StateMachine machine = stateMachineMap.get(machineId); |
| 76 | machine.decreaseTimeOut(); |
| 77 | updateStateMachine(machine); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void increaseTimeout(StateMachineId machineId) { |
| 82 | StateMachine machine = stateMachineMap.get(machineId); |
| 83 | machine.increaseTimeOut(); |
| 84 | updateStateMachine(machine); |
| 85 | } |
| 86 | |
| 87 | } |