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 java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.unimgr.api.UnimgrDataTreeChangeListener;
18 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.mef.services.mef.service.mef.service.choice.evc.choice.Evc;
19 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.mef.services.MefService;
20 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.mef.services.mef.service.mef.service.choice.evc.choice.evc.unis.Uni;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.common.base.Optional;
27 public class TenantlessEvcListener extends UnimgrDataTreeChangeListener<MefService> {
29 private static final Logger log = LoggerFactory.getLogger(TenantlessEvcListener.class);
30 private ListenerRegistration<TenantlessEvcListener> evcListenerRegistration;
32 public TenantlessEvcListener(final DataBroker dataBroker) {
38 public void registerListener() {
40 final DataTreeIdentifier<MefService> dataTreeIid = new DataTreeIdentifier<>(
41 LogicalDatastoreType.CONFIGURATION, MefUtils.getMefServiceInstanceIdentifier());
42 evcListenerRegistration = dataBroker.registerDataTreeChangeListener(dataTreeIid, this);
43 log.info("TenantlessEvcListener created and registered");
44 } catch (final Exception e) {
45 log.error("TenantlessEvcListener registration failed !", e);
46 throw new IllegalStateException("Evc registration Listener failed.", e);
51 public void close() throws Exception {
52 evcListenerRegistration.close();
56 public void add(DataTreeModification<MefService> newDataObject) {
57 if (newDataObject.getRootPath() != null && newDataObject.getRootNode() != null) {
58 log.info("service {} created", newDataObject.getRootNode().getIdentifier());
59 handleService(newDataObject.getRootNode().getDataAfter());
64 public void remove(DataTreeModification<MefService> removedDataObject) {
68 public void update(DataTreeModification<MefService> modifiedDataObject) {
69 if (modifiedDataObject.getRootPath() != null && modifiedDataObject.getRootNode() != null) {
70 log.info("service {} updated", modifiedDataObject.getRootNode().getIdentifier());
71 handleService(modifiedDataObject.getRootNode().getDataAfter());
75 private void handleService(MefService service) {
76 if (TenantEnhancerUtils.isServiceTenanted(service)) {
77 log.info("Service {} is already connected to a Service", service.getSvcId().getValue());
80 Evc evc = TenantEnhancerUtils.GetEvc(service);
81 if (evc.getUnis() == null) {
82 log.info("No UNI's in service {}, exiting", service.getSvcId().getValue());
85 List<Uni> unis = evc.getUnis().getUni();
86 for (Uni uni : unis) {
87 Optional<org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.Uni> optonalUniInterface = MdsalUtils
88 .read(dataBroker, LogicalDatastoreType.CONFIGURATION,
89 MefUtils.getUniInstanceIdentifier(uni.getUniId().getValue()));
90 if (optonalUniInterface.isPresent()) {
91 org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.Uni uniInterface = optonalUniInterface
93 if (TenantEnhancerUtils.isUniTenanted(uniInterface)) {
94 String tenant = uniInterface.getTenantId();
95 log.info("updating service {} with tenant {}", service.getSvcId().getValue(), tenant);
96 TenantEnhancerUtils.updateService(dataBroker, tenant, service);
100 log.info("Couldn't find uni {}", uni.getUniId());