e86d9048dd208194f5ba43c4881b79dd782ff8f6
[unimgr.git] / netvirt / src / main / java / org / opendaylight / unimgr / mef / netvirt / TenantUniListener.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.services.rev150526.MefServices;
17 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.mef.services.MefService;
18 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.Uni;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.common.base.Optional;
24
25 public class TenantUniListener extends UnimgrDataTreeChangeListener<Uni> {
26
27     private static final Logger log = LoggerFactory.getLogger(TenantUniListener.class);
28     private ListenerRegistration<TenantUniListener> evcListenerRegistration;
29
30     public TenantUniListener(final DataBroker dataBroker) {
31         super(dataBroker);
32
33         registerListener();
34     }
35
36     public void registerListener() {
37         try {
38             final DataTreeIdentifier<Uni> dataTreeIid = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
39                     MefUtils.getUniListInterfaceInstanceIdentifier());
40             evcListenerRegistration = dataBroker.registerDataTreeChangeListener(dataTreeIid, this);
41             log.info("TenantUniListener created and registered");
42         } catch (final Exception e) {
43             log.error("TenantUniListener registration failed !", e);
44             throw new IllegalStateException("Evc registration Listener failed.", e);
45         }
46     }
47
48     @Override
49     public void close() throws Exception {
50         evcListenerRegistration.close();
51     }
52
53     @Override
54     public void add(DataTreeModification<Uni> newDataObject) {
55         log.info("received add Uni notification");
56         handleUniChanged(newDataObject.getRootNode().getDataAfter());
57     }
58
59     @Override
60     public void remove(DataTreeModification<Uni> removedDataObject) {
61     }
62
63     @Override
64     public void update(DataTreeModification<Uni> modifiedDataObject) {
65         log.info("received update Uni notification");
66         handleUniChanged(modifiedDataObject.getRootNode().getDataAfter());
67     }
68
69     private void handleUniChanged(Uni uni) {
70         if (!TenantEnhancerUtils.isUniTenanted(uni)) {
71             return;
72         }
73
74         String tenant = uni.getTenantId();
75
76         Optional<MefServices> optionalServices = MdsalUtils.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
77                 MefUtils.getMefServicesInstanceIdentifier());
78         if (!optionalServices.isPresent()) {
79             return;
80         }
81         for (MefService service : optionalServices.get().getMefService()) {
82             for (org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.mef.services.mef.service.evc.unis.Uni serviceUni : service
83                     .getEvc().getUnis().getUni()) {
84                 if (!TenantEnhancerUtils.isServiceTenanted(service) && serviceUni.getUniId().equals(uni.getUniId())) {
85                     log.info("instance identifier is {}", MefUtils.getMefServiceInstanceIdentifier(service.getSvcId()));
86                     TenantEnhancerUtils.updateService(dataBroker, tenant, service);
87                 }
88             }
89         }
90     }
91 }