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.genius.interfacemanager.listeners;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
13 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
14 import org.opendaylight.genius.interfacemanager.IfmUtil;
15 import org.opendaylight.genius.interfacemanager.IfmConstants;
16 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
17 import org.opendaylight.genius.interfacemanager.commons.InterfaceMetaUtils;
18 import org.opendaylight.genius.interfacemanager.renderer.ovs.statehelpers.OvsInterfaceStateAddHelper;
19 import org.opendaylight.genius.interfacemanager.renderer.ovs.statehelpers.OvsInterfaceStateRemoveHelper;
20 import org.opendaylight.genius.interfacemanager.renderer.ovs.statehelpers.OvsInterfaceStateUpdateHelper;
21 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.AlivenessMonitorService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info._interface.parent.entry.InterfaceChildEntry;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.concurrent.Callable;
43 * This Class is a Data Change Listener for FlowCapableNodeConnector updates.
44 * This creates an entry in the interface-state OperDS for every node-connector used.
46 * NOTE: This class just creates an ifstate entry whose interface-name will be the same as the node-connector portname.
47 * If PortName is not unique across DPNs, this implementation can have problems.
50 public class InterfaceInventoryStateListener extends AsyncDataTreeChangeListenerBase<FlowCapableNodeConnector, InterfaceInventoryStateListener> {
51 private static final Logger LOG = LoggerFactory.getLogger(InterfaceInventoryStateListener.class);
52 private DataBroker dataBroker;
53 private IdManagerService idManager;
54 private IMdsalApiManager mdsalApiManager;
55 private AlivenessMonitorService alivenessMonitorService;
57 public InterfaceInventoryStateListener(final DataBroker dataBroker, final IdManagerService idManager,
58 final IMdsalApiManager mdsalApiManager, final AlivenessMonitorService alivenessMonitorService) {
59 super(FlowCapableNodeConnector.class, InterfaceInventoryStateListener.class);
60 this.dataBroker = dataBroker;
61 this.idManager = idManager;
62 this.mdsalApiManager = mdsalApiManager;
63 this.alivenessMonitorService = alivenessMonitorService;
67 protected InstanceIdentifier<FlowCapableNodeConnector> getWildCardPath() {
68 return InstanceIdentifier.create(Nodes.class).child(Node.class).child(NodeConnector.class)
69 .augmentation(FlowCapableNodeConnector.class);
73 protected InterfaceInventoryStateListener getDataTreeChangeListener() {
74 return InterfaceInventoryStateListener.this;
78 protected void remove(InstanceIdentifier<FlowCapableNodeConnector> key,
79 FlowCapableNodeConnector flowCapableNodeConnectorOld) {
80 LOG.debug("Received NodeConnector Remove Event: {}, {}", key, flowCapableNodeConnectorOld);
81 String portName = flowCapableNodeConnectorOld.getName();
82 NodeConnectorId nodeConnectorIdNew = InstanceIdentifier.keyOf(key.firstIdentifierOf(NodeConnector.class)).getId();
84 //VM Migration: Skip OFPPR_DELETE event received after OFPPR_ADD for same interface from Older DPN
85 NodeConnectorId nodeConnectorIdOld = IfmUtil.getNodeConnectorIdFromInterface(portName, dataBroker);
86 if(nodeConnectorIdOld != null && !nodeConnectorIdNew.equals(nodeConnectorIdOld)) {
87 LOG.info("Skipping the NodeConnector Remove Event received for the interface exists in newer DPN: {}, {}", nodeConnectorIdNew, nodeConnectorIdOld);
91 remove(nodeConnectorIdNew, nodeConnectorIdOld, flowCapableNodeConnectorOld, portName);
95 protected void update(InstanceIdentifier<FlowCapableNodeConnector> key, FlowCapableNodeConnector fcNodeConnectorOld,
96 FlowCapableNodeConnector fcNodeConnectorNew) {
97 LOG.debug("Received NodeConnector Update Event: {}, {}, {}", key, fcNodeConnectorOld, fcNodeConnectorNew);
98 String portName = fcNodeConnectorNew.getName();
99 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
101 InterfaceStateUpdateWorker portStateUpdateWorker = new InterfaceStateUpdateWorker(key, fcNodeConnectorOld,
102 fcNodeConnectorNew, portName);
103 coordinator.enqueueJob(portName, portStateUpdateWorker, 3);
107 protected void add(InstanceIdentifier<FlowCapableNodeConnector> key, FlowCapableNodeConnector fcNodeConnectorNew) {
108 LOG.debug("Received NodeConnector Add Event: {}, {}", key, fcNodeConnectorNew);
109 String portName = fcNodeConnectorNew.getName();
110 NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(key.firstIdentifierOf(NodeConnector.class)).getId();
112 //VM Migration: Delete existing interface entry for older DPN
113 NodeConnectorId nodeConnectorIdOld = IfmUtil.getNodeConnectorIdFromInterface(portName, dataBroker);
114 if(nodeConnectorIdOld != null && !nodeConnectorId.equals(nodeConnectorIdOld)) {
115 LOG.info("Received NodeConnector Remove Event for the interface exists in older DPN: {}, {}", nodeConnectorId, nodeConnectorIdOld);
116 remove(nodeConnectorId, nodeConnectorIdOld, fcNodeConnectorNew, portName);
119 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
121 InterfaceStateAddWorker ifStateAddWorker = new InterfaceStateAddWorker(idManager, nodeConnectorId,
122 fcNodeConnectorNew, portName);
123 coordinator.enqueueJob(portName, ifStateAddWorker, 3);
126 private void remove(NodeConnectorId nodeConnectorIdNew, NodeConnectorId nodeConnectorIdOld,
127 FlowCapableNodeConnector fcNodeConnectorNew, String portName) {
128 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
129 Interface ifState = InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(portName, dataBroker);
131 InterfaceStateRemoveWorker portStateRemoveWorker = new InterfaceStateRemoveWorker(idManager,
132 nodeConnectorIdNew, nodeConnectorIdOld, fcNodeConnectorNew, portName, ifState);
133 coordinator.enqueueJob(portName, portStateRemoveWorker, 3);
136 private class InterfaceStateAddWorker implements Callable {
137 private final NodeConnectorId nodeConnectorId;
138 private final FlowCapableNodeConnector fcNodeConnectorNew;
139 private final String interfaceName;
140 private final IdManagerService idManager;
142 public InterfaceStateAddWorker(IdManagerService idManager, NodeConnectorId nodeConnectorId,
143 FlowCapableNodeConnector fcNodeConnectorNew,
145 this.nodeConnectorId = nodeConnectorId;
146 this.fcNodeConnectorNew = fcNodeConnectorNew;
147 this.interfaceName = portName;
148 this.idManager = idManager;
152 public Object call() throws Exception {
153 // If another renderer(for eg : CSS) needs to be supported, check can be performed here
154 // to call the respective helpers.
155 List<ListenableFuture<Void>> futures = OvsInterfaceStateAddHelper.addState(dataBroker, idManager, mdsalApiManager, alivenessMonitorService, nodeConnectorId,
156 interfaceName, fcNodeConnectorNew);
157 List<InterfaceChildEntry> interfaceChildEntries = getInterfaceChildEntries(dataBroker, interfaceName, nodeConnectorId);
158 for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
159 InterfaceStateAddWorker interfaceStateAddWorker = new InterfaceStateAddWorker(idManager, nodeConnectorId,
160 fcNodeConnectorNew, interfaceChildEntry.getChildInterface());
161 DataStoreJobCoordinator.getInstance().enqueueJob(interfaceName, interfaceStateAddWorker);
167 public String toString() {
168 return "InterfaceStateAddWorker{" +
169 "nodeConnectorId=" + nodeConnectorId +
170 ", fcNodeConnectorNew=" + fcNodeConnectorNew +
171 ", interfaceName='" + interfaceName + '\'' +
176 private class InterfaceStateUpdateWorker implements Callable {
177 private InstanceIdentifier<FlowCapableNodeConnector> key;
178 private final FlowCapableNodeConnector fcNodeConnectorOld;
179 private final FlowCapableNodeConnector fcNodeConnectorNew;
180 private String interfaceName;
183 public InterfaceStateUpdateWorker(InstanceIdentifier<FlowCapableNodeConnector> key,
184 FlowCapableNodeConnector fcNodeConnectorOld,
185 FlowCapableNodeConnector fcNodeConnectorNew,
188 this.fcNodeConnectorOld = fcNodeConnectorOld;
189 this.fcNodeConnectorNew = fcNodeConnectorNew;
190 this.interfaceName = portName;
194 public Object call() throws Exception {
195 // If another renderer(for eg : CSS) needs to be supported, check can be performed here
196 // to call the respective helpers.
197 List<ListenableFuture<Void>> futures = OvsInterfaceStateUpdateHelper.updateState(key, alivenessMonitorService, dataBroker, interfaceName,
198 fcNodeConnectorNew, fcNodeConnectorOld);
199 NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(key.firstIdentifierOf(NodeConnector.class)).getId();
200 List<InterfaceChildEntry> interfaceChildEntries = getInterfaceChildEntries(dataBroker, interfaceName, nodeConnectorId);
201 for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
202 InterfaceStateUpdateWorker interfaceStateUpdateWorker = new InterfaceStateUpdateWorker(key, fcNodeConnectorOld,
203 fcNodeConnectorNew, interfaceChildEntry.getChildInterface());
204 DataStoreJobCoordinator.getInstance().enqueueJob(interfaceName, interfaceStateUpdateWorker);
210 public String toString() {
211 return "InterfaceStateUpdateWorker{" +
213 ", fcNodeConnectorOld=" + fcNodeConnectorOld +
214 ", fcNodeConnectorNew=" + fcNodeConnectorNew +
215 ", interfaceName='" + interfaceName + '\'' +
220 private class InterfaceStateRemoveWorker implements Callable {
221 private final NodeConnectorId nodeConnectorIdNew;
222 private final NodeConnectorId nodeConnectorIdOld;
223 FlowCapableNodeConnector fcNodeConnectorOld;
224 private final String interfaceName;
225 private final IdManagerService idManager;
226 private final Interface ifState;
228 public InterfaceStateRemoveWorker(IdManagerService idManager, NodeConnectorId nodeConnectorIdNew,
229 NodeConnectorId nodeConnectorIdOld,
230 FlowCapableNodeConnector fcNodeConnectorOld,
231 String portName,Interface ifState) {
232 this.nodeConnectorIdNew = nodeConnectorIdNew;
233 this.nodeConnectorIdOld = nodeConnectorIdOld;
234 this.fcNodeConnectorOld = fcNodeConnectorOld;
235 this.interfaceName = portName;
236 this.idManager = idManager;
237 this.ifState = ifState;
241 public Object call() throws Exception {
242 // If another renderer(for eg : CSS) needs to be supported, check can be performed here
243 // to call the respective helpers.
244 List<ListenableFuture<Void>> futures = OvsInterfaceStateRemoveHelper.removeInterfaceStateConfiguration(idManager, mdsalApiManager, alivenessMonitorService,
245 nodeConnectorIdNew, nodeConnectorIdOld, dataBroker, interfaceName, fcNodeConnectorOld, ifState);
247 List<InterfaceChildEntry> interfaceChildEntries = getInterfaceChildEntries(dataBroker, interfaceName, nodeConnectorIdNew);
248 for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
249 // Fetch all interfaces on this port and trigger remove worker for each of them
250 InterfaceStateRemoveWorker interfaceStateRemoveWorker = new InterfaceStateRemoveWorker(idManager,
251 nodeConnectorIdNew, nodeConnectorIdOld, fcNodeConnectorOld, interfaceChildEntry.getChildInterface(),
252 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceChildEntry.getChildInterface(), dataBroker));
253 DataStoreJobCoordinator.getInstance().enqueueJob(interfaceName, interfaceStateRemoveWorker);
260 public String toString() {
261 return "InterfaceStateRemoveWorker{" +
262 "nodeConnectorIdNew=" + nodeConnectorIdNew +
263 ", nodeConnectorIdOld=" + nodeConnectorIdOld +
264 ", fcNodeConnectorOld=" + fcNodeConnectorOld +
265 ", interfaceName='" + interfaceName + '\'' +
270 public static List<InterfaceChildEntry> getInterfaceChildEntries(DataBroker dataBroker, String interfaceName, NodeConnectorId nodeConnectorId) {
271 InterfaceParentEntry interfaceParentEntry =
272 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceName, dataBroker);
273 if (interfaceParentEntry != null && interfaceParentEntry.getInterfaceChildEntry() != null) {
274 return interfaceParentEntry.getInterfaceChildEntry();
276 StringBuilder parentInterface = new StringBuilder(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
277 parentInterface.append(IfmConstants.OF_URI_SEPARATOR);
278 parentInterface.append(interfaceName);
279 String dpnId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
280 interfaceParentEntry = InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(parentInterface.toString(), dataBroker);
281 if (interfaceParentEntry != null && interfaceParentEntry.getInterfaceChildEntry() != null) {
282 return interfaceParentEntry.getInterfaceChildEntry();
284 return new ArrayList<>();