Committed by
bharat saraswal-huawei
[ONOS-3116] Port Chain manager test case fixed
Change-Id: I1772cff0e33fff4f2ece43aea737f51408efdb74
Showing
2 changed files
with
46 additions
and
1 deletions
| ... | @@ -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; |
| ... | @@ -50,17 +52,22 @@ public class PortChainManager extends AbstractListenerManager<PortChainEvent, Po | ... | @@ -50,17 +52,22 @@ public class PortChainManager extends AbstractListenerManager<PortChainEvent, Po |
| 50 | 52 | ||
| 51 | private static final String PORT_CHAIN_ID_NULL = "PortChain ID cannot be null"; | 53 | private static final String PORT_CHAIN_ID_NULL = "PortChain ID cannot be null"; |
| 52 | private static final String PORT_CHAIN_NULL = "PortChain cannot be null"; | 54 | private static final String PORT_CHAIN_NULL = "PortChain cannot be null"; |
| 53 | - private static final String LISTENER_NOT_NULL = "Listener cannot be null"; | 55 | + private static final String EVENT_NOT_NULL = "event cannot be null"; |
| 54 | 56 | ||
| 55 | private final Logger log = getLogger(getClass()); | 57 | private final Logger log = getLogger(getClass()); |
| 56 | private EventuallyConsistentMap<PortChainId, PortChain> portChainStore; | 58 | private EventuallyConsistentMap<PortChainId, PortChain> portChainStore; |
| 57 | 59 | ||
| 60 | + private EventuallyConsistentMapListener<PortChainId, PortChain> portChainListener = | ||
| 61 | + new InnerPortChainStoreListener(); | ||
| 62 | + | ||
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected StorageService storageService; | 64 | protected StorageService storageService; |
| 60 | 65 | ||
| 61 | @Activate | 66 | @Activate |
| 62 | public void activate() { | 67 | public void activate() { |
| 63 | 68 | ||
| 69 | + eventDispatcher.addSink(PortChainEvent.class, listenerRegistry); | ||
| 70 | + | ||
| 64 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() | 71 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 65 | .register(KryoNamespaces.API) | 72 | .register(KryoNamespaces.API) |
| 66 | .register(MultiValuedTimestamp.class) | 73 | .register(MultiValuedTimestamp.class) |
| ... | @@ -71,11 +78,14 @@ public class PortChainManager extends AbstractListenerManager<PortChainEvent, Po | ... | @@ -71,11 +78,14 @@ public class PortChainManager extends AbstractListenerManager<PortChainEvent, Po |
| 71 | .withName("portchainstore").withSerializer(serializer) | 78 | .withName("portchainstore").withSerializer(serializer) |
| 72 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); | 79 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
| 73 | 80 | ||
| 81 | + portChainStore.addListener(portChainListener); | ||
| 82 | + | ||
| 74 | log.info("Started"); | 83 | log.info("Started"); |
| 75 | } | 84 | } |
| 76 | 85 | ||
| 77 | @Deactivate | 86 | @Deactivate |
| 78 | public void deactivate() { | 87 | public void deactivate() { |
| 88 | + eventDispatcher.removeSink(PortChainEvent.class); | ||
| 79 | portChainStore.destroy(); | 89 | portChainStore.destroy(); |
| 80 | log.info("Stopped"); | 90 | log.info("Stopped"); |
| 81 | } | 91 | } |
| ... | @@ -147,4 +157,35 @@ public class PortChainManager extends AbstractListenerManager<PortChainEvent, Po | ... | @@ -147,4 +157,35 @@ public class PortChainManager extends AbstractListenerManager<PortChainEvent, Po |
| 147 | } | 157 | } |
| 148 | return true; | 158 | return true; |
| 149 | } | 159 | } |
| 160 | + | ||
| 161 | + private class InnerPortChainStoreListener | ||
| 162 | + implements | ||
| 163 | + EventuallyConsistentMapListener<PortChainId, PortChain> { | ||
| 164 | + | ||
| 165 | + @Override | ||
| 166 | + public void event(EventuallyConsistentMapEvent<PortChainId, PortChain> event) { | ||
| 167 | + checkNotNull(event, EVENT_NOT_NULL); | ||
| 168 | + PortChain portChain = event.value(); | ||
| 169 | + if (EventuallyConsistentMapEvent.Type.PUT == event.type()) { | ||
| 170 | + notifyListeners(new PortChainEvent( | ||
| 171 | + PortChainEvent.Type.PORT_CHAIN_PUT, | ||
| 172 | + portChain)); | ||
| 173 | + } | ||
| 174 | + if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) { | ||
| 175 | + notifyListeners(new PortChainEvent( | ||
| 176 | + PortChainEvent.Type.PORT_CHAIN_DELETE, | ||
| 177 | + portChain)); | ||
| 178 | + } | ||
| 179 | + } | ||
| 180 | + } | ||
| 181 | + | ||
| 182 | + /** | ||
| 183 | + * Notifies specify event to all listeners. | ||
| 184 | + * | ||
| 185 | + * @param event port chain event | ||
| 186 | + */ | ||
| 187 | + private void notifyListeners(PortChainEvent event) { | ||
| 188 | + checkNotNull(event, EVENT_NOT_NULL); | ||
| 189 | + post(event); | ||
| 190 | + } | ||
| 150 | } | 191 | } | ... | ... |
| ... | @@ -31,6 +31,9 @@ import org.onosproject.vtnrsc.PortChain; | ... | @@ -31,6 +31,9 @@ import org.onosproject.vtnrsc.PortChain; |
| 31 | import org.onosproject.vtnrsc.DefaultPortChain; | 31 | import org.onosproject.vtnrsc.DefaultPortChain; |
| 32 | import org.onosproject.vtnrsc.DefaultFlowClassifier; | 32 | import org.onosproject.vtnrsc.DefaultFlowClassifier; |
| 33 | import org.onosproject.vtnrsc.util.VtnStorageServiceTest; | 33 | import org.onosproject.vtnrsc.util.VtnStorageServiceTest; |
| 34 | +import org.onosproject.common.event.impl.TestEventDispatcher; | ||
| 35 | + | ||
| 36 | +import static org.onosproject.net.NetTestTools.injectEventDispatcher; | ||
| 34 | 37 | ||
| 35 | /** | 38 | /** |
| 36 | * Unit tests for PortChainManager class. | 39 | * Unit tests for PortChainManager class. |
| ... | @@ -55,6 +58,7 @@ public class PortChainManagerTest { | ... | @@ -55,6 +58,7 @@ public class PortChainManagerTest { |
| 55 | public void testCreatePortChain() { | 58 | public void testCreatePortChain() { |
| 56 | // initialize port chain manager | 59 | // initialize port chain manager |
| 57 | portChainMgr.storageService = storageService; | 60 | portChainMgr.storageService = storageService; |
| 61 | + injectEventDispatcher(portChainMgr, new TestEventDispatcher()); | ||
| 58 | portChainMgr.activate(); | 62 | portChainMgr.activate(); |
| 59 | 63 | ||
| 60 | // create list of Port Pair Groups. | 64 | // create list of Port Pair Groups. | ... | ... |
-
Please register or login to post a comment