2 * Copyright (C) 2014 Red Hat, Inc.
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 * Authors : Madhu Venugopal
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
12 import java.util.List;
15 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.ovsdb.lib.notation.Row;
24 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
25 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbConfigurationService;
26 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbConnectionService;
27 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
28 import org.opendaylight.ovsdb.utils.mdsal.node.StringConvertor;
29 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
44 //import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import com.google.common.base.Optional;
53 import com.google.common.base.Preconditions;
54 import com.google.common.collect.Lists;
55 import com.google.common.util.concurrent.CheckedFuture;
58 * Any ServiceInstance class that extends AbstractServiceInstance to be a part of the pipeline
59 * have 2 basic requirements : <br>
60 * 1. Program a default pipeline flow to take any unmatched traffic to the next table in the pipeline. <br>
61 * 2. Get Pipeline Instructions from AbstractServiceInstance (using getMutablePipelineInstructionBuilder) and
62 * use it in any matching flows that needs to be further processed by next service in the pipeline.
65 public abstract class AbstractServiceInstance {
66 public static final String SERVICE_PROPERTY ="serviceProperty";
67 private static final Logger logger = LoggerFactory.getLogger(AbstractServiceInstance.class);
68 public static final String OPENFLOW = "openflow:";
69 // OSGi Services that we are dependent on.
70 /* TODO SB_MIGRATION */
71 private volatile OvsdbConnectionService connectionService;
72 private volatile OvsdbConfigurationService ovsdbConfigService;
73 private volatile MdsalConsumer mdsalConsumer;
74 private volatile PipelineOrchestrator orchestrator;
76 // Concrete Service that this AbstractServiceInstance represent
77 private Service service;
79 public AbstractServiceInstance (Service service) {
80 this.service = service;
83 public boolean isBridgeInPipeline (String nodeId){
84 String bridgeName = getBridgeName(nodeId.split(":")[1]);
85 logger.debug("isBridgeInPipeline: nodeId {} bridgeName {}", nodeId, bridgeName);
86 if (bridgeName != null && Constants.INTEGRATION_BRIDGE.equalsIgnoreCase(bridgeName)) {
92 private String getBridgeName(String nodeId){
93 /* TODO SB_MIGRATION */
94 List<org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node> ovsNodes = connectionService.getNodes();
96 for (org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node ovsNode : ovsNodes) {
97 Map<String, Row> bridges = ovsdbConfigService.getRows(ovsNode, ovsdbConfigService.getTableName(ovsNode, Bridge.class));
98 if (bridges == null) continue;
99 for (String brUuid : bridges.keySet()) {
100 Bridge bridge = ovsdbConfigService.getTypedRow(ovsNode, Bridge.class, bridges.get(brUuid));
102 Set<String> dpids = bridge.getDatapathIdColumn().getData();
103 if (dpids == null || dpids.size() == 0) return null;
104 Long dpid = StringConvertor.dpidStringToLong((String) dpids.toArray()[0]);
105 logger.debug("getBridgeName: bridgeDpid {} ofNodeDpid {}", bridge.getDatapathIdColumn().getData().toArray()[0], nodeId);
106 if (dpid.equals(Long.parseLong(nodeId))){
108 logger.debug("getOvsNode: found ovsNode {} bridge {} for ofNode {}",
109 ovsNode.getNodeId().getValue(), bridge.getName(), nodeId);
110 return bridge.getName();
117 public short getTable() {
118 return service.getTable();
121 public Service getService() {
125 public void setService(Service service) {
126 this.service = service;
129 public NodeBuilder createNodeBuilder(String nodeId) {
130 NodeBuilder builder = new NodeBuilder();
131 builder.setId(new NodeId(nodeId));
132 builder.setKey(new NodeKey(builder.getId()));
136 private static final InstanceIdentifier<Flow> createFlowPath(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
137 return InstanceIdentifier.builder(Nodes.class)
138 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
139 nodeBuilder.getKey())
140 .augmentation(FlowCapableNode.class)
141 .child(Table.class, new TableKey(flowBuilder.getTableId()))
142 .child(Flow.class, flowBuilder.getKey()).build();
145 private static final InstanceIdentifier<Node> createNodePath(NodeBuilder nodeBuilder) {
146 return InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeBuilder.getKey()).build();
150 * This method returns the required Pipeline Instructions to by used by any matching flows that needs
151 * to be further processed by next service in the pipeline.
153 * Important to note that this is a convenience method which returns a mutable instructionBuilder which
154 * needs to be further adjusted by the concrete ServiceInstance class such as setting the Instruction Order, etc.
155 * @return Newly created InstructionBuilder to be used along with other instructions on the main flow
157 protected final InstructionBuilder getMutablePipelineInstructionBuilder() {
158 Service nextService = orchestrator.getNextServiceInPipeline(service);
159 if (nextService != null) {
160 return InstructionUtils.createGotoTableInstructions(new InstructionBuilder(), nextService.getTable());
162 return InstructionUtils.createDropInstructions(new InstructionBuilder());
166 protected void writeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
167 Preconditions.checkNotNull(mdsalConsumer);
168 if (mdsalConsumer == null) {
169 logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
173 DataBroker dataBroker = mdsalConsumer.getDataBroker();
174 if (dataBroker == null) {
175 logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
179 ReadWriteTransaction modification = dataBroker.newReadWriteTransaction();
180 modification.put(LogicalDatastoreType.CONFIGURATION, createNodePath(nodeBuilder),
181 nodeBuilder.build(), true /*createMissingParents*/);
182 modification.put(LogicalDatastoreType.CONFIGURATION, createFlowPath(flowBuilder, nodeBuilder),
183 flowBuilder.build(), true /*createMissingParents*/);
185 CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
187 commitFuture.get(); // TODO: Make it async (See bug 1362)
188 logger.debug("Transaction success for write of Flow "+flowBuilder.getFlowName());
190 } catch (Exception e) {
191 logger.error(e.getMessage(), e);
192 modification.cancel();
196 protected void removeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
197 Preconditions.checkNotNull(mdsalConsumer);
198 if (mdsalConsumer == null) {
199 logger.error("ERROR finding MDSAL Service.");
203 DataBroker dataBroker = mdsalConsumer.getDataBroker();
204 if (dataBroker == null) {
205 logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
209 WriteTransaction modification = dataBroker.newWriteOnlyTransaction();
210 modification.delete(LogicalDatastoreType.CONFIGURATION, createFlowPath(flowBuilder, nodeBuilder));
212 CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
214 commitFuture.get(); // TODO: Make it async (See bug 1362)
215 logger.debug("Transaction success for deletion of Flow "+flowBuilder.getFlowName());
216 } catch (Exception e) {
217 logger.error(e.getMessage(), e);
218 modification.cancel();
222 public Flow getFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
223 Preconditions.checkNotNull(mdsalConsumer);
224 if (mdsalConsumer == null) {
225 logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
229 DataBroker dataBroker = mdsalConsumer.getDataBroker();
230 if (dataBroker == null) {
231 logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
235 ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
237 Optional<Flow> data =
238 readTx.read(LogicalDatastoreType.CONFIGURATION, createFlowPath(flowBuilder, nodeBuilder)).get();
239 if (data.isPresent()) {
242 } catch (InterruptedException|ExecutionException e) {
243 logger.error(e.getMessage(), e);
246 logger.debug("Cannot find data for Flow " + flowBuilder.getFlowName());
251 * Program Default Pipeline Flow.
253 * @param nodeId Node on which the default pipeline flow is programmed.
255 protected void programDefaultPipelineRule(String nodeId) {
256 if (!isBridgeInPipeline(nodeId)) {
257 logger.debug("Bridge {} is not in pipeline", nodeId);
260 MatchBuilder matchBuilder = new MatchBuilder();
261 FlowBuilder flowBuilder = new FlowBuilder();
262 NodeBuilder nodeBuilder = createNodeBuilder(nodeId);
264 // Create the OF Actions and Instructions
265 InstructionsBuilder isb = new InstructionsBuilder();
267 // Instructions List Stores Individual Instructions
268 List<Instruction> instructions = Lists.newArrayList();
270 // Call the InstructionBuilder Methods Containing Actions
271 InstructionBuilder ib = this.getMutablePipelineInstructionBuilder();
273 ib.setKey(new InstructionKey(0));
274 instructions.add(ib.build());
276 // Add InstructionBuilder to the Instruction(s)Builder List
277 isb.setInstruction(instructions);
279 // Add InstructionsBuilder to FlowBuilder
280 flowBuilder.setInstructions(isb.build());
282 String flowId = "DEFAULT_PIPELINE_FLOW_"+service.getTable();
283 flowBuilder.setId(new FlowId(flowId));
284 FlowKey key = new FlowKey(new FlowId(flowId));
285 flowBuilder.setMatch(matchBuilder.build());
286 flowBuilder.setPriority(0);
287 flowBuilder.setBarrier(true);
288 flowBuilder.setTableId(service.getTable());
289 flowBuilder.setKey(key);
290 flowBuilder.setFlowName(flowId);
291 flowBuilder.setHardTimeout(0);
292 flowBuilder.setIdleTimeout(0);
293 writeFlow(flowBuilder, nodeBuilder);