Integrating FAAS renderer with the faas fabric mapping services. Also,
[groupbasedpolicy.git] / renderers / faas / src / main / java / org / opendaylight / groupbasedpolicy / renderer / faas / FaasTenantManagerListener.java
1 /*
2  * Copyright (c) 2015 Huawei Technologies 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 package org.opendaylight.groupbasedpolicy.renderer.faas;
9
10 import java.util.concurrent.ScheduledExecutorService;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class FaasTenantManagerListener implements DataChangeListener {
23
24     private static final Logger LOG = LoggerFactory.getLogger(FaasTenantManagerListener.class);
25     private final ScheduledExecutorService executor;
26     private final TenantId gbpTenantId;
27     private final Uuid faasTenantId;
28     private final FaasPolicyManager policyManager;
29
30     public FaasTenantManagerListener(FaasPolicyManager policyManager, TenantId gbpTenantId, Uuid faasTenantId,
31             ScheduledExecutorService executor) {
32         this.executor = executor;
33         this.faasTenantId = faasTenantId;
34         this.gbpTenantId = gbpTenantId;
35         this.policyManager = policyManager;
36     }
37
38     @Override
39     public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
40         executor.execute(new Runnable() {
41
42             public void run() {
43                 executeEvent(change);
44             }
45         });
46     }
47
48     private void executeEvent(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
49         // Remove
50         for (InstanceIdentifier<?> iid : change.getRemovedPaths()) {
51             DataObject old = change.getOriginalData().get(iid);
52             if (old == null) {
53                 continue;
54             }
55             if (old instanceof Tenant) {
56                 Tenant tenant = (Tenant) old;
57                 if (tenant.getId().equals(gbpTenantId)) {
58                     LOG.info("Removed gbp Tenant {}  -- faas Tenant {}", gbpTenantId.getValue(),
59                             faasTenantId.getValue());
60                     this.policyManager.removeTenantLogicalNetwork(gbpTenantId, faasTenantId);
61                 }
62             }
63         }
64     }
65 }