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