Merge "Adding dest IP/MAC in ArpResponseReceived"
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / listeners / CacheInterfaceConfigListener.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.interfacemanager.listeners;
10
11 import java.util.Collection;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * This class listens for interface creation/removal/update in Configuration DS.
31  * This is used to handle interfaces for base of-ports.
32  */
33 @Singleton
34 public class CacheInterfaceConfigListener implements ClusteredDataTreeChangeListener<Interface> {
35     private static final Logger LOG = LoggerFactory.getLogger(CacheInterfaceConfigListener.class);
36     private final ListenerRegistration<CacheInterfaceConfigListener> registration;
37     private final DataTreeIdentifier<Interface> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
38             getWildcardPath());
39
40     @Inject
41     public CacheInterfaceConfigListener(final DataBroker dataBroker) {
42         LOG.trace("Registering on path: {}", treeId);
43         registration = dataBroker.registerDataTreeChangeListener(treeId, CacheInterfaceConfigListener.this);
44     }
45
46     @PreDestroy
47     public void close() {
48         if (registration != null) {
49             registration.close();
50         }
51     }
52
53     protected InstanceIdentifier<Interface> getWildcardPath() {
54         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
55     }
56
57     @Override
58     public void onDataTreeChanged(Collection<DataTreeModification<Interface>> changes) {
59         for (DataTreeModification<Interface> change : changes) {
60             final InstanceIdentifier<Interface> key = change.getRootPath().getRootIdentifier();
61             final DataObjectModification<Interface> mod = change.getRootNode();
62             switch (mod.getModificationType()) {
63                 case DELETE:
64                     InterfaceManagerCommonUtils.removeFromInterfaceCache(mod.getDataBefore());
65                     break;
66                 case SUBTREE_MODIFIED:
67                 case WRITE:
68                     InterfaceManagerCommonUtils.addInterfaceToCache(mod.getDataAfter());
69                     break;
70                 default:
71                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
72             }
73         }
74     }
75 }