Blueprint migration for ITM
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / listeners / VtepConfigSchemaListener.java
index 78f1455a6c7cb2080d4fa796dfae6a90e21e44ca..bca4a9559cef39a1ce31b14bc4e8daadbc4b08d0 100644 (file)
@@ -17,6 +17,7 @@ import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.itm.cli.TepCommandHelper;
+import org.opendaylight.genius.itm.cli.TepException;
 import org.opendaylight.genius.itm.globals.ITMConstants;
 import org.opendaylight.genius.itm.impl.ItmUtils;
 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
@@ -42,6 +43,10 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.List;
@@ -52,6 +57,7 @@ import java.util.List;
  *
  * @see VtepConfigSchema
  */
+@Singleton
 public class VtepConfigSchemaListener extends AbstractDataChangeListener<VtepConfigSchema> implements AutoCloseable {
 
     /** The Constant LOG. */
@@ -66,13 +72,19 @@ public class VtepConfigSchemaListener extends AbstractDataChangeListener<VtepCon
     /**
      * Instantiates a new vtep config schema listener.
      *
-     * @param db
+     * @param dataBroker
      *            the db
      */
-    public VtepConfigSchemaListener(final DataBroker db) {
+    @Inject
+    public VtepConfigSchemaListener(final DataBroker dataBroker) {
         super(VtepConfigSchema.class);
-        this.dataBroker = db;
-        registerListener(db);
+        this.dataBroker = dataBroker;
+    }
+
+    @PostConstruct
+    public void start() throws Exception {
+        registerListener(this.dataBroker);
+        LOG.info("VtepConfigSchemaListener Started");
     }
 
     /*
@@ -81,6 +93,7 @@ public class VtepConfigSchemaListener extends AbstractDataChangeListener<VtepCon
      * @see java.lang.AutoCloseable#close()
      */
     @Override
+    @PreDestroy
     public void close() throws Exception {
         if (this.listenerRegistration != null) {
             try {
@@ -140,8 +153,7 @@ public class VtepConfigSchemaListener extends AbstractDataChangeListener<VtepCon
             String subnetCidr = ItmUtils.getSubnetCidrAsString(schema.getSubnet());
             deleteVtepIpPool(subnetCidr);
         } catch (Exception e) {
-            String error = new StringBuilder("Failed to handle DCN for delete VtepConfigSchema: ").append(schema)
-                    .toString();
+            String error = "Failed to handle DCN for delete VtepConfigSchema: " + schema;
             LOG.error(error, e);
         }
     }
@@ -176,8 +188,8 @@ public class VtepConfigSchemaListener extends AbstractDataChangeListener<VtepCon
             handleUpdateOfDpnIds(orignalSchema, updatedSchema);
 
         } catch (Exception e) {
-            String error = new StringBuilder("Failed to handle DCN for update VtepConfigSchema original:")
-                    .append(original).append(", updated: ").append(updated).toString();
+            String error = "Failed to handle DCN for update VtepConfigSchema original:" +
+                    original + ", updated: " + updated;
             LOG.error(error, e);
         }
     }
@@ -217,9 +229,9 @@ public class VtepConfigSchemaListener extends AbstractDataChangeListener<VtepCon
      */
     private void handleUpdateOfDpnIds(VtepConfigSchema original, VtepConfigSchema updated) {
         // Handling add/delete DPNs from schema
-        List<DpnIds> originalDpnIds = (original.getDpnIds() == null) ? new ArrayList<DpnIds>()
+        List<DpnIds> originalDpnIds = (original.getDpnIds() == null) ? new ArrayList<>()
                 : original.getDpnIds();
-        List<DpnIds> updatedDpnIds = (updated.getDpnIds() == null) ? new ArrayList<DpnIds>()
+        List<DpnIds> updatedDpnIds = (updated.getDpnIds() == null) ? new ArrayList<>()
                 : updated.getDpnIds();
 
         handleDeletedDpnsFromSchema(original, originalDpnIds, updatedDpnIds);
@@ -305,12 +317,8 @@ public class VtepConfigSchemaListener extends AbstractDataChangeListener<VtepCon
      * @return the all vtep config schemas
      */
     private List<VtepConfigSchema> getAllVtepConfigSchemas() {
-        Optional<VtepConfigSchemas> schemas = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
-                ItmUtils.getVtepConfigSchemasIdentifier(), this.dataBroker);
-        if (schemas.isPresent()) {
-            return schemas.get().getVtepConfigSchema();
-        }
-        return null;
+        return ItmUtils.read(LogicalDatastoreType.CONFIGURATION, ItmUtils.getVtepConfigSchemasIdentifier(),
+                this.dataBroker).transform(VtepConfigSchemas::getVtepConfigSchema).orNull();
     }
 
     /**
@@ -355,8 +363,12 @@ public class VtepConfigSchemaListener extends AbstractDataChangeListener<VtepCon
                 skippedDpnIds.add(dpnId);
                 continue;
             }
-            tepCommandHelper.createLocalCache(dpnId, schema.getPortName(), schema.getVlanId(),
-                    String.valueOf(ipAddress.getValue()), subnetCidr, gatewayIp, schema.getTransportZoneName());
+            try {
+                tepCommandHelper.createLocalCache(dpnId, schema.getPortName(), schema.getVlanId(),
+                        String.valueOf(ipAddress.getValue()), subnetCidr, gatewayIp, schema.getTransportZoneName(), null);
+            } catch (TepException e) {
+                LOG.error(e.getMessage());
+            }
             newlyAllocatedIps.add(ipAddress);
         }
         if (!skippedDpnIds.isEmpty()) {
@@ -420,8 +432,12 @@ public class VtepConfigSchemaListener extends AbstractDataChangeListener<VtepCon
             }
 
             IpAddress ipAddress = vtep.getIpAddress();
-            tepCommandHelper.deleteVtep(dpnId, vtep.getPortname(), schema.getVlanId(),
-                    String.valueOf(ipAddress.getValue()), subnetCidr, gatewayIp, schema.getTransportZoneName());
+            try {
+                tepCommandHelper.deleteVtep(dpnId, vtep.getPortname(), schema.getVlanId(),
+                    String.valueOf(ipAddress.getValue()), subnetCidr, gatewayIp, schema.getTransportZoneName(), null);
+            } catch (TepException e) {
+                LOG.error(e.getMessage());
+            }
 
             freeIps.add(ipAddress);
         }
@@ -444,7 +460,7 @@ public class VtepConfigSchemaListener extends AbstractDataChangeListener<VtepCon
         List<IpAddress> availableIps = calculateAvailableIps(subnetUtils, schema.getExcludeIpFilter(),
                 schema.getGatewayIp());
         VtepIpPool vtepIpPool = new VtepIpPoolBuilder().setSubnetCidr(subnetCidr).setAvailableIpaddress(availableIps)
-                .setAllocatedIpaddress(new ArrayList<IpAddress>()).build();
+                .setAllocatedIpaddress(new ArrayList<>()).build();
 
         MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
                 ItmUtils.getVtepIpPoolIdentifier(subnetCidr), vtepIpPool);
@@ -460,12 +476,8 @@ public class VtepConfigSchemaListener extends AbstractDataChangeListener<VtepCon
      * @return the vtep ip pool
      */
     private VtepIpPool getVtepIpPool(final String subnetCidr) {
-        Optional<VtepIpPool> vtepIpPool = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
-                ItmUtils.getVtepIpPoolIdentifier(subnetCidr), this.dataBroker);
-        if (vtepIpPool.isPresent()) {
-            return vtepIpPool.get();
-        }
-        return null;
+        return ItmUtils.read(LogicalDatastoreType.CONFIGURATION, ItmUtils.getVtepIpPoolIdentifier(subnetCidr),
+                this.dataBroker).orNull();
     }
 
     /**