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 nodeConnectorId = 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 Interface ifState = InterfaceManagerCommonUtils.getInterfaceStateFromDS(portName);
86 NodeConnectorId nodeConnectorIdOld = IfmUtil.getNodeConnectorIdFromInterface(ifState);
87 if(nodeConnectorIdOld != null && !nodeConnectorId.equals(nodeConnectorIdOld)) {
88 LOG.debug("Dropping the NodeConnector Remove Event for the interface: {}, {}, {}", portName, nodeConnectorId, nodeConnectorIdOld);
91 remove(nodeConnectorId, nodeConnectorIdOld, flowCapableNodeConnectorOld, portName, ifState);
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 Interface ifState = InterfaceManagerCommonUtils.getInterfaceStateFromDS(portName);
114 NodeConnectorId nodeConnectorIdOld = IfmUtil.getNodeConnectorIdFromInterface(ifState);
115 if (nodeConnectorIdOld != null && !nodeConnectorId.equals(nodeConnectorIdOld)) {
116 LOG.debug("Triggering NodeConnector Remove Event for the interface: {}, {}, {}", portName, nodeConnectorId, nodeConnectorIdOld);
117 remove(nodeConnectorId, nodeConnectorIdOld, fcNodeConnectorNew, portName, ifState);
120 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, Interface ifState) {
128 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
129 InterfaceStateRemoveWorker portStateRemoveWorker = new InterfaceStateRemoveWorker(idManager,
130 nodeConnectorIdNew, nodeConnectorIdOld, fcNodeConnectorNew, portName, ifState);
131 coordinator.enqueueJob(portName, portStateRemoveWorker, 3);
134 private class InterfaceStateAddWorker implements Callable {
135 private final NodeConnectorId nodeConnectorId;
136 private final FlowCapableNodeConnector fcNodeConnectorNew;
137 private final String interfaceName;
138 private final IdManagerService idManager;
140 public InterfaceStateAddWorker(IdManagerService idManager, NodeConnectorId nodeConnectorId,
141 FlowCapableNodeConnector fcNodeConnectorNew,
143 this.nodeConnectorId = nodeConnectorId;
144 this.fcNodeConnectorNew = fcNodeConnectorNew;
145 this.interfaceName = portName;
146 this.idManager = idManager;
150 public Object call() throws Exception {
151 // If another renderer(for eg : CSS) needs to be supported, check can be performed here
152 // to call the respective helpers.
153 List<ListenableFuture<Void>> futures = OvsInterfaceStateAddHelper.addState(dataBroker, idManager, mdsalApiManager, alivenessMonitorService, nodeConnectorId,
154 interfaceName, fcNodeConnectorNew);
155 List<InterfaceChildEntry> interfaceChildEntries = getInterfaceChildEntries(dataBroker, interfaceName, nodeConnectorId);
156 for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
157 InterfaceStateAddWorker interfaceStateAddWorker = new InterfaceStateAddWorker(idManager, nodeConnectorId,
158 fcNodeConnectorNew, interfaceChildEntry.getChildInterface());
159 DataStoreJobCoordinator.getInstance().enqueueJob(interfaceName, interfaceStateAddWorker);
165 public String toString() {
166 return "InterfaceStateAddWorker{" +
167 "nodeConnectorId=" + nodeConnectorId +
168 ", fcNodeConnectorNew=" + fcNodeConnectorNew +
169 ", interfaceName='" + interfaceName + '\'' +
174 private class InterfaceStateUpdateWorker implements Callable {
175 private InstanceIdentifier<FlowCapableNodeConnector> key;
176 private final FlowCapableNodeConnector fcNodeConnectorOld;
177 private final FlowCapableNodeConnector fcNodeConnectorNew;
178 private String interfaceName;
181 public InterfaceStateUpdateWorker(InstanceIdentifier<FlowCapableNodeConnector> key,
182 FlowCapableNodeConnector fcNodeConnectorOld,
183 FlowCapableNodeConnector fcNodeConnectorNew,
186 this.fcNodeConnectorOld = fcNodeConnectorOld;
187 this.fcNodeConnectorNew = fcNodeConnectorNew;
188 this.interfaceName = portName;
192 public Object call() throws Exception {
193 // If another renderer(for eg : CSS) needs to be supported, check can be performed here
194 // to call the respective helpers.
195 List<ListenableFuture<Void>> futures = OvsInterfaceStateUpdateHelper.updateState(key, alivenessMonitorService, dataBroker, interfaceName,
196 fcNodeConnectorNew, fcNodeConnectorOld);
197 NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(key.firstIdentifierOf(NodeConnector.class)).getId();
198 List<InterfaceChildEntry> interfaceChildEntries = getInterfaceChildEntries(dataBroker, interfaceName, nodeConnectorId);
199 for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
200 InterfaceStateUpdateWorker interfaceStateUpdateWorker = new InterfaceStateUpdateWorker(key, fcNodeConnectorOld,
201 fcNodeConnectorNew, interfaceChildEntry.getChildInterface());
202 DataStoreJobCoordinator.getInstance().enqueueJob(interfaceName, interfaceStateUpdateWorker);
208 public String toString() {
209 return "InterfaceStateUpdateWorker{" +
211 ", fcNodeConnectorOld=" + fcNodeConnectorOld +
212 ", fcNodeConnectorNew=" + fcNodeConnectorNew +
213 ", interfaceName='" + interfaceName + '\'' +
218 private class InterfaceStateRemoveWorker implements Callable {
219 private final NodeConnectorId nodeConnectorIdNew;
220 private final NodeConnectorId nodeConnectorIdOld;
221 FlowCapableNodeConnector fcNodeConnectorOld;
222 private final String interfaceName;
223 private final IdManagerService idManager;
224 private final Interface ifState;
226 public InterfaceStateRemoveWorker(IdManagerService idManager, NodeConnectorId nodeConnectorIdNew,
227 NodeConnectorId nodeConnectorIdOld,
228 FlowCapableNodeConnector fcNodeConnectorOld,
229 String portName,Interface ifState) {
230 this.nodeConnectorIdNew = nodeConnectorIdNew;
231 this.nodeConnectorIdOld = nodeConnectorIdOld;
232 this.fcNodeConnectorOld = fcNodeConnectorOld;
233 this.interfaceName = portName;
234 this.idManager = idManager;
235 this.ifState = ifState;
239 public Object call() throws Exception {
240 // If another renderer(for eg : CSS) needs to be supported, check can be performed here
241 // to call the respective helpers.
242 List<ListenableFuture<Void>> futures = OvsInterfaceStateRemoveHelper.removeInterfaceStateConfiguration(idManager, mdsalApiManager, alivenessMonitorService,
243 nodeConnectorIdNew, nodeConnectorIdOld, dataBroker, interfaceName, fcNodeConnectorOld, ifState);
245 List<InterfaceChildEntry> interfaceChildEntries = getInterfaceChildEntries(dataBroker, interfaceName, nodeConnectorIdNew);
246 for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
247 // Fetch all interfaces on this port and trigger remove worker for each of them
248 InterfaceStateRemoveWorker interfaceStateRemoveWorker = new InterfaceStateRemoveWorker(idManager,
249 nodeConnectorIdNew, nodeConnectorIdOld, fcNodeConnectorOld, interfaceChildEntry.getChildInterface(),
250 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceChildEntry.getChildInterface(), dataBroker));
251 DataStoreJobCoordinator.getInstance().enqueueJob(interfaceName, interfaceStateRemoveWorker);
257 public String toString() {
258 return "InterfaceStateRemoveWorker{" +
259 "nodeConnectorIdNew=" + nodeConnectorIdNew +
260 ", nodeConnectorIdOld=" + nodeConnectorIdOld +
261 ", fcNodeConnectorOld=" + fcNodeConnectorOld +
262 ", interfaceName='" + interfaceName + '\'' +
267 public static List<InterfaceChildEntry> getInterfaceChildEntries(DataBroker dataBroker, String interfaceName, NodeConnectorId nodeConnectorId) {
268 InterfaceParentEntry interfaceParentEntry =
269 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceName, dataBroker);
270 if (interfaceParentEntry != null && interfaceParentEntry.getInterfaceChildEntry() != null) {
271 return interfaceParentEntry.getInterfaceChildEntry();
273 StringBuilder parentInterface = new StringBuilder(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
274 parentInterface.append(IfmConstants.OF_URI_SEPARATOR);
275 parentInterface.append(interfaceName);
276 interfaceParentEntry = InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(parentInterface.toString(), dataBroker);
277 if (interfaceParentEntry != null && interfaceParentEntry.getInterfaceChildEntry() != null) {
278 return interfaceParentEntry.getInterfaceChildEntry();
280 return new ArrayList<>();