Bug 1760 - Removing redudant call to writeFlow in L3ForwardingService
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / L3ForwardingService.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
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
7  *
8  * Authors : Dave Tucker
9  */
10
11 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
12
13 import java.math.BigInteger;
14 import java.net.InetAddress;
15 import java.util.List;
16
17 import org.opendaylight.controller.sal.core.Node;
18 import org.opendaylight.controller.sal.utils.Status;
19 import org.opendaylight.controller.sal.utils.StatusCode;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.L3ForwardingProvider;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
25 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
26 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
27 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
39
40 import com.google.common.collect.Lists;
41
42 public class L3ForwardingService extends AbstractServiceInstance implements L3ForwardingProvider {
43     public L3ForwardingService() {
44         super(Service.L3_FORWARDING);
45     }
46
47     public L3ForwardingService(Service service) {
48         super(service);
49     }
50
51     @Override
52     public boolean isBridgeInPipeline (String nodeId) {
53         return true;
54     }
55
56     @Override
57     public Status programForwardingTableEntry(Node node, Long dpid, String segmentationId, InetAddress ipAddress,
58                                               String macAddress, Action action) {
59         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
60
61         MatchBuilder matchBuilder = new MatchBuilder();
62         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
63
64         // Instructions List Stores Individual Instructions
65         InstructionsBuilder isb = new InstructionsBuilder();
66         List<Instruction> instructions = Lists.newArrayList();
67         InstructionBuilder ib = new InstructionBuilder();
68
69         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
70         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(ipAddress.getHostAddress()));
71
72         // Set Dest Mac address
73         InstructionUtils.createDlDstInstructions(ib, new MacAddress(macAddress));
74         ib.setOrder(0);
75         ib.setKey(new InstructionKey(0));
76         instructions.add(ib.build());
77
78         // Goto Next Table
79         ib = getMutablePipelineInstructionBuilder();
80         ib.setOrder(1);
81         ib.setKey(new InstructionKey(1));
82         instructions.add(ib.build());
83
84         FlowBuilder flowBuilder = new FlowBuilder();
85         flowBuilder.setMatch(matchBuilder.build());
86         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
87
88         String flowId = "L3Forwarding_" + ipAddress.getHostAddress();
89         flowBuilder.setId(new FlowId(flowId));
90         FlowKey key = new FlowKey(new FlowId(flowId));
91         flowBuilder.setBarrier(true);
92         flowBuilder.setTableId(this.getTable());
93         flowBuilder.setKey(key);
94         flowBuilder.setPriority(1024);
95         flowBuilder.setFlowName(flowId);
96         flowBuilder.setHardTimeout(0);
97         flowBuilder.setIdleTimeout(0);
98
99         if (action.equals(Action.ADD)) {
100             writeFlow(flowBuilder, nodeBuilder);
101         } else {
102             removeFlow(flowBuilder, nodeBuilder);
103         }
104
105         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
106         return new Status(StatusCode.SUCCESS);
107     }
108 }