2 * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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
9 package org.opendaylight.unimgr.mef.netvirt;
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;
23 import com.google.common.base.Optional;
25 public class TenantUniListener extends UnimgrDataTreeChangeListener<Uni> {
27 private static final Logger log = LoggerFactory.getLogger(TenantUniListener.class);
28 private ListenerRegistration<TenantUniListener> evcListenerRegistration;
30 public TenantUniListener(final DataBroker dataBroker) {
36 public void registerListener() {
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);
49 public void close() throws Exception {
50 evcListenerRegistration.close();
54 public void add(DataTreeModification<Uni> newDataObject) {
55 log.info("received add Uni notification");
56 handleUniChanged(newDataObject.getRootNode().getDataAfter());
60 public void remove(DataTreeModification<Uni> removedDataObject) {
64 public void update(DataTreeModification<Uni> modifiedDataObject) {
65 log.info("received update Uni notification");
66 handleUniChanged(modifiedDataObject.getRootNode().getDataAfter());
69 private void handleUniChanged(Uni uni) {
70 if (!TenantEnhancerUtils.isUniTenanted(uni)) {
74 String tenant = uni.getTenantId();
76 Optional<MefServices> optionalServices = MdsalUtils.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
77 MefUtils.getMefServicesInstanceIdentifier());
78 if (!optionalServices.isPresent()) {
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);