several fixex + bugfix with federation
[unimgr.git] / netvirt / src / main / java / org / opendaylight / unimgr / mef / netvirt / TenantlessEvcListener.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 java.util.List;
12
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;
24
25 import com.google.common.base.Optional;
26
27 public class TenantlessEvcListener extends UnimgrDataTreeChangeListener<MefService> {
28
29     private static final Logger log = LoggerFactory.getLogger(TenantlessEvcListener.class);
30     private ListenerRegistration<TenantlessEvcListener> evcListenerRegistration;
31
32     public TenantlessEvcListener(final DataBroker dataBroker) {
33         super(dataBroker);
34
35         registerListener();
36     }
37
38     public void registerListener() {
39         try {
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);
47         }
48     }
49
50     @Override
51     public void close() throws Exception {
52         evcListenerRegistration.close();
53     }
54
55     @Override
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());
60         }
61     }
62
63     @Override
64     public void remove(DataTreeModification<MefService> removedDataObject) {
65     }
66
67     @Override
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());
72         }
73     }
74
75     private void handleService(MefService service) {
76         if (TenantEnhancerUtils.isServiceTenanted(service)) {
77             log.info("Service {} is already connected to a Service", service.getSvcId().getValue());
78             return;
79         }
80         Evc evc = TenantEnhancerUtils.GetEvc(service);
81         if (evc.getUnis() == null) {
82             log.info("No UNI's in service {}, exiting", service.getSvcId().getValue());
83             return;
84         }
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
92                         .get();
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);
97                     return;
98                 }
99             } else {
100                 log.info("Couldn't find uni {}", uni.getUniId());
101             }
102         }
103     }
104 }