blob: 8e3a5bffeaa418b45337585a3dbf4446bd66f091 [file] [log] [blame]
yoonseonbd8a93d2016-12-07 15:51:21 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
yoonseonbd8a93d2016-12-07 15:51:21 -08003 *
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
17package org.onosproject.incubator.store.virtual.impl;
18
19import com.google.common.collect.Maps;
20import org.onosproject.event.Event;
21import org.onosproject.incubator.net.virtual.NetworkId;
22import org.onosproject.incubator.net.virtual.VirtualStore;
23import org.onosproject.store.StoreDelegate;
24
25import java.util.List;
26import java.util.Map;
27
28import static com.google.common.base.Preconditions.checkState;
29
30/**
31 * Base implementation of a virtual store.
32 */
33public 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}