Merge "Framework for netvirt model (Part 1)."
[ovsdb.git] / openstack / net-virt-sfc / impl / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / standalone / openflow13 / SfcClassifier.java
1 /*
2  * Copyright © 2015 Red Hat, 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.ovsdb.openstack.netvirt.sfc.standalone.openflow13;
10
11 import com.google.common.collect.Lists;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
18 import org.opendaylight.ovsdb.openstack.netvirt.sfc.NshUtils;
19 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
20 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
21 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
22 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
23 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.Matches;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.matches.ace.type.AceEth;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIp;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv4;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class SfcClassifier {
51     private static final Logger LOG = LoggerFactory.getLogger(SfcClassifier.class);
52     private DataBroker dataBroker;
53     private Southbound southbound;
54     private MdsalUtils mdsalUtils;
55     public final static long REG_VALUE_FROM_LOCAL = 0x1L;
56     public final static long REG_VALUE_FROM_REMOTE = 0x2L;
57     public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
58     private static final String OPENFLOW = "openflow:";
59
60     public SfcClassifier(DataBroker dataBroker, Southbound southbound, MdsalUtils mdsalUtils) {
61         this.dataBroker = dataBroker;
62         this.southbound = southbound;
63         this.mdsalUtils = mdsalUtils;
64     }
65
66     /*
67      * (TABLE:50) EGRESS VM TRAFFIC TOWARDS TEP with NSH header
68      * MATCH: Match fields passed through ACL entry
69      * INSTRUCTION: SET TUNNELID AND GOTO TABLE TUNNEL TABLE (N)
70      * TABLE=0,IN_PORT=2,DL_SRC=00:00:00:00:00:01 \
71      * ACTIONS=SET_FIELD:5->TUN_ID,GOTO_TABLE=1"
72      */
73     public void programSfcClassiferFlows(Long dpidLong, short writeTable, String ruleName, Matches match,
74                                          NshUtils nshHeader, long tunnelOfPort, boolean write) {
75         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpidLong);
76         FlowBuilder flowBuilder = new FlowBuilder();
77         String flowName = "sfcClass_" + ruleName + "_" + nshHeader.getNshNsp();
78         FlowUtils.initFlowBuilder(flowBuilder, flowName, writeTable);
79
80         MatchBuilder matchBuilder = buildMatch(match);
81         flowBuilder.setMatch(matchBuilder.build());
82
83         if (write) {
84             List<Action> actionList = getNshAction(nshHeader);
85             ActionBuilder ab = new ActionBuilder();
86
87             ab.setAction(ActionUtils.outputAction(FlowUtils.getNodeConnectorId(dpidLong, tunnelOfPort)));
88             ab.setOrder(actionList.size());
89             ab.setKey(new ActionKey(actionList.size()));
90             actionList.add(ab.build());
91
92             ApplyActionsBuilder aab = new ApplyActionsBuilder();
93             aab.setAction(actionList);
94
95             InstructionBuilder ib = new InstructionBuilder();
96             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
97             ib.setOrder(0);
98             ib.setKey(new InstructionKey(0));
99             List<Instruction> instructions = Lists.newArrayList();
100             instructions.add(ib.build());
101
102             InstructionsBuilder isb = new InstructionsBuilder();
103             isb.setInstruction(instructions);
104             flowBuilder.setInstructions(isb.build());
105             writeFlow(flowBuilder, nodeBuilder);
106         } else {
107             removeFlow(flowBuilder, nodeBuilder);
108         }
109     }
110
111     public void programEgressSfcClassiferFlows(Long dpidLong, short writeTable, String ruleName,
112                                                Matches match, NshUtils nshHeader,
113                                                long tunnelOfPort, long outOfPort, boolean write) {
114         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpidLong);
115         FlowBuilder flowBuilder = new FlowBuilder();
116         String flowName = "egressSfcClass_" + ruleName + "_" + nshHeader.getNshNsp() + "_" + nshHeader.getNshNsi();
117         FlowUtils.initFlowBuilder(flowBuilder, flowName, writeTable);
118
119         MatchBuilder matchBuilder = new MatchBuilder();
120         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, tunnelOfPort).build());
121         flowBuilder.setMatch(
122                 MatchUtils.createTunnelIDMatch(matchBuilder, BigInteger.valueOf(nshHeader.getNshMetaC2())).build());
123         flowBuilder.setMatch(MatchUtils.addNxNspMatch(matchBuilder, nshHeader.getNshNsp()).build());
124         flowBuilder.setMatch(MatchUtils.addNxNsiMatch(matchBuilder, nshHeader.getNshNsi()).build());
125
126         if (write) {
127             List<Action> actionList = new ArrayList<>();
128             ActionBuilder ab = new ActionBuilder();
129
130             ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
131                     BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
132             ab.setOrder(0);
133             ab.setKey(new ActionKey(0));
134             actionList.add(ab.build());
135
136             ab.setAction(ActionUtils.outputAction(FlowUtils.getNodeConnectorId(dpidLong, outOfPort)));
137             ab.setOrder(1);
138             ab.setKey(new ActionKey(1));
139             actionList.add(ab.build());
140
141             ApplyActionsBuilder aab = new ApplyActionsBuilder();
142             aab.setAction(actionList);
143
144             InstructionBuilder ib = new InstructionBuilder();
145             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
146
147             ib.setOrder(0);
148             ib.setKey(new InstructionKey(0));
149             List<Instruction> instructions = new ArrayList<>();
150             instructions.add(ib.build());
151
152             InstructionsBuilder isb = new InstructionsBuilder();
153             isb.setInstruction(instructions);
154
155             flowBuilder.setInstructions(isb.build());
156             writeFlow(flowBuilder, nodeBuilder);
157         } else {
158             removeFlow(flowBuilder, nodeBuilder);
159         }
160     }
161
162     private List<Action> getNshAction(NshUtils header) {
163         // Build the Actions to Add the NSH Header
164         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nshC1Load =
165                 ActionUtils.nxLoadNshc1RegAction(header.getNshMetaC1());
166         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nshC2Load =
167                 ActionUtils.nxLoadNshc2RegAction(header.getNshMetaC2());
168         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nspLoad =
169                 ActionUtils.nxSetNspAction(header.getNshNsp());
170         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nsiLoad =
171                 ActionUtils.nxSetNsiAction(header.getNshNsi());
172         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action loadChainTunVnid =
173                 ActionUtils.nxLoadTunIdAction(BigInteger.valueOf(header.getNshNsp()), false);
174         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action loadChainTunDest =
175                 ActionUtils.nxLoadTunIPv4Action(header.getNshTunIpDst().getValue(), false);
176
177         int count = 0;
178         List<Action> actionList = Lists.newArrayList();
179         actionList.add(new ActionBuilder().setOrder(count++).setAction(nshC1Load).build());
180         actionList.add(new ActionBuilder().setOrder(count++).setAction(nshC2Load).build());
181         actionList.add(new ActionBuilder().setOrder(count++).setAction(nspLoad).build());
182         actionList.add(new ActionBuilder().setOrder(count++).setAction(nsiLoad).build());
183         actionList.add(new ActionBuilder().setOrder(count++).setAction(loadChainTunDest).build());
184         actionList.add(new ActionBuilder().setOrder(count++).setAction(loadChainTunVnid).build());
185         return actionList;
186     }
187
188     public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort,
189                                    short writeTable, short goToTableId, Matches match, boolean write) {
190         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpidLong);
191         FlowBuilder flowBuilder = new FlowBuilder();
192         String flowName = "sfcIngress_" + segmentationId + "_" + inPort;
193         FlowUtils.initFlowBuilder(flowBuilder, flowName, writeTable);
194
195         MatchBuilder matchBuilder = buildMatch(match);
196         flowBuilder.setMatch(matchBuilder.build());
197         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
198
199         if (write) {
200             InstructionBuilder ib = new InstructionBuilder();
201             InstructionsBuilder isb = new InstructionsBuilder();
202             List<Instruction> instructions = Lists.newArrayList();
203
204             InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
205             ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
206             List<Action> actionList = aac.getApplyActions().getAction();
207
208             // TODO: Mark the packets as sfc classified?
209
210             ActionBuilder ab = new ActionBuilder();
211             ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
212                     BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
213             ab.setOrder(1);
214             ab.setKey(new ActionKey(1));
215             actionList.add(ab.build());
216
217             ib.setOrder(0);
218             ib.setKey(new InstructionKey(0));
219             instructions.add(ib.build());
220
221             // Next service GOTO Instructions Need to be appended to the List
222             ib = InstructionUtils.createGotoTableInstructions(new InstructionBuilder(), goToTableId);
223             ib.setOrder(1);
224             ib.setKey(new InstructionKey(1));
225             instructions.add(ib.build());
226
227             isb.setInstruction(instructions);
228             flowBuilder.setInstructions(isb.build());
229             writeFlow(flowBuilder, nodeBuilder);
230         } else {
231             removeFlow(flowBuilder, nodeBuilder);
232         }
233     }
234
235     public MatchBuilder buildMatch(Matches matches) {
236         MatchBuilder matchBuilder = new MatchBuilder();
237
238         if (matches.getAceType() instanceof AceIp) {
239             AceIp aceIp = (AceIp)matches.getAceType();
240             if (aceIp.getAceIpVersion() instanceof AceIpv4) {
241                 //AceIpv4 aceIpv4 = (AceIpv4) aceIp.getAceIpVersion();
242                 //MatchUtils.createSrcL3IPv4Match(matchBuilder, aceIpv4.getSourceIpv4Network());
243                 //MatchUtils.createDstL3IPv4Match(matchBuilder, aceIpv4.getDestinationIpv4Network());
244                 MatchUtils.createIpProtocolMatch(matchBuilder, aceIp.getProtocol());
245                 MatchUtils.addLayer4Match(matchBuilder, aceIp.getProtocol().intValue(), 0,
246                         aceIp.getDestinationPortRange().getLowerPort().getValue().intValue());
247             }
248         } else if (matches.getAceType() instanceof AceEth) {
249             AceEth aceEth = (AceEth) matches.getAceType();
250             MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(aceEth.getSourceMacAddress().getValue()));
251             MatchUtils.createDestEthMatch(matchBuilder, new MacAddress(aceEth.getDestinationMacAddress().getValue()),
252                     new MacAddress(aceEth.getDestinationMacAddressMask().getValue()));
253         }
254
255         LOG.info("buildMatch: {}", matchBuilder.build());
256         return matchBuilder;
257     }
258
259     protected void writeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
260         LOG.debug("writeFlow: flowBuilder: {}, nodeBuilder: {}", flowBuilder.build(), nodeBuilder.build());
261         mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION, FlowUtils.createNodePath(nodeBuilder),
262                 nodeBuilder.build());
263         mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, FlowUtils.createFlowPath(flowBuilder, nodeBuilder),
264                 flowBuilder.build());
265     }
266
267     protected void removeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
268         mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, FlowUtils.createFlowPath(flowBuilder, nodeBuilder));
269     }
270 }