BUG-2637: migration consequence - fix unit test
[controller.git] / opendaylight / md-sal / forwardingrules-manager / src / main / java / org / opendaylight / controller / frm / impl / FlowForwarder.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.frm.impl;
9
10 import org.opendaylight.controller.frm.ForwardingRulesManager;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.common.base.Preconditions;
36
37 /**
38  * GroupForwarder
39  * It implements {@link org.opendaylight.controller.md.sal.binding.api.DataChangeListener}}
40  * for WildCardedPath to {@link Flow} and ForwardingRulesCommiter interface for methods:
41  *  add, update and remove {@link Flow} processing for
42  *  {@link org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent}.
43  *
44  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
45  *
46  */
47 public class FlowForwarder extends AbstractListeningCommiter<Flow> {
48
49     private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class);
50
51     private ListenerRegistration<DataChangeListener> listenerRegistration;
52
53     public FlowForwarder (final ForwardingRulesManager manager, final DataBroker db) {
54         super(manager, Flow.class);
55         Preconditions.checkNotNull(db, "DataBroker can not be null!");
56         registrationListener(db, 5);
57     }
58
59     private void registrationListener(final DataBroker db, int i) {
60         try {
61             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
62                     getWildCardPath(), FlowForwarder.this, DataChangeScope.SUBTREE);
63         } catch (final Exception e) {
64             if (i >= 1) {
65                 try {
66                     Thread.sleep(100);
67                 } catch (InterruptedException e1) {
68                     LOG.error("Thread interrupted '{}'", e1);
69                     Thread.currentThread().interrupt();
70                 }
71                 registrationListener(db, --i);
72             } else {
73                 LOG.error("FRM Flow DataChange listener registration fail!", e);
74                 throw new IllegalStateException("FlowForwarder registration Listener fail! System needs restart.", e);
75             }
76         }
77     }
78
79     @Override
80     public void close() {
81         if (listenerRegistration != null) {
82             try {
83                 listenerRegistration.close();
84             } catch (final Exception e) {
85                 LOG.error("Error by stop FRM FlowChangeListener.", e);
86             }
87             listenerRegistration = null;
88         }
89     }
90
91     @Override
92     public void remove(final InstanceIdentifier<Flow> identifier,
93                        final Flow removeDataObj,
94                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
95
96         final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
97         if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
98             final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
99             builder.setFlowRef(new FlowRef(identifier));
100             builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
101             builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
102
103             // This method is called only when a given flow object has been
104             // removed from datastore. So FRM always needs to set strict flag
105             // into remove-flow input so that only a flow entry associated with
106             // a given flow object is removed.
107             builder.setTransactionUri(new Uri(provider.getNewTransactionId())).
108                 setStrict(Boolean.TRUE);
109             provider.getSalFlowService().removeFlow(builder.build());
110         }
111     }
112
113     @Override
114     public void update(final InstanceIdentifier<Flow> identifier,
115                        final Flow original, final Flow update,
116                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
117
118         final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
119         if (tableIdValidationPrecondition(tableKey, update)) {
120             final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder();
121
122             builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
123             builder.setFlowRef(new FlowRef(identifier));
124             builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
125
126             // This method is called only when a given flow object in datastore
127             // has been updated. So FRM always needs to set strict flag into
128             // update-flow input so that only a flow entry associated with
129             // a given flow object is updated.
130             builder.setUpdatedFlow((new UpdatedFlowBuilder(update)).setStrict(Boolean.TRUE).build());
131             builder.setOriginalFlow((new OriginalFlowBuilder(original)).setStrict(Boolean.TRUE).build());
132
133             provider.getSalFlowService().updateFlow(builder.build());
134         }
135     }
136
137     @Override
138     public void add(final InstanceIdentifier<Flow> identifier,
139                     final Flow addDataObj,
140                     final InstanceIdentifier<FlowCapableNode> nodeIdent) {
141
142         final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
143         if (tableIdValidationPrecondition(tableKey, addDataObj)) {
144             final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj);
145
146             builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
147             builder.setFlowRef(new FlowRef(identifier));
148             builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
149             builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
150             provider.getSalFlowService().addFlow(builder.build());
151         }
152     }
153
154     @Override
155     protected InstanceIdentifier<Flow> getWildCardPath() {
156         return InstanceIdentifier.create(Nodes.class).child(Node.class)
157                 .augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class);
158     }
159
160     private boolean tableIdValidationPrecondition (final TableKey tableKey, final Flow flow) {
161         Preconditions.checkNotNull(tableKey, "TableKey can not be null or empty!");
162         Preconditions.checkNotNull(flow, "Flow can not be null or empty!");
163         if (! tableKey.getId().equals(flow.getTableId())) {
164             LOG.error("TableID in URI tableId={} and in palyload tableId={} is not same.",
165                     flow.getTableId(), tableKey.getId());
166             return false;
167         }
168         return true;
169     }
170 }
171