2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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 package org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities;
10 import com.google.common.base.Optional;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
15 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
16 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
17 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
18 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
19 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
20 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.ServiceBindings;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.StypeOpenflow;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfoKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import java.math.BigInteger;
46 import java.util.ArrayList;
47 import java.util.List;
49 public class FlowBasedServicesUtils {
50 private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesUtils.class);
52 public static ServicesInfo getServicesInfoForInterface(String interfaceName, DataBroker dataBroker) {
53 ServicesInfoKey servicesInfoKey = new ServicesInfoKey(interfaceName);
54 InstanceIdentifier.InstanceIdentifierBuilder<ServicesInfo> servicesInfoIdentifierBuilder =
55 InstanceIdentifier.builder(ServiceBindings.class).child(ServicesInfo.class, servicesInfoKey);
56 Optional<ServicesInfo> servicesInfoOptional = IfmUtil.read(LogicalDatastoreType.CONFIGURATION,
57 servicesInfoIdentifierBuilder.build(), dataBroker);
59 if (servicesInfoOptional.isPresent()) {
60 return servicesInfoOptional.get();
66 public static NodeConnectorId getNodeConnectorIdFromInterface(Interface iface, DataBroker dataBroker) {
67 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
68 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(iface.getName(), dataBroker);
69 List<String> ofportIds = ifState.getLowerLayerIf();
70 return new NodeConnectorId(ofportIds.get(0));
73 public static List<MatchInfo> getMatchInfoForVlanPortAtIngressTable(BigInteger dpId, long portNo, long vlanId) {
74 List<MatchInfo> matches = new ArrayList<>();
75 matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {dpId, BigInteger.valueOf(portNo)}));
77 LOG.error("VlanId matching support is not fully available in Be.");
78 matches.add(new MatchInfo(MatchFieldType.vlan_vid, new long[]{vlanId}));
83 public static List<MatchInfo> getMatchInfoForTunnelPortAtIngressTable(BigInteger dpId, long portNo, Interface iface) {
84 List<MatchInfo> matches = new ArrayList<MatchInfo>();
85 matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[]{dpId, BigInteger.valueOf(portNo)}));
89 public static List<MatchInfo> getMatchInfoForDispatcherTable(BigInteger dpId, Interface iface,
90 int interfaceTag, short servicePriority) {
91 List<MatchInfo> matches = new ArrayList<MatchInfo>();
92 matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
93 MetaDataUtil.getMetaDataForLPortDispatcher(interfaceTag, servicePriority),
94 MetaDataUtil.getMetaDataMaskForLPortDispatcher() }));
98 public static Long getLPortTag(Interface iface, DataBroker dataBroker) {
99 /*ParentRefs parentRefs = iface.getAugmentation(ParentRefs.class);
100 String portName = parentRefs.getParentInterface();
101 BigInteger dpIdFromInterface = parentRefs.getDatapathNodeIdentifier();
102 String portKey = FlowBasedServicesUtils.getInterfaceRefInfo(dpIdFromInterface.toString(), portName);
103 if (iface.getType().isAssignableFrom(L2vlan.class)) {
104 InterfacesMetaKey interfacesMetaKey = new InterfacesMetaKey(portKey);
105 InterfacesInfoKey interfacesInfoKey = new InterfacesInfoKey(iface.getName());
106 InterfacesInfo interfacesInfo = VlanInterfaceUtilities.getInterfacesInfoFromConfigDS(interfacesMetaKey,
107 interfacesInfoKey, dataBroker);
108 return interfacesInfo.getLporttag();
109 } else if (iface.getType().isAssignableFrom(Tunnel.class)) {
110 TunnelInterfaceRefInfoKey tunnelInterfaceRefInfoKey = new TunnelInterfaceRefInfoKey(portKey);
111 TunnelInterfaceEntries tunnelInterfaceEntries =
112 TunnelInterfaceUtilities.getTunnelInterfaceRefEntriesFromConfigDs(
113 tunnelInterfaceRefInfoKey, iface.getName(), dataBroker);
114 return tunnelInterfaceEntries.getLportTag();
119 public static void installInterfaceIngressFlow(BigInteger dpId, int vlanId,
120 BoundServices boundServiceNew,
121 DataBroker dataBroker, WriteTransaction t,
122 List<MatchInfo> matches, int lportTag, short tableId) {
123 List<Instruction> instructions = boundServiceNew.getAugmentation(StypeOpenflow.class).getInstruction();
125 int serviceInstructionsSize = instructions.size();
126 List<Instruction> instructionSet = new ArrayList<Instruction>();
128 // incrementing instructionSize and using it as actionKey. Because it won't clash with any other instructions
129 int actionKey = ++serviceInstructionsSize;
130 instructionSet.add(MDSALUtil.buildAndGetPopVlanActionInstruction(actionKey, ++serviceInstructionsSize));
133 if (lportTag != 0L) {
134 BigInteger[] metadataValues = IfmUtil.mergeOpenflowMetadataWriteInstructions(instructions);
135 short sIndex = boundServiceNew.getServicePriority();
136 BigInteger metadata = MetaDataUtil.getMetaDataForLPortDispatcher(lportTag,
137 ++sIndex, metadataValues[0]);
138 BigInteger metadataMask = MetaDataUtil.getMetaDataMaskForLPortDispatcher(
139 MetaDataUtil.METADATA_MASK_SERVICE_INDEX,
140 MetaDataUtil.METADATA_MASK_LPORT_TAG, metadataValues[1]);
141 instructionSet.add(MDSALUtil.buildAndGetWriteMetadaInstruction(metadata, metadataMask,
142 ++serviceInstructionsSize));
145 if (instructions != null && !instructions.isEmpty()) {
146 for (Instruction info : instructions) {
147 // Skip meta data write as that is handled already
148 if (info.getInstruction() instanceof WriteMetadataCase) {
151 instructionSet.add(info);
155 String serviceRef = boundServiceNew.getServiceName();
156 StypeOpenflow stypeOpenflow = boundServiceNew.getAugmentation(StypeOpenflow.class);
157 Flow ingressFlow = MDSALUtil.buildFlowNew(tableId, serviceRef,
158 stypeOpenflow.getFlowPriority(), serviceRef, 0, 0,
159 stypeOpenflow.getFlowCookie(), matches, instructionSet);
160 installFlow(dpId, ingressFlow, dataBroker, t);
163 private static void installFlow(BigInteger dpId, Flow flow, DataBroker dataBroker, WriteTransaction t) {
164 FlowKey flowKey = new FlowKey(new FlowId(flow.getId()));
165 Node nodeDpn = buildInventoryDpnNode(dpId);
166 InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
167 .child(Node.class, nodeDpn.getKey()).augmentation(FlowCapableNode.class)
168 .child(Table.class, new TableKey(flow.getTableId())).child(Flow.class,flowKey).build();
170 t.put(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow, true);
173 private static Node buildInventoryDpnNode(BigInteger dpnId) {
174 NodeId nodeId = new NodeId("openflow:" + dpnId);
175 Node nodeDpn = new NodeBuilder().setId(nodeId).setKey(new NodeKey(nodeId)).build();
180 public static void installLPortDispatcherFlow(BigInteger dpId, BoundServices boundService, Interface iface,
181 DataBroker dataBroker, WriteTransaction t, int interfaceTag) {
182 LOG.debug("Installing LPort Dispatcher Flows {}, {}", dpId, iface);
183 short serviceIndex = boundService.getServicePriority();
184 String serviceRef = boundService.getServiceName();
185 List<MatchInfo> matches = FlowBasedServicesUtils.getMatchInfoForDispatcherTable(dpId, iface,
186 interfaceTag, serviceIndex);
188 // Get the metadata and mask from the service's write metadata instruction
189 StypeOpenflow stypeOpenFlow = boundService.getAugmentation(StypeOpenflow.class);
190 List<Instruction> serviceInstructions = stypeOpenFlow.getInstruction();
191 int instructionSize = serviceInstructions.size();
192 BigInteger[] metadataValues = IfmUtil.mergeOpenflowMetadataWriteInstructions(serviceInstructions);
193 BigInteger metadata = MetaDataUtil.getMetaDataForLPortDispatcher(interfaceTag, ++serviceIndex, metadataValues[0]);
194 BigInteger metadataMask = MetaDataUtil.getMetaDataMaskForLPortDispatcher(MetaDataUtil.METADATA_MASK_SERVICE_INDEX,
195 MetaDataUtil.METADATA_MASK_LPORT_TAG, metadataValues[1]);
197 // build the final instruction for LPort Dispatcher table flow entry
198 List<Instruction> instructions = new ArrayList<Instruction>();
199 instructions.add(MDSALUtil.buildAndGetWriteMetadaInstruction(metadata, metadataMask, ++instructionSize));
200 if (serviceInstructions != null && !serviceInstructions.isEmpty()) {
201 for (Instruction info : serviceInstructions) {
202 // Skip meta data write as that is handled already
203 if (info.getInstruction() instanceof WriteMetadataCase) {
206 instructions.add(info);
210 // build the flow and install it
211 Flow ingressFlow = MDSALUtil.buildFlowNew(stypeOpenFlow.getDispatcherTableId(), serviceRef,
212 boundService.getServicePriority(), serviceRef, 0, 0, stypeOpenFlow.getFlowCookie(), matches, instructions);
213 installFlow(dpId, ingressFlow, dataBroker, t);
216 public static void removeIngressFlow(Interface iface, BoundServices serviceOld, BigInteger dpId,
217 DataBroker dataBroker, WriteTransaction t) {
218 LOG.debug("Removing Ingress Flows");
219 String flowKeyStr = iface.getName() + serviceOld.getServicePriority() +
220 serviceOld.getServiceName() + IfmConstants.VLAN_INTERFACE_INGRESS_TABLE;
221 FlowKey flowKey = new FlowKey(new FlowId(flowKeyStr));
222 Node nodeDpn = buildInventoryDpnNode(dpId);
223 InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
224 .child(Node.class, nodeDpn.getKey()).augmentation(FlowCapableNode.class)
225 .child(Table.class, new TableKey(IfmConstants.VLAN_INTERFACE_INGRESS_TABLE)).child(Flow.class, flowKey).build();
227 t.delete(LogicalDatastoreType.CONFIGURATION, flowInstanceId);
230 public static void removeLPortDispatcherFlow(BigInteger dpId, Interface iface, BoundServices boundServicesOld,
231 DataBroker dataBroker, WriteTransaction t) {
232 LOG.debug("Removing LPort Dispatcher Flows {}, {}", dpId, iface);
233 Long interfaceTag = FlowBasedServicesUtils.getLPortTag(iface, dataBroker);
235 StypeOpenflow stypeOpenFlow = boundServicesOld.getAugmentation(StypeOpenflow.class);
236 String flowKeyStr = iface.getName() + boundServicesOld.getServicePriority() +
237 boundServicesOld.getServiceName() + stypeOpenFlow.getDispatcherTableId();
238 FlowKey flowKey = new FlowKey(new FlowId(flowKeyStr));
239 Node nodeDpn = buildInventoryDpnNode(dpId);
240 InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
241 .child(Node.class, nodeDpn.getKey()).augmentation(FlowCapableNode.class)
242 .child(Table.class, new TableKey(stypeOpenFlow.getDispatcherTableId())).child(Flow.class, flowKey).build();
244 t.delete(LogicalDatastoreType.CONFIGURATION, flowInstanceId);
247 public static String getInterfaceRefInfo(String dpId, String portName) {
248 String portRefInfo = "";
249 if (!"".equals(dpId)) {
250 portRefInfo = dpId.toString() + ":";
252 portRefInfo = portRefInfo + portName;