Bharat saraswal
Committed by bharat saraswal-huawei

[ONOS-3110] Port pair manager test case fixed

Change-Id: I7962527669e45dc7b4eb3e84dadcf9f059177bb5
...@@ -30,6 +30,8 @@ import org.onlab.util.KryoNamespace; ...@@ -30,6 +30,8 @@ import org.onlab.util.KryoNamespace;
30 import org.onosproject.event.AbstractListenerManager; 30 import org.onosproject.event.AbstractListenerManager;
31 import org.onosproject.store.serializers.KryoNamespaces; 31 import org.onosproject.store.serializers.KryoNamespaces;
32 import org.onosproject.store.service.EventuallyConsistentMap; 32 import org.onosproject.store.service.EventuallyConsistentMap;
33 +import org.onosproject.store.service.EventuallyConsistentMapEvent;
34 +import org.onosproject.store.service.EventuallyConsistentMapListener;
33 import org.onosproject.store.service.MultiValuedTimestamp; 35 import org.onosproject.store.service.MultiValuedTimestamp;
34 import org.onosproject.store.service.StorageService; 36 import org.onosproject.store.service.StorageService;
35 import org.onosproject.store.service.WallClockTimestamp; 37 import org.onosproject.store.service.WallClockTimestamp;
...@@ -51,17 +53,22 @@ public class PortPairManager extends AbstractListenerManager<PortPairEvent, Port ...@@ -51,17 +53,22 @@ public class PortPairManager extends AbstractListenerManager<PortPairEvent, Port
51 private static final String PORT_PAIR_ID_NULL = "PortPair ID cannot be null"; 53 private static final String PORT_PAIR_ID_NULL = "PortPair ID cannot be null";
52 private static final String PORT_PAIR_NULL = "PortPair cannot be null"; 54 private static final String PORT_PAIR_NULL = "PortPair cannot be null";
53 private static final String LISTENER_NOT_NULL = "Listener cannot be null"; 55 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
56 + private static final String EVENT_NOT_NULL = "event cannot be null";
54 57
55 private final Logger log = getLogger(getClass()); 58 private final Logger log = getLogger(getClass());
56 59
57 private EventuallyConsistentMap<PortPairId, PortPair> portPairStore; 60 private EventuallyConsistentMap<PortPairId, PortPair> portPairStore;
58 61
62 + private EventuallyConsistentMapListener<PortPairId, PortPair> portPairListener =
63 + new InnerPortPairStoreListener();
64 +
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected StorageService storageService; 66 protected StorageService storageService;
61 67
62 @Activate 68 @Activate
63 public void activate() { 69 public void activate() {
64 70
71 + eventDispatcher.addSink(PortPairEvent.class, listenerRegistry);
65 KryoNamespace.Builder serializer = KryoNamespace.newBuilder() 72 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
66 .register(KryoNamespaces.API) 73 .register(KryoNamespaces.API)
67 .register(MultiValuedTimestamp.class) 74 .register(MultiValuedTimestamp.class)
...@@ -73,11 +80,14 @@ public class PortPairManager extends AbstractListenerManager<PortPairEvent, Port ...@@ -73,11 +80,14 @@ public class PortPairManager extends AbstractListenerManager<PortPairEvent, Port
73 .withTimestampProvider((k, v) -> new WallClockTimestamp()) 80 .withTimestampProvider((k, v) -> new WallClockTimestamp())
74 .build(); 81 .build();
75 82
83 + portPairStore.addListener(portPairListener);
84 +
76 log.info("Started"); 85 log.info("Started");
77 } 86 }
78 87
79 @Deactivate 88 @Deactivate
80 public void deactivate() { 89 public void deactivate() {
90 + eventDispatcher.removeSink(PortPairEvent.class);
81 portPairStore.destroy(); 91 portPairStore.destroy();
82 log.info("Stopped"); 92 log.info("Stopped");
83 } 93 }
...@@ -145,4 +155,35 @@ public class PortPairManager extends AbstractListenerManager<PortPairEvent, Port ...@@ -145,4 +155,35 @@ public class PortPairManager extends AbstractListenerManager<PortPairEvent, Port
145 } 155 }
146 return true; 156 return true;
147 } 157 }
158 +
159 + private class InnerPortPairStoreListener
160 + implements
161 + EventuallyConsistentMapListener<PortPairId, PortPair> {
162 +
163 + @Override
164 + public void event(EventuallyConsistentMapEvent<PortPairId, PortPair> event) {
165 + checkNotNull(event, EVENT_NOT_NULL);
166 + PortPair portPair = event.value();
167 + if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
168 + notifyListeners(new PortPairEvent(
169 + PortPairEvent.Type.PORT_PAIR_PUT,
170 + portPair));
171 + }
172 + if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
173 + notifyListeners(new PortPairEvent(
174 + PortPairEvent.Type.PORT_PAIR_DELETE,
175 + portPair));
176 + }
177 + }
178 + }
179 +
180 + /**
181 + * Notifies specify event to all listeners.
182 + *
183 + * @param event port pair event
184 + */
185 + private void notifyListeners(PortPairEvent event) {
186 + checkNotNull(event, EVENT_NOT_NULL);
187 + post(event);
188 + }
148 } 189 }
......
...@@ -26,6 +26,9 @@ import org.onosproject.vtnrsc.PortPairId; ...@@ -26,6 +26,9 @@ import org.onosproject.vtnrsc.PortPairId;
26 import org.onosproject.vtnrsc.TenantId; 26 import org.onosproject.vtnrsc.TenantId;
27 import org.onosproject.vtnrsc.DefaultPortPair; 27 import org.onosproject.vtnrsc.DefaultPortPair;
28 import org.onosproject.vtnrsc.util.VtnStorageServiceTest; 28 import org.onosproject.vtnrsc.util.VtnStorageServiceTest;
29 +import org.onosproject.common.event.impl.TestEventDispatcher;
30 +
31 +import static org.onosproject.net.NetTestTools.injectEventDispatcher;
29 32
30 /** 33 /**
31 * Unit tests for PortPairManager class. 34 * Unit tests for PortPairManager class.
...@@ -49,6 +52,7 @@ public class PortPairManagerTest { ...@@ -49,6 +52,7 @@ public class PortPairManagerTest {
49 public void testCreatePortPair() { 52 public void testCreatePortPair() {
50 // initialize port pair manager 53 // initialize port pair manager
51 portPairMgr.storageService = storageService; 54 portPairMgr.storageService = storageService;
55 + injectEventDispatcher(portPairMgr, new TestEventDispatcher());
52 portPairMgr.activate(); 56 portPairMgr.activate();
53 57
54 // create port pair 58 // create port pair
......