NETVIRT-1122 - L3 ping between pods not working
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / VpnNodeListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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 package org.opendaylight.netvirt.vpnmanager;
9
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;
43
44 @Singleton
45 public class VpnNodeListener extends AsyncClusteredDataTreeChangeListenerBase<Node, VpnNodeListener> {
46
47     private static final Logger LOG = LoggerFactory.getLogger(VpnNodeListener.class);
48     private static final String FLOWID_PREFIX = "L3.";
49
50     private final DataBroker broker;
51     private final IMdsalApiManager mdsalManager;
52     private final JobCoordinator jobCoordinator;
53     private final List<BigInteger> connectedDpnIds;
54
55     @Inject
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<>();
62     }
63
64     @PostConstruct
65     public void start() {
66         registerListener(LogicalDatastoreType.OPERATIONAL, broker);
67     }
68
69     @Override
70     protected InstanceIdentifier<Node> getWildCardPath() {
71         return InstanceIdentifier.create(Nodes.class).child(Node.class);
72     }
73
74     @Override
75     protected VpnNodeListener getDataTreeChangeListener() {
76         return VpnNodeListener.this;
77     }
78
79     @Override
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);
84         }
85         processNodeAdd(dpId);
86     }
87
88     @Override
89     protected void remove(InstanceIdentifier<Node> identifier, Node del) {
90         BigInteger dpId = MDSALUtil.getDpnIdFromNodeName(del.getId());
91         connectedDpnIds.remove(dpId);
92     }
93
94     @Override
95     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
96     }
97
98     public boolean isConnectedNode(BigInteger nodeId) {
99         return nodeId != null && connectedDpnIds.contains(nodeId);
100     }
101
102     private void processNodeAdd(BigInteger dpId) {
103         jobCoordinator.enqueueJob("VPNNODE-" + dpId.toString(),
104             () -> {
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());
114             }, 3);
115     }
116
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);
126
127         if (addOrRemove == NwConstants.ADD_FLOW) {
128             LOG.debug("Invoking MDSAL to install Table Miss Entry");
129             mdsalManager.addFlowToTx(flowEntityLfib, writeFlowTx);
130         } else {
131             mdsalManager.removeFlowToTx(flowEntityLfib, writeFlowTx);
132         }
133     }
134
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
140
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));
145
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);
153         } else {
154             mdsalManager.removeFlowToTx(flowEntityL3Intf, writeFlowTx);
155         }
156     }
157
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);
168
169         if (addOrRemove == NwConstants.ADD_FLOW) {
170             mdsalManager.addFlowToTx(flowEntity, writeFlowTx);
171         } else {
172             mdsalManager.removeFlowToTx(flowEntity, writeFlowTx);
173         }
174     }
175
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);
186
187         if (addOrRemove == NwConstants.ADD_FLOW) {
188             mdsalManager.addFlowToTx(flowEntity, writeFlowTx);
189         } else {
190             mdsalManager.removeFlowToTx(flowEntity, writeFlowTx);
191         }
192     }
193
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,
202             instructions);
203         LOG.trace("Invoking MDSAL to install L3 Gw Mac Table Miss Entry");
204         mdsalManager.addFlowToTx(flowEntityMissforGw, writeFlowTx);
205     }
206
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);
218     }
219
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();
224     }
225 }