a9b9fdb0dd79bea2beb5f24281c8927610378600
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / test / java / org / opendaylight / vpnservice / bgpmanager / test / MockFibManager.java
1 package org.opendaylight.vpnservice.bgpmanager.test;
2
3 import java.util.Collection;
4
5 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
6 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
7 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
8 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
9 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.FibEntries;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTables;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntry;
13 import org.opendaylight.yangtools.concepts.ListenerRegistration;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
15
16 public class MockFibManager extends AbstractMockFibManager<VrfEntry> {
17
18     private int nFibEntries = 0;
19
20     private ListenerRegistration<MockFibManager> listenerRegistration;
21
22     public MockFibManager( final DataBroker db) {
23         super() ;
24         registerListener(db) ;
25     }
26
27     private void registerListener(final DataBroker db) {
28         final DataTreeIdentifier<VrfEntry> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, getWildCardPath());
29         try {
30             listenerRegistration = db.registerDataTreeChangeListener(treeId, MockFibManager.this);
31         } catch (final Exception e) {
32             throw new IllegalStateException("FibManager registration Listener fail! System needs restart.", e);
33         }
34     }
35
36     private InstanceIdentifier<VrfEntry> getWildCardPath() {
37             return InstanceIdentifier.create(FibEntries.class).child(VrfTables.class).child(VrfEntry.class);
38      }
39
40     @Override
41     public void onDataTreeChanged(Collection<DataTreeModification<VrfEntry>> changes) {
42         for (DataTreeModification<VrfEntry> change : changes) {
43             final InstanceIdentifier<VrfEntry> key = change.getRootPath().getRootIdentifier();
44             final DataObjectModification<VrfEntry> mod = change.getRootNode();
45
46                 switch (mod.getModificationType()) {
47                 case DELETE:
48                     nFibEntries -= 1;
49                     break;
50                 case WRITE:
51                     if (mod.getDataBefore() == null) {
52                         nFibEntries += 1;
53                     } else {
54                         // UPDATE COUNT UNCHANGED
55                     }
56                     break;
57                 default:
58                     throw new IllegalArgumentException("Unhandled modification  type " + mod.getModificationType());
59                 }
60             }
61      }
62
63     public int getDataChgCount() {
64         return nFibEntries;
65     }
66 }