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.opendaylight.inventory.rev130819.nodes.NodeBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
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;
84 logger.info(">>>>> init {}", this.getClass());
87 private String getBridgeName(Node node) {
88 return (node.getAugmentation(OvsdbBridgeAugmentation.class).getBridgeName().getValue());
91 public boolean isBridgeInPipeline (Node node){
92 String bridgeName = getBridgeName(node);
93 logger.debug("isBridgeInPipeline: node {} bridgeName {}", node, bridgeName);
94 if (bridgeName != null && Constants.INTEGRATION_BRIDGE.equalsIgnoreCase(bridgeName)) {
100 private String getBridgeName(String nodeId){
101 /* TODO SB_MIGRATION */
102 List<org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node> ovsNodes = connectionService.getNodes();
104 for (org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node ovsNode : ovsNodes) {
105 Map<String, Row> bridges = ovsdbConfigService.getRows(ovsNode, ovsdbConfigService.getTableName(ovsNode, Bridge.class));
106 if (bridges == null) continue;
107 for (String brUuid : bridges.keySet()) {
108 Bridge bridge = ovsdbConfigService.getTypedRow(ovsNode, Bridge.class, bridges.get(brUuid));
110 Set<String> dpids = bridge.getDatapathIdColumn().getData();
111 if (dpids == null || dpids.size() == 0) return null;
112 Long dpid = StringConvertor.dpidStringToLong((String) dpids.toArray()[0]);
113 logger.debug("getBridgeName: bridgeDpid {} ofNodeDpid {}", bridge.getDatapathIdColumn().getData().toArray()[0], nodeId);
114 if (dpid.equals(Long.parseLong(nodeId))){
116 logger.debug("getOvsNode: found ovsNode {} bridge {} for ofNode {}",
117 ovsNode.getNodeId().getValue(), bridge.getName(), nodeId);
118 return bridge.getName();
125 public short getTable() {
126 return service.getTable();
129 public Service getService() {
133 public void setService(Service service) {
134 this.service = service;
137 public NodeBuilder createNodeBuilder(String nodeId) {
138 NodeBuilder builder = new NodeBuilder();
139 builder.setId(new NodeId(nodeId));
140 builder.setKey(new NodeKey(builder.getId()));
144 private static final InstanceIdentifier<Flow> createFlowPath(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
145 return InstanceIdentifier.builder(Nodes.class)
146 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
147 nodeBuilder.getKey())
148 .augmentation(FlowCapableNode.class)
149 .child(Table.class, new TableKey(flowBuilder.getTableId()))
150 .child(Flow.class, flowBuilder.getKey()).build();
154 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node>
155 createNodePath(NodeBuilder nodeBuilder) {
156 return InstanceIdentifier.builder(Nodes.class)
157 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
158 nodeBuilder.getKey()).build();
162 * This method returns the required Pipeline Instructions to by used by any matching flows that needs
163 * to be further processed by next service in the pipeline.
165 * Important to note that this is a convenience method which returns a mutable instructionBuilder which
166 * needs to be further adjusted by the concrete ServiceInstance class such as setting the Instruction Order, etc.
167 * @return Newly created InstructionBuilder to be used along with other instructions on the main flow
169 protected final InstructionBuilder getMutablePipelineInstructionBuilder() {
170 Service nextService = orchestrator.getNextServiceInPipeline(service);
171 if (nextService != null) {
172 return InstructionUtils.createGotoTableInstructions(new InstructionBuilder(), nextService.getTable());
174 return InstructionUtils.createDropInstructions(new InstructionBuilder());
178 protected void writeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
179 Preconditions.checkNotNull(mdsalConsumer);
180 if (mdsalConsumer == null) {
181 logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
185 DataBroker dataBroker = mdsalConsumer.getDataBroker();
186 if (dataBroker == null) {
187 logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
191 ReadWriteTransaction modification = dataBroker.newReadWriteTransaction();
192 modification.put(LogicalDatastoreType.CONFIGURATION, createNodePath(nodeBuilder),
193 nodeBuilder.build(), true /*createMissingParents*/);
194 modification.put(LogicalDatastoreType.CONFIGURATION, createFlowPath(flowBuilder, nodeBuilder),
195 flowBuilder.build(), true /*createMissingParents*/);
197 CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
199 commitFuture.get(); // TODO: Make it async (See bug 1362)
200 logger.debug("Transaction success for write of Flow "+flowBuilder.getFlowName());
202 } catch (Exception e) {
203 logger.error(e.getMessage(), e);
204 modification.cancel();
208 protected void removeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
209 Preconditions.checkNotNull(mdsalConsumer);
210 if (mdsalConsumer == null) {
211 logger.error("ERROR finding MDSAL Service.");
215 DataBroker dataBroker = mdsalConsumer.getDataBroker();
216 if (dataBroker == null) {
217 logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
221 WriteTransaction modification = dataBroker.newWriteOnlyTransaction();
222 modification.delete(LogicalDatastoreType.CONFIGURATION, createFlowPath(flowBuilder, nodeBuilder));
224 CheckedFuture<Void, TransactionCommitFailedException> commitFuture = modification.submit();
226 commitFuture.get(); // TODO: Make it async (See bug 1362)
227 logger.debug("Transaction success for deletion of Flow "+flowBuilder.getFlowName());
228 } catch (Exception e) {
229 logger.error(e.getMessage(), e);
230 modification.cancel();
234 public Flow getFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
235 Preconditions.checkNotNull(mdsalConsumer);
236 if (mdsalConsumer == null) {
237 logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
241 DataBroker dataBroker = mdsalConsumer.getDataBroker();
242 if (dataBroker == null) {
243 logger.error("ERROR finding reference for DataBroker. Please check MD-SAL support on the Controller.");
247 ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
249 Optional<Flow> data =
250 readTx.read(LogicalDatastoreType.CONFIGURATION, createFlowPath(flowBuilder, nodeBuilder)).get();
251 if (data.isPresent()) {
254 } catch (InterruptedException|ExecutionException e) {
255 logger.error(e.getMessage(), e);
258 logger.debug("Cannot find data for Flow " + flowBuilder.getFlowName());
263 * Program Default Pipeline Flow.
265 * @param nodeId Node on which the default pipeline flow is programmed.
267 protected void programDefaultPipelineRule(Node node) {
268 if (!isBridgeInPipeline(node)) {
269 logger.debug("Bridge {} is not in pipeline", node);
272 MatchBuilder matchBuilder = new MatchBuilder();
273 FlowBuilder flowBuilder = new FlowBuilder();
274 NodeBuilder nodeBuilder = createNodeBuilder(node.getNodeId().getValue());
276 // Create the OF Actions and Instructions
277 InstructionsBuilder isb = new InstructionsBuilder();
279 // Instructions List Stores Individual Instructions
280 List<Instruction> instructions = Lists.newArrayList();
282 // Call the InstructionBuilder Methods Containing Actions
283 InstructionBuilder ib = this.getMutablePipelineInstructionBuilder();
285 ib.setKey(new InstructionKey(0));
286 instructions.add(ib.build());
288 // Add InstructionBuilder to the Instruction(s)Builder List
289 isb.setInstruction(instructions);
291 // Add InstructionsBuilder to FlowBuilder
292 flowBuilder.setInstructions(isb.build());
294 String flowId = "DEFAULT_PIPELINE_FLOW_"+service.getTable();
295 flowBuilder.setId(new FlowId(flowId));
296 FlowKey key = new FlowKey(new FlowId(flowId));
297 flowBuilder.setMatch(matchBuilder.build());
298 flowBuilder.setPriority(0);
299 flowBuilder.setBarrier(true);
300 flowBuilder.setTableId(service.getTable());
301 flowBuilder.setKey(key);
302 flowBuilder.setFlowName(flowId);
303 flowBuilder.setHardTimeout(0);
304 flowBuilder.setIdleTimeout(0);
305 writeFlow(flowBuilder, nodeBuilder);