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.plugin.api.OvsdbConfigurationService;
26 import org.opendaylight.ovsdb.plugin.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.opendaylight.inventory.rev130819.nodes.Node;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import com.google.common.base.Optional;
52 import com.google.common.base.Preconditions;
53 import com.google.common.collect.Lists;
54 import com.google.common.util.concurrent.CheckedFuture;
57 * Any ServiceInstance class that extends AbstractServiceInstance to be a part of the pipeline
58 * have 2 basic requirements : <br>
59 * 1. Program a default pipeline flow to take any unmatched traffic to the next table in the pipeline. <br>
60 * 2. Get Pipeline Instructions from AbstractServiceInstance (using getMutablePipelineInstructionBuilder) and
61 * use it in any matching flows that needs to be further processed by next service in the pipeline.
64 public abstract class AbstractServiceInstance {
65 public static final String SERVICE_PROPERTY ="serviceProperty";
66 private static final Logger logger = LoggerFactory.getLogger(AbstractServiceInstance.class);
67 public static final String OPENFLOW = "openflow:";
68 // OSGi Services that we are dependent on.
69 private volatile MdsalConsumer mdsalConsumer;
70 private volatile PipelineOrchestrator orchestrator;
71 private volatile OvsdbConfigurationService ovsdbConfigService;
72 private volatile OvsdbConnectionService connectionService;
74 // Concrete Service that this AbstractServiceInstance represent
75 private Service service;
77 public AbstractServiceInstance (Service service) {
78 this.service = service;
81 public boolean isBridgeInPipeline (String nodeId){
82 String bridgeName = getBridgeName(nodeId.split(":")[1]);
83 logger.debug("isBridgeInPipeline: nodeId {} bridgeName {}", nodeId, bridgeName);
84 if (bridgeName != null && Constants.INTEGRATION_BRIDGE.equalsIgnoreCase(bridgeName)) {
90 private String getBridgeName(String nodeId){
91 List<Node> ovsNodes = connectionService.getNodes();
93 for (Node ovsNode : ovsNodes) {
94 Map<String, Row> bridges = ovsdbConfigService.getRows(ovsNode, ovsdbConfigService.getTableName(ovsNode, Bridge.class));
95 if (bridges == null) continue;
96 for (String brUuid : bridges.keySet()) {
97 Bridge bridge = ovsdbConfigService.getTypedRow(ovsNode, Bridge.class, bridges.get(brUuid));
99 Set<String> dpids = bridge.getDatapathIdColumn().getData();
100 if (dpids == null || dpids.size() == 0) return null;
101 Long dpid = StringConvertor.dpidStringToLong((String) dpids.toArray()[0]);
102 logger.debug("getBridgeName: bridgeDpid {} ofNodeDpid {}", bridge.getDatapathIdColumn().getData().toArray()[0], nodeId);
103 if (dpid.equals(Long.parseLong(nodeId))){
105 logger.debug("getOvsNode: found ovsNode {} bridge {} for ofNode {}",
106 ovsNode.getId().getValue(), bridge.getName(), nodeId);
107 return bridge.getName();
114 public short getTable() {
115 return service.getTable();
118 public Service getService() {
122 public void setService(Service service) {
123 this.service = service;
126 public NodeBuilder createNodeBuilder(String nodeId) {
127 NodeBuilder builder = new NodeBuilder();
128 builder.setId(new NodeId(nodeId));
129 builder.setKey(new NodeKey(builder.getId()));
133 private static final InstanceIdentifier<Flow> createFlowPath(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
134 return InstanceIdentifier.builder(Nodes.class)
135 .child(Node.class, nodeBuilder.getKey())
136 .augmentation(FlowCapableNode.class)
137 .child(Table.class, new TableKey(flowBuilder.getTableId()))
138 .child(Flow.class, flowBuilder.getKey()).build();
141 private static final InstanceIdentifier<Node> createNodePath(NodeBuilder nodeBuilder) {
142 return InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeBuilder.getKey()).build();
146 * This method returns the required Pipeline Instructions to by used by any matching flows that needs
147 * to be further processed by next service in the pipeline.
149 * Important to note that this is a convenience method which returns a mutable instructionBuilder which
150 * needs to be further adjusted by the concrete ServiceInstance class such as setting the Instruction Order, etc.
151 * @return Newly created InstructionBuilder to be used along with other instructions on the main flow
153 protected final InstructionBuilder getMutablePipelineInstructionBuilder() {
154 Service nextService = orchestrator.getNextServiceInPipeline(service);
155 if (nextService != null) {
156 return InstructionUtils.createGotoTableInstructions(new InstructionBuilder(), nextService.getTable());
158 return InstructionUtils.createDropInstructions(new InstructionBuilder());
162 protected void writeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
163 Preconditions.checkNotNull(mdsalConsumer);
164 if (mdsalConsumer == null) {
165 logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
169 DataBroker dataBroker = mdsalConsumer.getDataBroker();
170 if (dataBroker == null) {
171 logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
175 ReadWriteTransaction modification = dataBroker.newReadWriteTransaction();
176 modification.put(LogicalDatastoreType.CONFIGURATION, createNodePath(nodeBuilder),
177 nodeBuilder.build(), true /*createMissingParents*/);
178 modification.put(LogicalDatastoreType.CONFIGURATION, createFlowPath(flowBuilder, nodeBuilder),
179 flowBuilder.build(), true /*createMissingParents*/);
181 CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
183 commitFuture.get(); // TODO: Make it async (See bug 1362)
184 logger.debug("Transaction success for write of Flow "+flowBuilder.getFlowName());
186 } catch (Exception e) {
187 logger.error(e.getMessage(), e);
188 modification.cancel();
192 protected void removeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
193 Preconditions.checkNotNull(mdsalConsumer);
194 if (mdsalConsumer == null) {
195 logger.error("ERROR finding MDSAL Service.");
199 DataBroker dataBroker = mdsalConsumer.getDataBroker();
200 if (dataBroker == null) {
201 logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
205 WriteTransaction modification = dataBroker.newWriteOnlyTransaction();
206 modification.delete(LogicalDatastoreType.CONFIGURATION, createFlowPath(flowBuilder, nodeBuilder));
208 CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
210 commitFuture.get(); // TODO: Make it async (See bug 1362)
211 logger.debug("Transaction success for deletion of Flow "+flowBuilder.getFlowName());
212 } catch (Exception e) {
213 logger.error(e.getMessage(), e);
214 modification.cancel();
218 public Flow getFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
219 Preconditions.checkNotNull(mdsalConsumer);
220 if (mdsalConsumer == null) {
221 logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
225 DataBroker dataBroker = mdsalConsumer.getDataBroker();
226 if (dataBroker == null) {
227 logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
231 ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
233 Optional<Flow> data =
234 readTx.read(LogicalDatastoreType.CONFIGURATION, createFlowPath(flowBuilder, nodeBuilder)).get();
235 if (data.isPresent()) {
238 } catch (InterruptedException|ExecutionException e) {
239 logger.error(e.getMessage(), e);
242 logger.debug("Cannot find data for Flow " + flowBuilder.getFlowName());
247 * Program Default Pipeline Flow.
249 * @param nodeId Node on which the default pipeline flow is programmed.
251 protected void programDefaultPipelineRule(String nodeId) {
252 if (!isBridgeInPipeline(nodeId)) {
253 logger.debug("Bridge {} is not in pipeline", nodeId);
256 MatchBuilder matchBuilder = new MatchBuilder();
257 FlowBuilder flowBuilder = new FlowBuilder();
258 NodeBuilder nodeBuilder = createNodeBuilder(nodeId);
260 // Create the OF Actions and Instructions
261 InstructionsBuilder isb = new InstructionsBuilder();
263 // Instructions List Stores Individual Instructions
264 List<Instruction> instructions = Lists.newArrayList();
266 // Call the InstructionBuilder Methods Containing Actions
267 InstructionBuilder ib = this.getMutablePipelineInstructionBuilder();
269 ib.setKey(new InstructionKey(0));
270 instructions.add(ib.build());
272 // Add InstructionBuilder to the Instruction(s)Builder List
273 isb.setInstruction(instructions);
275 // Add InstructionsBuilder to FlowBuilder
276 flowBuilder.setInstructions(isb.build());
278 String flowId = "DEFAULT_PIPELINE_FLOW_"+service.getTable();
279 flowBuilder.setId(new FlowId(flowId));
280 FlowKey key = new FlowKey(new FlowId(flowId));
281 flowBuilder.setMatch(matchBuilder.build());
282 flowBuilder.setPriority(0);
283 flowBuilder.setBarrier(true);
284 flowBuilder.setTableId(service.getTable());
285 flowBuilder.setKey(key);
286 flowBuilder.setFlowName(flowId);
287 flowBuilder.setHardTimeout(0);
288 flowBuilder.setIdleTimeout(0);
289 writeFlow(flowBuilder, nodeBuilder);