2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.openflowplugin.samples.sample.bundles;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.concurrent.CompletableFuture;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.Future;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.sal.binding.api.AbstractBrokerAwareActivator;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
29 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.group.action._case.GroupActionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.vlan.action._case.PopVlanActionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInput;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInputBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesOutput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleInput;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleInputBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleOutput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.Messages;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.MessagesBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.Message;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.MessageBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleAddFlowCaseBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.BundleAddGroupCaseBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.bundle.add.flow._case.AddFlowCaseDataBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.bundle.inner.message.grouping.bundle.inner.message.bundle.add.group._case.AddGroupCaseDataBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleControlType;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleFlags;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId;
87 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
88 import org.opendaylight.yangtools.yang.common.RpcResult;
89 import org.osgi.framework.BundleContext;
90 import org.slf4j.Logger;
91 import org.slf4j.LoggerFactory;
94 * Sample bundles activator.
96 public class Activator extends AbstractBrokerAwareActivator implements BindingAwareConsumer,
97 ClusteredDataTreeChangeListener<FlowCapableNode>, AutoCloseable {
99 private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
101 private static final BundleId BUNDLE_ID = new BundleId(1L);
102 private static final BundleFlags BUNDLE_FLAGS = new BundleFlags(true, true);
103 private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();
105 private DataBroker dataBroker;
106 private SalBundleService bundleService;
109 public void close() throws Exception {
110 LOG.debug("close() passing");
114 public void onSessionInitialized(ConsumerContext consumerContext) {
115 LOG.debug("inSessionInitialized() passing");
116 dataBroker = consumerContext.getSALService(DataBroker.class);
118 final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
119 .augmentation(FlowCapableNode.class);
120 final DataTreeIdentifier<FlowCapableNode> identifier =
121 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, path);
123 dataBroker.registerDataTreeChangeListener(identifier, Activator.this);
124 bundleService = consumerContext.getRpcService(SalBundleService.class);
128 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<FlowCapableNode>> modifications) {
129 for (DataTreeModification modification : modifications) {
130 if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
131 LOG.info("Node connected: {}",
132 modification.getRootPath().getRootIdentifier().firstIdentifierOf(Node.class));
134 final NodeRef nodeRef =
135 new NodeRef(modification.getRootPath().getRootIdentifier().firstIdentifierOf(Node.class));
137 final ControlBundleInput openBundleInput = new ControlBundleInputBuilder()
139 .setBundleId(BUNDLE_ID)
140 .setFlags(BUNDLE_FLAGS)
141 .setType(BundleControlType.ONFBCTOPENREQUEST)
144 final ControlBundleInput commitBundleInput = new ControlBundleInputBuilder()
146 .setBundleId(BUNDLE_ID)
147 .setFlags(BUNDLE_FLAGS)
148 .setType(BundleControlType.ONFBCTCOMMITREQUEST)
151 final List<Message> innerMessages = createMessages(nodeRef);
152 final Messages messages = new MessagesBuilder().setMessage(innerMessages).build();
153 final AddBundleMessagesInput addBundleMessagesInput = new AddBundleMessagesInputBuilder()
155 .setBundleId(BUNDLE_ID)
156 .setFlags(BUNDLE_FLAGS)
157 .setMessages(messages)
160 makeCompletableFuture(bundleService.controlBundle(openBundleInput))
161 .thenComposeAsync(voidRpcResult -> {
162 LOG.debug("Open successful: {}, msg: {}", voidRpcResult.isSuccessful(),
163 voidRpcResult.getErrors());
165 final CompletableFuture<RpcResult<AddBundleMessagesOutput>> addFuture =
166 makeCompletableFuture(bundleService.addBundleMessages(addBundleMessagesInput));
169 }).thenComposeAsync(voidRpcResult -> {
170 LOG.debug("AddBundleMessages successful: {}, msg: {}", voidRpcResult.isSuccessful(),
171 voidRpcResult.getErrors());
173 final CompletableFuture<RpcResult<ControlBundleOutput>> controlCommitFuture =
174 makeCompletableFuture(bundleService.controlBundle(commitBundleInput));
176 return controlCommitFuture;
177 }).thenAccept(voidRpcResult -> {
178 LOG.debug("Commit successful: {}, msg: {}", voidRpcResult.isSuccessful(),
179 voidRpcResult.getErrors());
185 private static <T> CompletableFuture<T> makeCompletableFuture(Future<T> future) {
186 return CompletableFuture.supplyAsync(() -> {
189 } catch (InterruptedException | ExecutionException e) {
190 throw new RuntimeException(e);
196 protected void onBrokerAvailable(BindingAwareBroker bindingAwareBroker, BundleContext bundleContext) {
197 LOG.debug("onBrokerAvailable() passing");
198 bindingAwareBroker.registerConsumer(this);
201 private static List<Message> createMessages(NodeRef nodeRef) {
202 List<Message> messages = new ArrayList<>();
205 messages.add(new MessageBuilder().setNode(nodeRef).setBundleInnerMessage(new BundleAddGroupCaseBuilder()
206 .setAddGroupCaseData(new AddGroupCaseDataBuilder(createGroup(1L)).build()).build()).build());
208 messages.add(new MessageBuilder().setNode(nodeRef).setBundleInnerMessage(new BundleAddFlowCaseBuilder()
209 .setAddFlowCaseData(new AddFlowCaseDataBuilder(createFlow("42", 1L, 1, (short) 1)).build()).build())
212 messages.add(new MessageBuilder().setNode(nodeRef).setBundleInnerMessage(new BundleAddFlowCaseBuilder()
213 .setAddFlowCaseData(new AddFlowCaseDataBuilder(createFlow("43", 1L, 2, (short) 2)).build()).build())
216 LOG.debug("createMessages() passing {}", messages);
221 private static Flow createFlow(String flowId, long groupId, int priority, short tableId) {
222 MatchBuilder matchBuilder = new MatchBuilder();
223 matchBuilder.setEthernetMatch(new EthernetMatchBuilder()
224 .setEthernetType(new EthernetTypeBuilder()
225 .setType(new EtherType(2048L)).build()).build());
227 FlowBuilder flowBuilder = new FlowBuilder();
228 flowBuilder.setMatch(matchBuilder.build());
229 flowBuilder.setInstructions(createGroupInstructions(groupId).build());
230 flowBuilder.setPriority(priority);
231 flowBuilder.setCookie(new FlowCookie(new BigInteger(flowId + "" + priority)));
233 FlowKey key = new FlowKey(new FlowId(flowId));
234 flowBuilder.setHardTimeout(0);
235 flowBuilder.setIdleTimeout(0);
236 flowBuilder.setStrict(false);
237 flowBuilder.setContainerName(null);
238 flowBuilder.setId(new FlowId(flowId));
239 flowBuilder.setTableId(tableId);
240 flowBuilder.withKey(key);
241 flowBuilder.setFlowName("FlowWithGroupInstruction");
243 return flowBuilder.build();
246 private static Group createGroup(long groupId) {
247 GroupBuilder groupBuilder = new GroupBuilder();
248 GroupKey groupKey = new GroupKey(new GroupId(groupId));
249 groupBuilder.withKey(groupKey);
250 groupBuilder.setGroupId(groupKey.getGroupId());
251 groupBuilder.setBarrier(false);
252 groupBuilder.setGroupName("Foo");
253 groupBuilder.setContainerName(null);
254 groupBuilder.setGroupType(GroupTypes.GroupAll);
255 groupBuilder.setBuckets(createBuckets().build());
257 return groupBuilder.build();
260 private static BucketsBuilder createBuckets() {
261 List<Action> actionList = new ArrayList<>();
263 actionList.add(new ActionBuilder()
265 .setAction(new PopVlanActionCaseBuilder()
266 .setPopVlanAction(new PopVlanActionBuilder().build())
269 BucketBuilder bucketBuilder = new BucketBuilder();
270 bucketBuilder.setBucketId(new BucketId(12L));
271 bucketBuilder.setAction(actionList);
273 List<Bucket> bucketList = new ArrayList<>();
274 bucketList.add(bucketBuilder.build());
276 actionList = new ArrayList<>();
277 SetFieldCaseBuilder setFieldCaseBuilder = new SetFieldCaseBuilder();
278 setFieldCaseBuilder.setSetField(new SetFieldBuilder()
279 .setLayer3Match(new Ipv4MatchBuilder()
280 .setIpv4Source(new Ipv4Prefix("10.0.1.0/32"))
284 actionList.add(new ActionBuilder()
285 .setAction(setFieldCaseBuilder.build())
289 setFieldCaseBuilder = new SetFieldCaseBuilder();
290 setFieldCaseBuilder.setSetField(new SetFieldBuilder()
291 .setLayer3Match(new Ipv4MatchBuilder()
292 .setIpv4Destination(new Ipv4Prefix("10.0.10.0/32"))
296 actionList.add(new ActionBuilder()
297 .setAction(setFieldCaseBuilder.build())
301 bucketBuilder = new BucketBuilder();
302 bucketBuilder.setBucketId(new BucketId(13L));
303 bucketBuilder.setAction(actionList);
305 bucketList.add(bucketBuilder.build());
307 BucketsBuilder bucketsBuilder = new BucketsBuilder();
308 bucketsBuilder.setBucket(bucketList);
310 return bucketsBuilder;
313 private static InstructionsBuilder createGroupInstructions(long groupId) {
314 ActionBuilder actionBuilder = new ActionBuilder();
316 GroupActionBuilder groupActionBuilder = new GroupActionBuilder();
317 groupActionBuilder.setGroupId(groupId);
319 actionBuilder.setAction(new GroupActionCaseBuilder().setGroupAction(groupActionBuilder.build()).build());
320 actionBuilder.setOrder(1);
321 actionBuilder.withKey(new ActionKey(0));
322 List<Action> actionList = new ArrayList<>();
323 actionList.add(actionBuilder.build());
325 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
326 applyActionsBuilder.setAction(actionList);
328 InstructionBuilder instructionBuilder = new InstructionBuilder();
329 instructionBuilder.setInstruction(new ApplyActionsCaseBuilder()
330 .setApplyActions(applyActionsBuilder.build()).build());
331 instructionBuilder.setOrder(0);
332 instructionBuilder.withKey(new InstructionKey(0));
334 InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
335 List<Instruction> instructions = new ArrayList<>();
336 instructions.add(instructionBuilder.build());
337 instructionsBuilder.setInstruction(instructions);
339 return instructionsBuilder;