Merge "BUG-6109: Having a null match field is totally fine."
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / GroupForwarder.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.openflowplugin.applications.frm.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
20 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.group.update.OriginalGroupBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.group.update.UpdatedGroupBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yangtools.concepts.ListenerRegistration;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import java.util.concurrent.Callable;
45 import java.util.concurrent.Future;
46
47 /**
48  * GroupForwarder
49  * It implements {@link org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener}}
50  * for WildCardedPath to {@link Group} and ForwardingRulesCommiter interface for methods:
51  *  add, update and remove {@link Group} processing for
52  *  {@link org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent}.
53  *
54  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
55  *
56  */
57 public class GroupForwarder extends AbstractListeningCommiter<Group> {
58
59     private static final Logger LOG = LoggerFactory.getLogger(GroupForwarder.class);
60
61     private final DataBroker dataBroker;
62
63     private ListenerRegistration<GroupForwarder> listenerRegistration;
64
65     public GroupForwarder (final ForwardingRulesManager manager, final DataBroker db) {
66         super(manager, Group.class);
67         dataBroker = Preconditions.checkNotNull(db, "DataBroker can not be null!");
68         final DataTreeIdentifier<Group> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildCardPath());
69
70         try {
71             SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
72                     ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
73             listenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<GroupForwarder>>() {
74                 @Override
75                 public ListenerRegistration<GroupForwarder> call() throws Exception {
76                     return db.registerDataTreeChangeListener(treeId, GroupForwarder.this);
77                 }
78             });
79         } catch (final Exception e) {
80             LOG.warn("FRM Group DataTreeChange listener registration fail!");
81             LOG.debug("FRM Group DataTreeChange listener registration fail ..", e);
82             throw new IllegalStateException("GroupForwarder startup fail! System needs restart.", e);
83         }
84     }
85
86     @Override
87     public void close() {
88         if (listenerRegistration != null) {
89             try {
90                 listenerRegistration.close();
91             } catch (Exception e) {
92                 LOG.warn("Error by stop FRM GroupChangeListener: {}", e.getMessage());
93                 LOG.debug("Error by stop FRM GroupChangeListener..", e);
94             }
95             listenerRegistration = null;
96         }
97     }
98
99     @Override
100     protected InstanceIdentifier<Group> getWildCardPath() {
101         return InstanceIdentifier.create(Nodes.class).child(Node.class)
102                 .augmentation(FlowCapableNode.class).child(Group.class);
103     }
104
105     @Override
106     public void remove(final InstanceIdentifier<Group> identifier, final Group removeDataObj,
107                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
108
109         final Group group = (removeDataObj);
110         final RemoveGroupInputBuilder builder = new RemoveGroupInputBuilder(group);
111
112         builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
113         builder.setGroupRef(new GroupRef(identifier));
114         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
115         this.provider.getSalGroupService().removeGroup(builder.build());
116     }
117
118     //TODO: Pull this into ForwardingRulesCommiter and override it here
119     @Override
120     public Future<RpcResult<RemoveGroupOutput>> removeWithResult(final InstanceIdentifier<Group> identifier, final Group removeDataObj,
121                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
122
123         final Group group = (removeDataObj);
124         final RemoveGroupInputBuilder builder = new RemoveGroupInputBuilder(group);
125
126         builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
127         builder.setGroupRef(new GroupRef(identifier));
128         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
129         return this.provider.getSalGroupService().removeGroup(builder.build());
130     }
131
132     @Override
133     public void update(final InstanceIdentifier<Group> identifier,
134                        final Group original, final Group update,
135                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
136
137         final Group originalGroup = (original);
138         final Group updatedGroup = (update);
139         final UpdateGroupInputBuilder builder = new UpdateGroupInputBuilder();
140
141         builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
142         builder.setGroupRef(new GroupRef(identifier));
143         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
144         builder.setUpdatedGroup((new UpdatedGroupBuilder(updatedGroup)).build());
145         builder.setOriginalGroup((new OriginalGroupBuilder(originalGroup)).build());
146
147         this.provider.getSalGroupService().updateGroup(builder.build());
148     }
149
150     @Override
151     public void add(final InstanceIdentifier<Group> identifier, final Group addDataObj,
152                     final InstanceIdentifier<FlowCapableNode> nodeIdent) {
153
154         final Group group = (addDataObj);
155         final AddGroupInputBuilder builder = new AddGroupInputBuilder(group);
156
157         builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
158         builder.setGroupRef(new GroupRef(identifier));
159         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
160         this.provider.getSalGroupService().addGroup(builder.build());
161     }
162
163     @Override
164     public void createStaleMarkEntity(InstanceIdentifier<Group> identifier, Group del, InstanceIdentifier<FlowCapableNode> nodeIdent) {
165         LOG.debug("Creating Stale-Mark entry for the switch {} for Group {} ", nodeIdent.toString(), del.toString());
166         StaleGroup staleGroup = makeStaleGroup(identifier, del, nodeIdent);
167         persistStaleGroup(staleGroup, nodeIdent);
168
169     }
170
171
172     private StaleGroup makeStaleGroup(InstanceIdentifier<Group> identifier, Group del, InstanceIdentifier<FlowCapableNode> nodeIdent){
173         StaleGroupBuilder staleGroupBuilder = new StaleGroupBuilder(del);
174         return staleGroupBuilder.setGroupId(del.getGroupId()).build();
175     }
176
177     private void persistStaleGroup(StaleGroup staleGroup, InstanceIdentifier<FlowCapableNode> nodeIdent){
178         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
179         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleGroupInstanceIdentifier(staleGroup, nodeIdent), staleGroup, false);
180
181         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTransaction.submit();
182         handleStaleGroupResultFuture(submitFuture);
183     }
184
185     private void handleStaleGroupResultFuture(CheckedFuture<Void, TransactionCommitFailedException> submitFuture) {
186         Futures.addCallback(submitFuture, new FutureCallback<Void>() {
187             @Override
188             public void onSuccess(Void result) {
189                 LOG.debug("Stale Group creation success");
190             }
191
192             @Override
193             public void onFailure(Throwable t) {
194                 LOG.error("Stale Group creation failed {}", t);
195             }
196         });
197
198     }
199
200     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup> getStaleGroupInstanceIdentifier(StaleGroup staleGroup, InstanceIdentifier<FlowCapableNode> nodeIdent) {
201             return nodeIdent
202                 .child(StaleGroup.class, new StaleGroupKey(new GroupId(staleGroup.getGroupId())));
203     }
204
205 }
206