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