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.sample.l2switch.md.flow;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
13 import org.opendaylight.controller.sample.l2switch.md.topology.NetworkGraphService;
14 import org.opendaylight.controller.sample.l2switch.md.util.InstanceIdentifierUtils;
15 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
16 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
17 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import java.math.BigInteger;
56 import java.util.List;
57 import java.util.concurrent.Future;
58 import java.util.concurrent.atomic.AtomicLong;
61 * Implementation of FlowWriterService{@link org.opendaylight.controller.sample.l2switch.md.flow.FlowWriterService},
62 * that builds required flow and writes to configuration data store using provided DataBrokerService
63 * {@link org.opendaylight.controller.sal.binding.api.data.DataBrokerService}
65 public class FlowWriterServiceImpl implements FlowWriterService {
66 private static final Logger _logger = LoggerFactory.getLogger(FlowWriterServiceImpl.class);
67 private final DataBrokerService dataBrokerService;
68 private final NetworkGraphService networkGraphService;
69 private AtomicLong flowIdInc = new AtomicLong();
70 private AtomicLong flowCookieInc = new AtomicLong(0x2a00000000000000L);
73 public FlowWriterServiceImpl(DataBrokerService dataBrokerService, NetworkGraphService networkGraphService) {
74 Preconditions.checkNotNull(dataBrokerService, "dataBrokerService should not be null.");
75 Preconditions.checkNotNull(networkGraphService, "networkGraphService should not be null.");
76 this.dataBrokerService = dataBrokerService;
77 this.networkGraphService = networkGraphService;
81 * Writes a flow that forwards packets to destPort if destination mac in packet is destMac and
82 * source Mac in packet is sourceMac. If sourceMac is null then flow would not set any source mac,
83 * resulting in all packets with destMac being forwarded to destPort.
87 * @param destNodeConnectorRef
90 public void addMacToMacFlow(MacAddress sourceMac, MacAddress destMac, NodeConnectorRef destNodeConnectorRef) {
92 Preconditions.checkNotNull(destMac, "Destination mac address should not be null.");
93 Preconditions.checkNotNull(destNodeConnectorRef, "Destination port should not be null.");
96 // do not add flow if both macs are same.
97 if(sourceMac != null && destMac.equals(sourceMac)) {
98 _logger.info("In addMacToMacFlow: No flows added. Source and Destination mac are same.");
102 // get flow table key
103 TableKey flowTableKey = new TableKey((short) 0); //TODO: Hard coded Table Id 0, need to get it from Configuration data.
105 //build a flow path based on node connector to program flow
106 InstanceIdentifier<Flow> flowPath = buildFlowPath(destNodeConnectorRef, flowTableKey);
108 // build a flow that target given mac id
109 Flow flowBody = createMacToMacFlow(flowTableKey.getId(), 0, sourceMac, destMac, destNodeConnectorRef);
111 // commit the flow in config data
112 writeFlowToConfigData(flowPath, flowBody);
116 * Writes mac-to-mac flow on all ports that are in the path between given source and destination ports.
117 * It uses path provided by NetworkGraphService
118 * {@link org.opendaylight.controller.sample.l2switch.md.topology.NetworkGraphService} to find a links
119 * {@link org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link}
120 * between given ports. And then writes appropriate flow on each port that is covered in that path.
123 * @param sourceNodeConnectorRef
125 * @param destNodeConnectorRef
128 public void addMacToMacFlowsUsingShortestPath(MacAddress sourceMac,
129 NodeConnectorRef sourceNodeConnectorRef,
131 NodeConnectorRef destNodeConnectorRef) {
132 Preconditions.checkNotNull(sourceMac, "Source mac address should not be null.");
133 Preconditions.checkNotNull(sourceNodeConnectorRef, "Source port should not be null.");
134 Preconditions.checkNotNull(destMac, "Destination mac address should not be null.");
135 Preconditions.checkNotNull(destNodeConnectorRef, "Destination port should not be null.");
137 if(sourceNodeConnectorRef.equals(destNodeConnectorRef)) {
138 _logger.info("In addMacToMacFlowsUsingShortestPath: No flows added. Source and Destination ports are same.");
142 NodeId sourceNodeId = new NodeId(sourceNodeConnectorRef.getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
143 NodeId destNodeId = new NodeId(destNodeConnectorRef.getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
145 // add destMac-To-sourceMac flow on source port
146 addMacToMacFlow(destMac, sourceMac, sourceNodeConnectorRef);
148 // add sourceMac-To-destMac flow on destination port
149 addMacToMacFlow(sourceMac, destMac, destNodeConnectorRef);
151 if(!sourceNodeId.equals(destNodeId)) {
152 List<Link> linksInBeween = networkGraphService.getPath(sourceNodeId, destNodeId);
154 if(linksInBeween != null) {
155 // assumes the list order is maintained and starts with link that has source as source node
156 for(Link link : linksInBeween) {
157 // add sourceMac-To-destMac flow on source port
158 addMacToMacFlow(sourceMac, destMac, getSourceNodeConnectorRef(link));
160 // add destMac-To-sourceMac flow on destination port
161 addMacToMacFlow(destMac, sourceMac, getDestNodeConnectorRef(link));
167 private NodeConnectorRef getSourceNodeConnectorRef(Link link) {
168 InstanceIdentifier<NodeConnector> nodeConnectorInstanceIdentifier
169 = InstanceIdentifierUtils.createNodeConnectorIdentifier(
170 link.getSource().getSourceNode().getValue(),
171 link.getSource().getSourceTp().getValue());
172 return new NodeConnectorRef(nodeConnectorInstanceIdentifier);
175 private NodeConnectorRef getDestNodeConnectorRef(Link link) {
176 InstanceIdentifier<NodeConnector> nodeConnectorInstanceIdentifier
177 = InstanceIdentifierUtils.createNodeConnectorIdentifier(
178 link.getDestination().getDestNode().getValue(),
179 link.getDestination().getDestTp().getValue());
181 return new NodeConnectorRef(nodeConnectorInstanceIdentifier);
185 * @param nodeConnectorRef
188 private InstanceIdentifier<Flow> buildFlowPath(NodeConnectorRef nodeConnectorRef, TableKey flowTableKey) {
190 // generate unique flow key
191 FlowId flowId = new FlowId(String.valueOf(flowIdInc.getAndIncrement()));
192 FlowKey flowKey = new FlowKey(flowId);
194 return InstanceIdentifierUtils.generateFlowInstanceIdentifier(nodeConnectorRef, flowTableKey, flowKey);
203 * @return {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder}
204 * builds flow that forwards all packets with destMac to given port
206 private Flow createMacToMacFlow(Short tableId, int priority,
207 MacAddress sourceMac, MacAddress destMac, NodeConnectorRef destPort) {
209 // start building flow
210 FlowBuilder macToMacFlow = new FlowBuilder() //
211 .setTableId(tableId) //
212 .setFlowName("mac2mac");
214 // use its own hash code for id.
215 macToMacFlow.setId(new FlowId(Long.toString(macToMacFlow.hashCode())));
217 // create a match that has mac to mac ethernet match
218 EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder() //
219 .setEthernetDestination(new EthernetDestinationBuilder() //
220 .setAddress(destMac) //
222 // set source in the match only if present
223 if(sourceMac != null) {
224 ethernetMatchBuilder.setEthernetSource(new EthernetSourceBuilder()
225 .setAddress(sourceMac)
228 EthernetMatch ethernetMatch = ethernetMatchBuilder.build();
229 Match match = new MatchBuilder()
230 .setEthernetMatch(ethernetMatch)
234 Uri destPortUri = destPort.getValue().firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId();
236 Action outputToControllerAction = new ActionBuilder() //
238 .setAction(new OutputActionCaseBuilder() //
239 .setOutputAction(new OutputActionBuilder() //
240 .setMaxLength(new Integer(0xffff)) //
241 .setOutputNodeConnector(destPortUri) //
246 // Create an Apply Action
247 ApplyActions applyActions = new ApplyActionsBuilder().setAction(ImmutableList.of(outputToControllerAction))
250 // Wrap our Apply Action in an Instruction
251 Instruction applyActionsInstruction = new InstructionBuilder() //
253 .setInstruction(new ApplyActionsCaseBuilder()//
254 .setApplyActions(applyActions) //
258 // Put our Instruction in a list of Instructions
261 .setInstructions(new InstructionsBuilder() //
262 .setInstruction(ImmutableList.of(applyActionsInstruction)) //
264 .setPriority(priority) //
266 .setHardTimeout(0) //
267 .setIdleTimeout(0) //
268 .setCookie(new FlowCookie(BigInteger.valueOf(flowCookieInc.getAndIncrement())))
269 .setFlags(new FlowModFlags(false, false, false, false, false));
271 return macToMacFlow.build();
275 * Starts and commits data change transaction which
276 * modifies provided flow path with supplied body.
280 * @return transaction commit
282 private Future<RpcResult<TransactionStatus>> writeFlowToConfigData(InstanceIdentifier<Flow> flowPath,
284 DataModificationTransaction addFlowTransaction = dataBrokerService.beginTransaction();
285 addFlowTransaction.putConfigurationData(flowPath, flowBody);
286 return addFlowTransaction.commit();