MDSAL-API Migration
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / listeners / InterfaceStateListener.java
1 /*
2  * Copyright (c) 2016, 2017 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
9 package org.opendaylight.genius.itm.listeners;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Objects;
15 import java.util.Optional;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
20 import org.opendaylight.genius.itm.cache.TunnelStateCache;
21 import org.opendaylight.genius.itm.cache.UnprocessedTunnelsStateCache;
22 import org.opendaylight.genius.itm.confighelpers.ItmTunnelAggregationHelper;
23 import org.opendaylight.genius.itm.confighelpers.ItmTunnelStateAddHelper;
24 import org.opendaylight.genius.itm.confighelpers.ItmTunnelStateRemoveHelper;
25 import org.opendaylight.genius.itm.globals.ITMConstants;
26 import org.opendaylight.genius.itm.impl.ITMBatchingUtils;
27 import org.opendaylight.genius.itm.impl.ItmUtils;
28 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
29 import org.opendaylight.mdsal.binding.api.DataBroker;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.mdsal.common.api.ReadFailedException;
32 import org.opendaylight.serviceutils.tools.listener.AbstractSyncDataTreeChangeListener;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelOperStatus;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnels_state.StateTunnelList;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnels_state.StateTunnelListBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnels_state.StateTunnelListKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 @Singleton
45 public class InterfaceStateListener extends AbstractSyncDataTreeChangeListener<Interface> {
46
47     private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateListener.class);
48     private static final Logger EVENT_LOGGER = LoggerFactory.getLogger("GeniusEventLogger");
49
50     private final DataBroker dataBroker;
51     private final JobCoordinator jobCoordinator;
52     private final IInterfaceManager interfaceManager;
53     private final ItmTunnelAggregationHelper tunnelAggregationHelper;
54     private final TunnelStateCache tunnelStateCache;
55     private final UnprocessedTunnelsStateCache unprocessedTunnelsStateCache;
56
57     @Inject
58     public InterfaceStateListener(final DataBroker dataBroker, IInterfaceManager iinterfacemanager,
59             final ItmTunnelAggregationHelper tunnelAggregation, JobCoordinator jobCoordinator,
60             TunnelStateCache tunnelStateCache, UnprocessedTunnelsStateCache unprocessedTunnelsStateCache) {
61         super(dataBroker, LogicalDatastoreType.OPERATIONAL,
62               InstanceIdentifier.create(InterfacesState.class).child(Interface.class));
63         this.dataBroker = dataBroker;
64         this.jobCoordinator = jobCoordinator;
65         this.interfaceManager = iinterfacemanager;
66         this.tunnelAggregationHelper = tunnelAggregation;
67         this.tunnelStateCache = tunnelStateCache;
68         this.unprocessedTunnelsStateCache = unprocessedTunnelsStateCache;
69     }
70
71     @Override
72     public void add(@NonNull InstanceIdentifier<Interface> instanceIdentifier, @NonNull Interface iface) {
73         LOG.trace("Interface added: {}", iface);
74         EVENT_LOGGER.debug("ITM-InterfaceState,ADD {}", iface.getName());
75         if (ItmUtils.isItmIfType(iface.getType())) {
76             LOG.debug("Interface of type Tunnel added: {}", iface.getName());
77             jobCoordinator.enqueueJob(ITMConstants.ITM_PREFIX + iface.getName(), () -> ItmTunnelStateAddHelper
78                     .addTunnel(iface, interfaceManager, dataBroker));
79             if (tunnelAggregationHelper.isTunnelAggregationEnabled()) {
80                 tunnelAggregationHelper.updateLogicalTunnelState(iface, ItmTunnelAggregationHelper.ADD_TUNNEL,
81                                                                  dataBroker);
82             }
83         }
84     }
85
86     @Override
87     public void remove(@NonNull InstanceIdentifier<Interface> instanceIdentifier, @NonNull Interface iface) {
88         LOG.trace("Interface deleted: {}", iface);
89         EVENT_LOGGER.debug("ITM-InterfaceState,REMOVE {}", iface.getName());
90         if (ItmUtils.isItmIfType(iface.getType())) {
91             LOG.debug("Tunnel interface deleted: {}", iface.getName());
92             jobCoordinator.enqueueJob(ITMConstants.ITM_PREFIX + iface.getName(),
93                 () -> ItmTunnelStateRemoveHelper.removeTunnel(iface));
94             if (tunnelAggregationHelper.isTunnelAggregationEnabled()) {
95                 tunnelAggregationHelper.updateLogicalTunnelState(iface, ItmTunnelAggregationHelper.DEL_TUNNEL,
96                                                                  dataBroker);
97             }
98         }
99     }
100
101     @Override
102     public void update(@NonNull InstanceIdentifier<Interface> instanceIdentifier, @NonNull Interface originalInterface,
103                        @NonNull Interface updatedInterface) {
104         /*
105          * update contains only delta, may not include iftype Note: This assumes
106          * type can't be edited on the fly
107          */
108         if (ItmUtils.isItmIfType(originalInterface.getType())) {
109             EVENT_LOGGER.debug("ITM-InterfaceState,UPDATE {}", updatedInterface.getName());
110             LOG.trace("Interface updated. Old: {} New: {}", originalInterface, updatedInterface);
111             OperStatus operStatus = updatedInterface.getOperStatus();
112             if (!Objects.equals(originalInterface.getOperStatus(), updatedInterface.getOperStatus())) {
113                 LOG.debug("Tunnel Interface {} changed state to {}", originalInterface.getName(), operStatus);
114                 jobCoordinator.enqueueJob(ITMConstants.ITM_PREFIX + originalInterface.getName(),
115                     () -> updateTunnel(updatedInterface));
116             }
117             if (tunnelAggregationHelper.isTunnelAggregationEnabled()) {
118                 tunnelAggregationHelper.updateLogicalTunnelState(originalInterface, updatedInterface,
119                                                                  ItmTunnelAggregationHelper.MOD_TUNNEL, dataBroker);
120             }
121         }
122     }
123
124     private List<ListenableFuture<Void>> updateTunnel(Interface updated) throws Exception {
125         LOG.debug("Invoking ItmTunnelStateUpdateHelper for Interface {} ", updated);
126         StateTunnelListKey tlKey = ItmUtils.getTunnelStateKey(updated);
127         LOG.trace("TunnelStateKey: {} for interface: {}", tlKey, updated.getName());
128         StateTunnelListBuilder stlBuilder;
129         TunnelOperStatus tunnelOperStatus;
130         try {
131             InstanceIdentifier<StateTunnelList> stListId = ItmUtils.buildStateTunnelListId(tlKey);
132             Optional<StateTunnelList> tunnelsState = tunnelStateCache.get(stListId);
133             boolean tunnelState = OperStatus.Up.equals(updated.getOperStatus());
134             switch (updated.getOperStatus()) {
135                 case Up:
136                     tunnelOperStatus = TunnelOperStatus.Up;
137                     break;
138                 case Down:
139                     tunnelOperStatus = TunnelOperStatus.Down;
140                     break;
141                 case Unknown:
142                     tunnelOperStatus = TunnelOperStatus.Unknown;
143                     break;
144                 default:
145                     tunnelOperStatus = TunnelOperStatus.Ignore;
146             }
147             if (tunnelsState.isPresent()) {
148                 stlBuilder = new StateTunnelListBuilder(tunnelsState.get());
149                 stlBuilder.setTunnelState(tunnelState);
150                 stlBuilder.setOperState(tunnelOperStatus);
151                 StateTunnelList stList = stlBuilder.build();
152                 LOG.trace("Batching the updation of tunnel_state: {} for Id: {}", stList, stListId);
153                 ITMBatchingUtils.updateContainer(stListId, stList, ITMBatchingUtils.EntityType.DEFAULT_OPERATIONAL);
154             } else {
155                 LOG.debug("Tunnel is not yet added but an update has come in for {},so cache it", updated.getName());
156                 unprocessedTunnelsStateCache.add(updated.getName(), tunnelOperStatus);
157             }
158         } catch (ReadFailedException e) {
159             LOG.debug("TunnelState cache returned with error while processing {}", updated.getName());
160         }
161         return Collections.emptyList();
162     }
163 }