Added copyright and updated appropriate log levels
[vpnservice.git] / mdsalutil / mdsalutil-impl / src / test / java / org / opendaylight / vpnservice / test / MockFlowForwarder.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.vpnservice.test;
9
10 import java.util.Collection;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24
25 public class MockFlowForwarder extends AbstractMockForwardingRulesManager<Flow> {
26
27     private int nFlowCount = 0;
28
29     private ListenerRegistration<MockFlowForwarder> listenerRegistration;
30
31     public MockFlowForwarder( final DataBroker db) {
32         super() ;
33         registerListener(db) ;
34     }
35
36     private void registerListener(final DataBroker db) {
37         final DataTreeIdentifier<Flow> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildCardPath());
38         try {
39             listenerRegistration = db.registerDataTreeChangeListener(treeId, MockFlowForwarder.this);
40         } catch (final Exception e) {
41             throw new IllegalStateException("FlowForwarder registration Listener fail! System needs restart.", e);
42         }
43     }
44
45     private InstanceIdentifier<Flow> getWildCardPath() {
46             return InstanceIdentifier.create(Nodes.class).child(Node.class)
47                     .augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class);
48      }
49
50     @Override
51     public void onDataTreeChanged(Collection<DataTreeModification<Flow>> changes) {
52         for (DataTreeModification<Flow> change : changes) {
53             final InstanceIdentifier<Flow> key = change.getRootPath().getRootIdentifier();
54             final DataObjectModification<Flow> mod = change.getRootNode();
55
56                 switch (mod.getModificationType()) {
57                 case DELETE:
58                     nFlowCount -= 1;
59                     break;
60                 case SUBTREE_MODIFIED:
61                     // CHECK IF RQD
62                     break;
63                 case WRITE:
64                     if (mod.getDataBefore() == null) {
65                         nFlowCount += 1;
66                     } else {
67                         // UPDATE COUNT UNCHANGED
68                     }
69                     break;
70                 default:
71                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
72                 }
73             }
74      }
75
76     public int getDataChgCount() {
77         return nFlowCount;
78     }
79 }