yoonseon | bd8a93d | 2016-12-07 15:51:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | package org.onosproject.incubator.store.virtual.impl; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.onosproject.event.Event; |
| 21 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 22 | import org.onosproject.incubator.net.virtual.VirtualStore; |
| 23 | import org.onosproject.store.StoreDelegate; |
| 24 | |
| 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkState; |
| 29 | |
| 30 | /** |
| 31 | * Base implementation of a virtual store. |
| 32 | */ |
| 33 | public class AbstractVirtualStore<E extends Event, D extends StoreDelegate<E>> |
| 34 | implements VirtualStore<E, D> { |
| 35 | |
| 36 | protected Map<NetworkId, D> delegateMap = Maps.newConcurrentMap(); |
| 37 | |
| 38 | @Override |
| 39 | public void setDelegate(NetworkId networkId, D delegate) { |
| 40 | checkState(delegateMap.get(networkId) == null |
| 41 | || delegateMap.get(networkId) == delegate, |
| 42 | "Store delegate already set"); |
| 43 | |
| 44 | delegateMap.putIfAbsent(networkId, delegate); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public void unsetDelegate(NetworkId networkId, D delegate) { |
| 49 | if (delegateMap.get(networkId) == delegate) { |
| 50 | delegateMap.remove(networkId, delegate); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public boolean hasDelegate(NetworkId networkId) { |
| 56 | return delegateMap.get(networkId) != null; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Notifies the delegate with the specified event. |
| 61 | * |
| 62 | * @param networkId a virtual network identifier |
| 63 | * @param event event to delegate |
| 64 | */ |
| 65 | protected void notifyDelegate(NetworkId networkId, E event) { |
| 66 | if (delegateMap.get(networkId) != null) { |
| 67 | delegateMap.get(networkId).notify(event); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Notifies the delegate with the specified list of events. |
| 73 | * |
| 74 | * @param networkId a virtual network identifier |
| 75 | * @param events list of events to delegate |
| 76 | */ |
| 77 | protected void notifyDelegate(NetworkId networkId, List<E> events) { |
| 78 | for (E event: events) { |
| 79 | notifyDelegate(networkId, event); |
| 80 | } |
| 81 | } |
| 82 | } |