Convert DataChangeListeners to DataTreeChangeListeners
[groupbasedpolicy.git] / renderers / faas / src / main / java / org / opendaylight / groupbasedpolicy / renderer / faas / FaasSubnetManagerListener.java
index 4a3f6aab33f53e5983cdd37813edde7326b9802c..0d97b3bb4c884ef40faaf47e56eff2a81086b6e8 100644 (file)
@@ -1,28 +1,29 @@
 /*
  * Copyright (c) 2015 Huawei Technologies and others. All rights reserved.
- * 
+ *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.groupbasedpolicy.renderer.faas;
 
+import com.google.common.base.Optional;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
-import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ScheduledExecutorService;
-
+import java.util.concurrent.Executor;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
+import org.opendaylight.groupbasedpolicy.util.IetfModelCodec;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Text;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.SubnetBuilder;
@@ -35,24 +36,20 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev15
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.Gateways;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.gateways.Prefixes;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-
-public class FaasSubnetManagerListener implements DataChangeListener {
+public class FaasSubnetManagerListener implements DataTreeChangeListener<Subnet> {
 
     private static final Logger LOG = LoggerFactory.getLogger(FaasSubnetManagerListener.class);
-    private ConcurrentHashMap<SubnetId, Uuid> mappedSubnets = new ConcurrentHashMap<>();
-    private final ScheduledExecutorService executor;
+    private final ConcurrentHashMap<SubnetId, Uuid> mappedSubnets = new ConcurrentHashMap<>();
+    private final Executor executor;
     private final DataBroker dataProvider;
     private final TenantId gbpTenantId;
     private final Uuid faasTenantId;
 
     public FaasSubnetManagerListener(DataBroker dataProvider, TenantId gbpTenantId, Uuid faasTenantId,
-            ScheduledExecutorService executor) {
+            Executor executor) {
         this.executor = executor;
         this.faasTenantId = faasTenantId;
         this.gbpTenantId = gbpTenantId;
@@ -60,51 +57,35 @@ public class FaasSubnetManagerListener implements DataChangeListener {
     }
 
     @Override
-    public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
-        executor.execute(new Runnable() {
-
-            public void run() {
-                executeEvent(change);
-            }
-        });
+    public void onDataTreeChanged(Collection<DataTreeModification<Subnet>> changes) {
+        executor.execute(() -> executeEvent(changes));
     }
 
-    private void executeEvent(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
-        // Create
-        for (DataObject dao : change.getCreatedData().values()) {
-            if (dao instanceof Subnet) {
-                Subnet subnet = (Subnet) dao;
-                LOG.debug("Subnet {} is Created.", subnet.getId().getValue());
-                UlnDatastoreApi.submitSubnetToDs(initSubnetBuilder(subnet).build());
-            }
-        }
-        // Update
-        Map<InstanceIdentifier<?>, DataObject> dao = change.getUpdatedData();
-        for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : dao.entrySet()) {
-            if (entry.getValue() instanceof Subnet) {
-                Subnet subnet = (Subnet) dao;
-                LOG.debug("Subnet {} is Updated.", subnet.getId().getValue());
-                UlnDatastoreApi.submitSubnetToDs(initSubnetBuilder(subnet).build());
-            }
-        }
-        // Remove
-        for (InstanceIdentifier<?> iid : change.getRemovedPaths()) {
-            DataObject old = change.getOriginalData().get(iid);
-            if (old == null) {
-                continue;
-            }
-            if (old instanceof Subnet) {
-                Subnet subnet = (Subnet) old;
-                ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
-                Optional<MappedSubnet> op = DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
-                        FaasIidFactory.mappedSubnetIid(gbpTenantId, subnet.getId()), rwTx);
-                if (op.isPresent()) {
-                    DataStoreHelper.submitToDs(rwTx);
-                }
-                Uuid faasSubnetId = mappedSubnets.remove(subnet.getId());
-                if (faasSubnetId != null) {
-                    UlnDatastoreApi.removeSubnetFromDsIfExists(faasTenantId, faasSubnetId);
-                }
+    private void executeEvent(final Collection<DataTreeModification<Subnet>> changes) {
+        for (DataTreeModification<Subnet> change: changes) {
+            DataObjectModification<Subnet> rootNode = change.getRootNode();
+            switch (rootNode.getModificationType()) {
+                case SUBTREE_MODIFIED:
+                case WRITE:
+                    Subnet updatedSubnet = rootNode.getDataAfter();
+                    LOG.debug("Subnet {} is Updated.", updatedSubnet.getId().getValue());
+                    UlnDatastoreApi.submitSubnetToDs(initSubnetBuilder(updatedSubnet).build());
+                    break;
+                case DELETE:
+                    Subnet deletedSubnet = rootNode.getDataBefore();
+                    ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
+                    Optional<MappedSubnet> op = DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
+                            FaasIidFactory.mappedSubnetIid(gbpTenantId, deletedSubnet.getId()), rwTx);
+                    if (op.isPresent()) {
+                        DataStoreHelper.submitToDs(rwTx);
+                    }
+                    Uuid faasSubnetId = mappedSubnets.remove(deletedSubnet.getId());
+                    if (faasSubnetId != null) {
+                        UlnDatastoreApi.removeSubnetFromDsIfExists(faasTenantId, faasSubnetId);
+                    }
+                    break;
+                default:
+                    break;
             }
         }
     }
@@ -129,11 +110,11 @@ public class FaasSubnetManagerListener implements DataChangeListener {
             List<ExternalGateways> gateways = new ArrayList<>();
             for (Gateways gw : gbpSubnet.getGateways()) {
                 ExternalGatewaysBuilder eb = new ExternalGatewaysBuilder();
-                eb.setExternalGateway(gw.getGateway());
+                eb.setExternalGateway(IetfModelCodec.ipAddress2013(gw.getGateway()));
                 if (gw.getPrefixes() != null) {
-                    List<IpPrefix> ipPrefixes = new ArrayList<>();
+                    List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix> ipPrefixes = new ArrayList<>();
                     for (Prefixes px : gw.getPrefixes()) {
-                        ipPrefixes.add(px.getPrefix());
+                        ipPrefixes.add(IetfModelCodec.ipPrefix2013(px.getPrefix()));
                     }
                     eb.setPrefixes(ipPrefixes);
                 }
@@ -142,15 +123,16 @@ public class FaasSubnetManagerListener implements DataChangeListener {
             builder.setExternalGateways(gateways);
         }
 
-        builder.setIpPrefix(gbpSubnet.getIpPrefix());
+        builder.setIpPrefix(IetfModelCodec.ipPrefix2013(gbpSubnet.getIpPrefix()));
         builder.setUuid(getFaasSubnetId(gbpSubnet.getId()));
         builder.setName(new Text(gbpSubnet.getId().getValue()));
-        if (gbpSubnet.getDescription() != null)
+        if (gbpSubnet.getDescription() != null) {
             builder.setDescription(new Text("gbp-subnet: " + gbpSubnet.getDescription().getValue()));
-        else
+        } else {
             builder.setDescription(new Text("gbp-subnet"));
+        }
         builder.setTenantId(faasTenantId);
-        builder.setVirtualRouterIp(gbpSubnet.getVirtualRouterIp());
+        builder.setVirtualRouterIp(IetfModelCodec.ipAddress2013(gbpSubnet.getVirtualRouterIp()));
         // TODO DNS servers
         builder.setDnsNameservers(null);
         // TODO DHCP server
@@ -185,5 +167,4 @@ public class FaasSubnetManagerListener implements DataChangeListener {
         }
         return val;
     }
-
 }