2 * Copyright (c) 2016 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.netvirt.vpnmanager;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.concurrent.CopyOnWriteArrayList;
16 import javax.annotation.PostConstruct;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
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.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
23 import org.opendaylight.genius.mdsalutil.ActionInfo;
24 import org.opendaylight.genius.mdsalutil.FlowEntity;
25 import org.opendaylight.genius.mdsalutil.InstructionInfo;
26 import org.opendaylight.genius.mdsalutil.MDSALUtil;
27 import org.opendaylight.genius.mdsalutil.MatchInfo;
28 import org.opendaylight.genius.mdsalutil.NwConstants;
29 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
30 import org.opendaylight.genius.mdsalutil.actions.ActionPuntToController;
31 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
32 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
33 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
34 import org.opendaylight.genius.mdsalutil.matches.MatchArpOp;
35 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
36 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
37 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderUtil;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 public class VpnNodeListener extends AsyncClusteredDataTreeChangeListenerBase<Node, VpnNodeListener> {
47 private static final Logger LOG = LoggerFactory.getLogger(VpnNodeListener.class);
48 private static final String FLOWID_PREFIX = "L3.";
50 private final DataBroker broker;
51 private final IMdsalApiManager mdsalManager;
52 private final JobCoordinator jobCoordinator;
53 private final List<BigInteger> connectedDpnIds;
56 public VpnNodeListener(DataBroker dataBroker, IMdsalApiManager mdsalManager, JobCoordinator jobCoordinator) {
57 super(Node.class, VpnNodeListener.class);
58 this.broker = dataBroker;
59 this.mdsalManager = mdsalManager;
60 this.jobCoordinator = jobCoordinator;
61 this.connectedDpnIds = new CopyOnWriteArrayList<>();
66 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
70 protected InstanceIdentifier<Node> getWildCardPath() {
71 return InstanceIdentifier.create(Nodes.class).child(Node.class);
75 protected VpnNodeListener getDataTreeChangeListener() {
76 return VpnNodeListener.this;
80 protected void add(InstanceIdentifier<Node> identifier, Node add) {
81 BigInteger dpId = MDSALUtil.getDpnIdFromNodeName(add.getId());
82 if (!connectedDpnIds.contains(dpId)) {
83 connectedDpnIds.add(dpId);
89 protected void remove(InstanceIdentifier<Node> identifier, Node del) {
90 BigInteger dpId = MDSALUtil.getDpnIdFromNodeName(del.getId());
91 connectedDpnIds.remove(dpId);
95 protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
98 public boolean isConnectedNode(BigInteger nodeId) {
99 return nodeId != null && connectedDpnIds.contains(nodeId);
102 private void processNodeAdd(BigInteger dpId) {
103 jobCoordinator.enqueueJob("VPNNODE-" + dpId.toString(),
105 WriteTransaction writeFlowTx = broker.newWriteOnlyTransaction();
106 LOG.debug("Received notification to install TableMiss entries for dpn {} ", dpId);
107 makeTableMissFlow(writeFlowTx, dpId, NwConstants.ADD_FLOW);
108 makeL3IntfTblMissFlow(writeFlowTx, dpId, NwConstants.ADD_FLOW);
109 makeSubnetRouteTableMissFlow(writeFlowTx, dpId, NwConstants.ADD_FLOW);
110 createTableMissForVpnGwFlow(writeFlowTx, dpId);
111 createL3GwMacArpFlows(writeFlowTx, dpId);
112 programTableMissForVpnVniDemuxTable(writeFlowTx, dpId, NwConstants.ADD_FLOW);
113 return Collections.singletonList(writeFlowTx.submit());
117 private void makeTableMissFlow(WriteTransaction writeFlowTx, BigInteger dpnId, int addOrRemove) {
118 final BigInteger cookieTableMiss = new BigInteger("1030000", 16);
119 // Instruction to goto L3 InterfaceTable
120 List<InstructionInfo> instructions =
121 Collections.singletonList(new InstructionGotoTable(NwConstants.L3_INTERFACE_TABLE));
122 List<MatchInfo> matches = new ArrayList<>();
123 FlowEntity flowEntityLfib = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L3_LFIB_TABLE,
124 getTableMissFlowRef(dpnId, NwConstants.L3_LFIB_TABLE, NwConstants.TABLE_MISS_FLOW),
125 NwConstants.TABLE_MISS_PRIORITY, "Table Miss", 0, 0, cookieTableMiss, matches, instructions);
127 if (addOrRemove == NwConstants.ADD_FLOW) {
128 LOG.debug("Invoking MDSAL to install Table Miss Entry");
129 mdsalManager.addFlowToTx(flowEntityLfib, writeFlowTx);
131 mdsalManager.removeFlowToTx(flowEntityLfib, writeFlowTx);
135 private void makeL3IntfTblMissFlow(WriteTransaction writeFlowTx, BigInteger dpnId, int addOrRemove) {
136 List<InstructionInfo> instructions = new ArrayList<>();
137 List<MatchInfo> matches = new ArrayList<>();
138 final BigInteger cookieTableMiss = new BigInteger("1030000", 16);
139 // Instruction to goto L3 InterfaceTable
141 List<ActionInfo> actionsInfos = new ArrayList<>();
142 actionsInfos.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
143 instructions.add(new InstructionApplyActions(actionsInfos));
144 //instructions.add(new InstructionGotoTable(NwConstants.LPORT_DISPATCHER_TABLE));
146 FlowEntity flowEntityL3Intf = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L3_INTERFACE_TABLE,
147 getTableMissFlowRef(dpnId, NwConstants.L3_INTERFACE_TABLE, NwConstants.TABLE_MISS_FLOW),
148 NwConstants.TABLE_MISS_PRIORITY, "L3 Interface Table Miss", 0, 0, cookieTableMiss,
149 matches, instructions);
150 if (addOrRemove == NwConstants.ADD_FLOW) {
151 LOG.debug("Invoking MDSAL to install L3 interface Table Miss Entries");
152 mdsalManager.addFlowToTx(flowEntityL3Intf, writeFlowTx);
154 mdsalManager.removeFlowToTx(flowEntityL3Intf, writeFlowTx);
158 private void makeSubnetRouteTableMissFlow(WriteTransaction writeFlowTx, BigInteger dpnId, int addOrRemove) {
159 final BigInteger cookieTableMiss = new BigInteger("8000004", 16);
160 List<ActionInfo> actionsInfos = new ArrayList<>();
161 List<InstructionInfo> instructions = new ArrayList<>();
162 actionsInfos.add(new ActionPuntToController());
163 instructions.add(new InstructionApplyActions(actionsInfos));
164 List<MatchInfo> matches = new ArrayList<>();
165 String flowRef = getTableMissFlowRef(dpnId, NwConstants.L3_SUBNET_ROUTE_TABLE, NwConstants.TABLE_MISS_FLOW);
166 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L3_SUBNET_ROUTE_TABLE, flowRef,
167 NwConstants.TABLE_MISS_PRIORITY, "Subnet Route Table Miss", 0, 0, cookieTableMiss, matches, instructions);
169 if (addOrRemove == NwConstants.ADD_FLOW) {
170 mdsalManager.addFlowToTx(flowEntity, writeFlowTx);
172 mdsalManager.removeFlowToTx(flowEntity, writeFlowTx);
176 private void programTableMissForVpnVniDemuxTable(WriteTransaction writeFlowTx, BigInteger dpnId, int addOrRemove) {
177 List<ActionInfo> actionsInfos = Collections.singletonList(new ActionNxResubmit(NwConstants
178 .LPORT_DISPATCHER_TABLE));
179 List<InstructionInfo> instructions = Collections.singletonList(new InstructionApplyActions(actionsInfos));
180 List<MatchInfo> matches = new ArrayList<>();
181 String flowRef = getTableMissFlowRef(dpnId, NwConstants.L3VNI_EXTERNAL_TUNNEL_DEMUX_TABLE,
182 NwConstants.TABLE_MISS_FLOW);
183 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L3VNI_EXTERNAL_TUNNEL_DEMUX_TABLE,
184 flowRef, NwConstants.TABLE_MISS_PRIORITY, "VPN-VNI Demux Table Miss", 0, 0,
185 new BigInteger("1080000", 16), matches, instructions);
187 if (addOrRemove == NwConstants.ADD_FLOW) {
188 mdsalManager.addFlowToTx(flowEntity, writeFlowTx);
190 mdsalManager.removeFlowToTx(flowEntity, writeFlowTx);
194 private void createTableMissForVpnGwFlow(WriteTransaction writeFlowTx, BigInteger dpId) {
195 List<MatchInfo> matches = new ArrayList<>();
196 List<ActionInfo> actionsInfos =
197 Collections.singletonList(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
198 List<InstructionInfo> instructions = Collections.singletonList(new InstructionApplyActions(actionsInfos));
199 FlowEntity flowEntityMissforGw = MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_GW_MAC_TABLE,
200 getTableMissFlowRef(dpId, NwConstants.L3_GW_MAC_TABLE, NwConstants.TABLE_MISS_FLOW),
201 NwConstants.TABLE_MISS_PRIORITY, "L3 Gw Mac Table Miss", 0, 0, new BigInteger("1080000", 16), matches,
203 LOG.trace("Invoking MDSAL to install L3 Gw Mac Table Miss Entry");
204 mdsalManager.addFlowToTx(flowEntityMissforGw, writeFlowTx);
207 private void createL3GwMacArpFlows(WriteTransaction writeFlowTx, BigInteger dpId) {
208 FlowEntity arpReqGwMacTbl = ArpResponderUtil.createArpDefaultFlow(dpId, NwConstants.L3_GW_MAC_TABLE,
209 NwConstants.ARP_REQUEST, () -> Arrays.asList(MatchEthernetType.ARP, MatchArpOp.REQUEST),
210 () -> Collections.singletonList(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE)));
211 LOG.trace("Invoking MDSAL to install Arp Rquest Match Flow for table {}", NwConstants.L3_GW_MAC_TABLE);
212 mdsalManager.addFlowToTx(arpReqGwMacTbl, writeFlowTx);
213 FlowEntity arpRepGwMacTbl = ArpResponderUtil.createArpDefaultFlow(dpId, NwConstants.L3_GW_MAC_TABLE,
214 NwConstants.ARP_REPLY, () -> Arrays.asList(MatchEthernetType.ARP, MatchArpOp.REPLY),
215 () -> Collections.singletonList(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE)));
216 LOG.trace("Invoking MDSAL to install Arp Reply Match Flow for Table {} ", NwConstants.L3_GW_MAC_TABLE);
217 mdsalManager.addFlowToTx(arpRepGwMacTbl, writeFlowTx);
220 private String getTableMissFlowRef(BigInteger dpnId, short tableId, int tableMiss) {
221 return new StringBuffer().append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
222 .append(tableId).append(NwConstants.FLOWID_SEPARATOR).append(tableMiss)
223 .append(FLOWID_PREFIX).toString();