2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.frm.impl;
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.controller.frm.ForwardingRulesManager;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
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.TableKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
38 * It implements {@link org.opendaylight.controller.md.sal.binding.api.DataChangeListener}}
39 * for WildCardedPath to {@link Flow} and ForwardingRulesCommiter interface for methods:
40 * add, update and remove {@link Flow} processing for
41 * {@link org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent}.
43 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
46 public class FlowForwarder extends AbstractListeningCommiter<Flow> {
48 private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class);
50 private ListenerRegistration<DataChangeListener> listenerRegistration;
52 public FlowForwarder (final ForwardingRulesManager manager, final DataBroker db) {
53 super(manager, Flow.class);
54 Preconditions.checkNotNull(db, "DataBroker can not be null!");
55 this.listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
56 getWildCardPath(), FlowForwarder.this, DataChangeScope.SUBTREE);
61 if (listenerRegistration != null) {
63 listenerRegistration.close();
64 } catch (Exception e) {
65 LOG.error("Error by stop FRM FlowChangeListener.", e);
67 listenerRegistration = null;
72 public void remove(final InstanceIdentifier<Flow> identifier,
73 final Flow removeDataObj,
74 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
76 final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
77 if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
78 final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
79 builder.setFlowRef(new FlowRef(identifier));
80 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
81 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
82 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
83 this.provider.getSalFlowService().removeFlow(builder.build());
88 public void update(final InstanceIdentifier<Flow> identifier,
89 final Flow original, final Flow update,
90 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
92 final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
93 if (tableIdValidationPrecondition(tableKey, update)) {
94 final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder();
96 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
97 builder.setFlowRef(new FlowRef(identifier));
98 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
99 builder.setUpdatedFlow((new UpdatedFlowBuilder(update)).build());
100 builder.setOriginalFlow((new OriginalFlowBuilder(original)).build());
102 this.provider.getSalFlowService().updateFlow(builder.build());
107 public void add(final InstanceIdentifier<Flow> identifier,
108 final Flow addDataObj,
109 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
111 final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
112 if (tableIdValidationPrecondition(tableKey, addDataObj)) {
113 final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj);
115 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
116 builder.setFlowRef(new FlowRef(identifier));
117 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
118 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
119 this.provider.getSalFlowService().addFlow(builder.build());
124 protected InstanceIdentifier<Flow> getWildCardPath() {
125 return InstanceIdentifier.create(Nodes.class).child(Node.class)
126 .augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class);
129 private boolean tableIdValidationPrecondition (final TableKey tableKey, final Flow flow) {
130 Preconditions.checkNotNull(tableKey, "TableKey can not be null or empty!");
131 Preconditions.checkNotNull(flow, "Flow can not be null or empty!");
132 if (flow.getTableId() != tableKey.getId()) {
133 LOG.error("TableID in URI tableId={} and in palyload tableId={} is not same.",
134 flow.getTableId(), tableKey.getId());