Merge "Tests for neutron-ovsdb"
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / mapper / source / SourceMapperFlows.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.source;
10
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
13 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowIdUtils;
14 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
15 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTable;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
31
32 import java.math.BigInteger;
33
34 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.*;
35
36 public class SourceMapperFlows {
37
38     private final NodeId nodeId;
39     private final short tableId;
40
41     public SourceMapperFlows(NodeId nodeId, short tableId) {
42         this.nodeId = Preconditions.checkNotNull(nodeId);
43         this.tableId = tableId;
44     }
45
46     /**
47      * Default flow which drops all traffic
48      *
49      * @param priority  of flow in the table
50      * @param etherType can be set as specific protocol to match
51      * @param ofWriter  flow writer
52      */
53     void dropFlow(int priority, Long etherType, OfWriter ofWriter) {
54         FlowId flowId;
55         FlowBuilder flowBuilder = FlowUtils.base(tableId)
56                 .setPriority(priority)
57                 .setInstructions(FlowUtils.dropInstructions());
58         if (etherType != null) {
59             MatchBuilder matchBuilder = new MatchBuilder()
60                     .setEthernetMatch(FlowUtils.ethernetMatch(null, null, etherType));
61             Match match = matchBuilder.build();
62             flowId = FlowIdUtils.newFlowId(tableId, "drop", match);
63             flowBuilder.setMatch(match);
64         } else {
65             flowId = FlowIdUtils.newFlowId("dropAll");
66         }
67         flowBuilder.setId(flowId);
68         ofWriter.writeFlow(nodeId, tableId, flowBuilder.build());
69     }
70
71     /**
72      * Load endpoint ordinals to registers and set tunnel ID value (when exists). Traffic is redirected to destination
73      * mapper
74      *
75      * @param goToTable       table ID for {@link GoToTable} instruction
76      * @param priority        of the flow
77      * @param ordinals        ordinals of the {@link Endpoint}
78      * @param macAddress      of the endpoint
79      * @param nodeConnectorId of endpoint openflow port
80      * @param ofWriter        flow writer
81      */
82     public void synchronizeEp(short goToTable, int priority, OrdinalFactory.EndpointFwdCtxOrdinals ordinals,
83                               MacAddress macAddress, NodeConnectorId nodeConnectorId, OfWriter ofWriter) {
84         int egId = ordinals.getEpgId();
85         int bdId = ordinals.getBdId();
86         int fdId = ordinals.getFdId();
87         int l3Id = ordinals.getL3Id();
88         int cgId = ordinals.getCgId();
89         int tunnelId = ordinals.getTunnelId();
90
91         Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
92         Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(cgId));
93         Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
94         Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
95         Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
96         Action tunIdAction = nxLoadTunIdAction(BigInteger.valueOf(tunnelId), false);
97
98         Match match = new MatchBuilder().setEthernetMatch(ethernetMatch(macAddress, null, null))
99                 .setInPort(nodeConnectorId)
100                 .build();
101         FlowId flowid = FlowIdUtils.newFlowId(tableId, "ep", match);
102         FlowBuilder flowBuilder = base(tableId).setPriority(priority)
103                 .setId(flowid)
104                 .setMatch(match)
105                 .setInstructions(
106                         instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg, tunIdAction),
107                                 gotoTableIns(goToTable)));
108         ofWriter.writeFlow(nodeId, tableId, flowBuilder.build());
109     }
110
111     /**
112      * Writes remote tunnel flows. Is evaluated, for which nodes this one acts as a remote node. Then using their
113      * ordinals, tunnel flow is written on this node
114      *
115      * @param goToTable      table ID for {@link GoToTable} instruction
116      * @param priority       of the flow
117      * @param tunnel         port number
118      * @param remoteOrdinals ordinals of remote node
119      * @param ofWriter       flow writer
120      */
121     public void createTunnelFlow(short goToTable, int priority, NodeConnectorId tunnel,
122                                  OrdinalFactory.EndpointFwdCtxOrdinals remoteOrdinals, OfWriter ofWriter) {
123         int egId = remoteOrdinals.getEpgId();
124         int bdId = remoteOrdinals.getBdId();
125         int fdId = remoteOrdinals.getFdId();
126         int l3Id = remoteOrdinals.getL3Id();
127         int tunnelId = remoteOrdinals.getTunnelId();
128
129         MatchBuilder matchBuilder = new MatchBuilder().setInPort(tunnel);
130         addNxTunIdMatch(matchBuilder, tunnelId);
131
132         Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
133         Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(0xffffff));
134         Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
135         Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
136         Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
137         Match match = matchBuilder.build();
138         FlowId flowId = FlowIdUtils.newFlowId(tableId, "tunnel", match);
139         FlowBuilder flowBuilder = base(tableId).setId(flowId)
140                 .setPriority(priority)
141                 .setMatch(match)
142                 .setInstructions(
143                         instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg), gotoTableIns(goToTable)));
144         ofWriter.writeFlow(nodeId, tableId, flowBuilder.build());
145     }
146
147     /**
148      * Writes remote broadcast flows. Is evaluated, for which nodes this one acts as a remote node. Then using their
149      * ordinals, broadcast flow is written on this node
150      *
151      * @param goToTable      table ID for {@link GoToTable} instruction
152      * @param priority       of the flow
153      * @param tunnel         port number
154      * @param remoteOrdinals ordinals of remote node
155      * @param ofWriter       flow writer
156      */
157     public void createBroadcastFlow(short goToTable, int priority, NodeConnectorId tunnel,
158                                     OrdinalFactory.EndpointFwdCtxOrdinals remoteOrdinals, OfWriter ofWriter) {
159         int fdId = remoteOrdinals.getFdId();
160
161         MatchBuilder matchBuilder = new MatchBuilder().setInPort(tunnel);
162         addNxTunIdMatch(matchBuilder, fdId);
163
164         Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
165
166         Match match = matchBuilder.build();
167         FlowId flowId = FlowIdUtils.newFlowId(tableId, "tunnelFdId", match);
168         FlowBuilder flowBuilder = base(tableId).setId(flowId)
169                 .setPriority(priority)
170                 .setMatch(match)
171                 .setInstructions(instructions(applyActionIns(fdReg), gotoTableIns(goToTable)));
172         ofWriter.writeFlow(nodeId, tableId, flowBuilder.build());
173     }
174 }