Reconnect port support
[unimgr.git] / netvirt / src / main / java / org / opendaylight / unimgr / mef / netvirt / UniAwareListener.java
1 /*
2  * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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.unimgr.mef.netvirt;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.unimgr.api.UnimgrDataTreeChangeListener;
16 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.Uni;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class UniAwareListener extends UnimgrDataTreeChangeListener<Uni> {
22     private static final Logger Log = LoggerFactory.getLogger(UniAwareListener.class);
23     private ListenerRegistration<UniAwareListener> uniListenerRegistration;
24     IUniAwareService serviceSubscribe;
25
26     public UniAwareListener(final DataBroker dataBroker, final IUniAwareService serviceSubscribe) {
27         super(dataBroker);
28         this.serviceSubscribe = serviceSubscribe;
29         registerListener();
30     }
31
32     public void registerListener() {
33         try {
34             final DataTreeIdentifier<Uni> dataTreeIid = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
35                     MefInterfaceUtils.getUniListInstanceIdentifier());
36             uniListenerRegistration = dataBroker.registerDataTreeChangeListener(dataTreeIid, this);
37             Log.info("UniAwareListener created and registered for service {}", serviceSubscribe);
38         } catch (final Exception e) {
39             Log.error("UniAwareListener registration failed !", e);
40             throw new IllegalStateException("UniAwareListener failed.", e);
41         }
42     }
43
44     @Override
45     public void close() throws Exception {
46         uniListenerRegistration.close();
47     }
48
49     @Override
50     public void add(DataTreeModification<Uni> newDataObject) {
51         if (newDataObject.getRootPath() != null && newDataObject.getRootNode() != null) {
52             Log.info("Uni {} is operational", newDataObject.getRootNode().getIdentifier());
53             // Uni Id is same in interface and service
54             serviceSubscribe.connectUni(newDataObject.getRootNode().getDataAfter().getUniId().getValue());
55         }
56     }
57
58     @Override
59     public void remove(DataTreeModification<Uni> removedDataObject) {
60         if (removedDataObject.getRootPath() != null && removedDataObject.getRootNode() != null) {
61             Log.info("Remove Uni {} from operational", removedDataObject.getRootNode().getIdentifier());
62             // Uni Id is same in interface and service
63             serviceSubscribe.disconnectUni(removedDataObject.getRootNode().getDataBefore().getUniId().getValue());
64         }
65     }
66
67     @Override
68     public void update(DataTreeModification<Uni> modifiedDataObject) {
69     }
70 }