TSC-99 Adjust to RPC method signature update
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / impl / ItmProvider.java
index f92aeb000c3ddd068e16136610e270f4b2511e69..3cb0fdc061661394de694f8789203aac6c9e8794 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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,
@@ -8,40 +8,46 @@
 package org.opendaylight.genius.itm.impl;
 
 import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.math.BigInteger;
+import java.util.Collection;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
+
 import org.apache.felix.service.command.CommandSession;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
 import org.opendaylight.genius.itm.api.IITMProvider;
+import org.opendaylight.genius.itm.cache.DpnTepStateCache;
 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.listeners.InterfaceStateListener;
+import org.opendaylight.genius.itm.listeners.OvsdbNodeListener;
 import org.opendaylight.genius.itm.listeners.TransportZoneListener;
 import org.opendaylight.genius.itm.listeners.TunnelMonitorChangeListener;
 import org.opendaylight.genius.itm.listeners.TunnelMonitorIntervalListener;
 import org.opendaylight.genius.itm.listeners.VtepConfigSchemaListener;
-import org.opendaylight.genius.itm.listeners.cache.DpnTepsInfoListener;
-import org.opendaylight.genius.itm.listeners.cache.ItmMonitoringIntervalListener;
-import org.opendaylight.genius.itm.listeners.cache.ItmMonitoringListener;
-import org.opendaylight.genius.itm.listeners.cache.StateTunnelListListener;
 import org.opendaylight.genius.itm.monitoring.ItmTunnelEventListener;
 import org.opendaylight.genius.itm.rpc.ItmManagerRpcService;
-import org.opendaylight.genius.itm.snd.ITMStatusMonitor;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
+import org.opendaylight.mdsal.eos.binding.api.Entity;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
+import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.VtepConfigSchemas;
@@ -57,89 +63,99 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class ItmProvider implements AutoCloseable, IITMProvider /*,ItmStateService */{
+public class ItmProvider implements AutoCloseable, IITMProvider /*,ItmStateService */ {
 
     private static final Logger LOG = LoggerFactory.getLogger(ItmProvider.class);
-    private ITMManager itmManager;
-    private DataBroker dataBroker;
-    private ItmManagerRpcService itmRpcService ;
-    private IdManagerService idManager;
-    private TepCommandHelper tepCommandHelper;
-    private TransportZoneListener tzChangeListener;
-    private TunnelMonitorChangeListener tnlToggleListener;
-    private TunnelMonitorIntervalListener tnlIntervalListener;
-    private VtepConfigSchemaListener vtepConfigSchemaListener;
-    private InterfaceStateListener ifStateListener;
+
+    private final DataBroker dataBroker;
+    private final ItmManagerRpcService itmRpcService ;
+    private final IdManagerService idManager;
+    private final TepCommandHelper tepCommandHelper;
+    private final TransportZoneListener tzChangeListener;
+    private final TunnelMonitorChangeListener tnlToggleListener;
+    private final TunnelMonitorIntervalListener tnlIntervalListener;
+    private final VtepConfigSchemaListener vtepConfigSchemaListener;
+    private final InterfaceStateListener ifStateListener;
+    private final EntityOwnershipService entityOwnershipService;
     private RpcProviderRegistry rpcProviderRegistry;
-    private static final ITMStatusMonitor itmStatusMonitor = ITMStatusMonitor.getInstance();
-    private ItmTunnelEventListener itmStateListener;
-    private ItmMonitoringListener itmMonitoringListener;
-    private ItmMonitoringIntervalListener itmMonitoringIntervalListener;
+    private final ItmTunnelEventListener itmStateListener;
+    private final OvsdbNodeListener ovsdbChangeListener;
     static short flag = 0;
-    private StateTunnelListListener tunnelStateListener ;
-    private DpnTepsInfoListener dpnTepsInfoListener ;
+    private final TunnelMonitoringConfig tunnelMonitoringConfig;
+    private EntityOwnershipCandidateRegistration registryCandidate;
+    private final DpnTepStateCache dpnTepStateCache;
 
     @Inject
     public ItmProvider(DataBroker dataBroker,
-                       DpnTepsInfoListener dpnTepsInfoListener,
                        IdManagerService idManagerService,
                        InterfaceStateListener interfaceStateListener,
-                       ITMManager itmManager,
                        ItmManagerRpcService itmManagerRpcService,
-                       ItmMonitoringListener itmMonitoringListener,
-                       ItmMonitoringIntervalListener itmMonitoringIntervalListener,
                        ItmTunnelEventListener itmTunnelEventListener,
-                       StateTunnelListListener stateTunnelListListener,
                        TepCommandHelper tepCommandHelper,
                        TunnelMonitorChangeListener tunnelMonitorChangeListener,
                        TunnelMonitorIntervalListener tunnelMonitorIntervalListener,
                        TransportZoneListener transportZoneListener,
-                       VtepConfigSchemaListener vtepConfigSchemaListener) {
+                       VtepConfigSchemaListener vtepConfigSchemaListener,
+                       OvsdbNodeListener ovsdbNodeListener,
+                       TunnelMonitoringConfig tunnelMonitoringConfig,
+                       EntityOwnershipService entityOwnershipService,
+                       DpnTepStateCache dpnTepStateCache) {
         LOG.info("ItmProvider Before register MBean");
-        itmStatusMonitor.registerMbean();
         this.dataBroker = dataBroker;
-        this.dpnTepsInfoListener = dpnTepsInfoListener;
         this.idManager = idManagerService;
         this.ifStateListener = interfaceStateListener;
-        this.itmManager = itmManager;
         this.itmRpcService = itmManagerRpcService;
-        this.itmMonitoringListener = itmMonitoringListener;
-        this.itmMonitoringIntervalListener = itmMonitoringIntervalListener;
         this.itmStateListener = itmTunnelEventListener;
-        this.tunnelStateListener = stateTunnelListListener;
         this.tepCommandHelper = tepCommandHelper;
         this.tnlToggleListener = tunnelMonitorChangeListener;
         this.tnlIntervalListener = tunnelMonitorIntervalListener;
         this.tzChangeListener = transportZoneListener;
         this.vtepConfigSchemaListener = vtepConfigSchemaListener;
+        this.ovsdbChangeListener = ovsdbNodeListener;
+        this.tunnelMonitoringConfig = tunnelMonitoringConfig;
+        this.entityOwnershipService = entityOwnershipService;
+        this.dpnTepStateCache = dpnTepStateCache;
+        ITMBatchingUtils.registerWithBatchManager(this.dataBroker);
     }
 
     @PostConstruct
-    public void start() throws Exception {
-        createIdPool();
-        LOG.info("ItmProvider Started");
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    public void start() {
+        try {
+            createIdPool();
+            registerEntityForOwnership();
+            LOG.info("ItmProvider Started");
+        } catch (Exception ex) {
+            LOG.info("ItmProvider failed to start");
+        }
+    }
+
+    private void registerEntityForOwnership() {
+        try {
+            this.registryCandidate = entityOwnershipService
+                    .registerCandidate(new Entity(ITMConstants.ITM_CONFIG_ENTITY, ITMConstants.ITM_CONFIG_ENTITY));
+        } catch (CandidateAlreadyRegisteredException e) {
+            LOG.error("failed to register entity {} for entity-ownership-service", e.getEntity());
+        }
     }
 
     @Override
     @PreDestroy
-    public void close() throws Exception {
-        if (itmManager != null) {
-            itmManager.close();
-        }
+    public void close() {
         if (tzChangeListener != null) {
             tzChangeListener.close();
         }
         if (tnlIntervalListener != null) {
             tnlIntervalListener.close();
         }
-        if(tnlToggleListener!= null){
+        if (tnlToggleListener != null) {
             tnlToggleListener.close();
         }
-        if(tunnelStateListener!= null){
-            tunnelStateListener.close();
+        if (ovsdbChangeListener != null) {
+            ovsdbChangeListener.close();
         }
-        if(dpnTepsInfoListener!= null){
-            dpnTepsInfoListener.close();
+        if (registryCandidate != null) {
+            registryCandidate.close();
         }
         LOG.info("ItmProvider Closed");
     }
@@ -151,12 +167,12 @@ public class ItmProvider implements AutoCloseable, IITMProvider /*,ItmStateServi
                 .setHigh(new BigInteger(ITMConstants.ITM_IDPOOL_SIZE).longValue())
                 .build();
         try {
-            Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
-            if ((result != null) && (result.get().isSuccessful())) {
+            ListenableFuture<RpcResult<CreateIdPoolOutput>> result = idManager.createIdPool(createPool);
+            if (result != null && result.get().isSuccessful()) {
                 LOG.debug("Created IdPool for ITM Service");
             }
         } catch (InterruptedException | ExecutionException e) {
-            LOG.error("Failed to create idPool for ITM Service",e);
+            LOG.error("Failed to create idPool for ITM Service ", e);
         }
     }
 
@@ -165,27 +181,33 @@ public class ItmProvider implements AutoCloseable, IITMProvider /*,ItmStateServi
         return dataBroker;
     }
 
+    @Override
     public void addExternalEndpoint(Class<? extends TunnelTypeBase> tunnelType, IpAddress dcgwIP) {
         AddExternalTunnelEndpointInput addExternalTunnelEndpointInput =
                 new AddExternalTunnelEndpointInputBuilder().setTunnelType(tunnelType)
                         .setDestinationIp(dcgwIP).build();
-        itmRpcService.addExternalTunnelEndpoint(addExternalTunnelEndpointInput);
+        JdkFutures.addErrorLogging(itmRpcService.addExternalTunnelEndpoint(addExternalTunnelEndpointInput),
+                LOG, "addExternalTunnelEndpoint");
     }
 
+    @Override
     public void remExternalEndpoint(Class<? extends TunnelTypeBase> tunnelType, IpAddress dcgwIP) {
         RemoveExternalTunnelEndpointInput removeExternalTunnelEndpointInput =
                 new RemoveExternalTunnelEndpointInputBuilder().setTunnelType(tunnelType)
                         .setDestinationIp(dcgwIP).build();
-        itmRpcService.removeExternalTunnelEndpoint(removeExternalTunnelEndpointInput);
+        JdkFutures.addErrorLogging(itmRpcService.removeExternalTunnelEndpoint(removeExternalTunnelEndpointInput),
+                LOG, "removeExternalTunnelEndpoint");
     }
+
     @Override
     public void createLocalCache(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
                                  String gatewayIp, String transportZone, CommandSession session) {
         if (tepCommandHelper != null) {
             try {
-                tepCommandHelper.createLocalCache(dpnId, portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone, session);
+                tepCommandHelper.createLocalCache(dpnId, portName, vlanId, ipAddress, subnetMask,
+                        gatewayIp, transportZone, session);
             } catch (TepException e) {
-                LOG.error(e.getMessage());
+                LOG.error("Create Local Cache failed", e);
             }
         } else {
             LOG.trace("tepCommandHelper doesnt exist");
@@ -194,45 +216,46 @@ public class ItmProvider implements AutoCloseable, IITMProvider /*,ItmStateServi
 
     @Override
     public void commitTeps() {
-        try {
-            tepCommandHelper.deleteOnCommit();
-            tepCommandHelper.buildTeps();
-        } catch (Exception e) {
-            LOG.debug("unable to configure teps" + e.toString());
-        }
+        tepCommandHelper.deleteOnCommit();
+        tepCommandHelper.buildTeps();
     }
 
     @Override
     public void showTeps(CommandSession session) {
         try {
-            tepCommandHelper.showTeps(itmManager.getTunnelMonitorEnabledFromConfigDS(), ItmUtils.determineMonitorInterval(this.dataBroker), session);
+            tepCommandHelper.showTeps(tunnelMonitoringConfig.isTunnelMonitoringEnabled(),
+                    tunnelMonitoringConfig.getMonitorInterval(), session);
         } catch (TepException e) {
-            LOG.error(e.getMessage());
+            LOG.error("show teps failed", e);
         }
     }
 
-    public void showState(List<StateTunnelList> tunnels,CommandSession session) {
+    @Override
+    public void showState(Collection<StateTunnelList> tunnels,CommandSession session) {
         if (tunnels != null) {
             try {
-                tepCommandHelper.showState(tunnels, itmManager.getTunnelMonitorEnabledFromConfigDS(), session);
-            }catch(TepException e) {
-                LOG.error(e.getMessage());
+                tepCommandHelper.showState(tunnels, tunnelMonitoringConfig.isTunnelMonitoringEnabled(), session);
+            } catch (TepException e) {
+                LOG.error("show state failed", e);
             }
-        }else
+        } else {
             LOG.debug("No tunnels available");
+        }
     }
 
     @Override
-    public void showCache( String cacheName) {
+    public void showCache(String cacheName) {
         tepCommandHelper.showCache(cacheName);
     }
 
+    @Override
     public void deleteVtep(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
                            String gatewayIp, String transportZone, CommandSession session) {
         try {
-            tepCommandHelper.deleteVtep(dpnId,  portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone, session);
-        } catch (Exception e) {
-            LOG.error(e.getMessage());
+            tepCommandHelper.deleteVtep(dpnId,  portName, vlanId, ipAddress,
+                    subnetMask, gatewayIp, transportZone, session);
+        } catch (TepException e) {
+            LOG.error("Delete Vteps Failed", e);
         }
     }
 
@@ -266,7 +289,7 @@ public class ItmProvider implements AutoCloseable, IITMProvider /*,ItmStateServi
     @Override
     public List<VtepConfigSchema> getAllVtepConfigSchemas() {
         return ItmUtils.read(LogicalDatastoreType.CONFIGURATION, ItmUtils.getVtepConfigSchemasIdentifier(),
-                this.dataBroker).transform(VtepConfigSchemas::getVtepConfigSchema).orNull();
+                this.dataBroker).toJavaUtil().map(VtepConfigSchemas::getVtepConfigSchema).orElse(null);
     }
 
     @Override
@@ -280,22 +303,24 @@ public class ItmProvider implements AutoCloseable, IITMProvider /*,ItmStateServi
        /* if (ItmUtils.getDpnIdList(schema.getDpnIds()).isEmpty()) {
             builder.setDpnIds(schema.getDpnIds());
         } else {*/
-            if (lstDpnsForAdd != null && !lstDpnsForAdd.isEmpty()) {
-                List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
-                originalDpnList.addAll(lstDpnsForAdd) ;
-                builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList));
-            }
-            if (lstDpnsForDelete != null && !lstDpnsForDelete.isEmpty()) {
-                List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
-                originalDpnList.removeAll(lstDpnsForDelete) ;
-                builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList)) ;
-                // schema.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(ItmUtils.getDpnIdList(schema.getDpnIds()).removeAll(lstDpnsForAdd)));
-            }
-       // }
+        if (lstDpnsForAdd != null && !lstDpnsForAdd.isEmpty()) {
+            List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds());
+            originalDpnList.addAll(lstDpnsForAdd) ;
+            builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList));
+        }
+        if (lstDpnsForDelete != null && !lstDpnsForDelete.isEmpty()) {
+            List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds());
+            originalDpnList.removeAll(lstDpnsForDelete) ;
+            builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList));
+            // schema.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(ItmUtils.getDpnIdList(schema.getDpnIds())
+            // .removeAll(lstDpnsForAdd)));
+        }
+        // }
         schema = builder.build();
         MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
                 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), schema);
-        LOG.debug("Vtep config schema {} updated to config DS with DPN's {}", schemaName, ItmUtils.getDpnIdList(schema.getDpnIds()));
+        LOG.debug("Vtep config schema {} updated to config DS with DPN's {}",
+                schemaName, ItmUtils.getDpnIdList(schema.getDpnIds()));
     }
 
     @Override
@@ -310,22 +335,30 @@ public class ItmProvider implements AutoCloseable, IITMProvider /*,ItmStateServi
         LOG.debug("Deleted all Vtep schemas from config DS");
     }
 
+    @Override
     public void configureTunnelMonitorParams(boolean monitorEnabled, String monitorProtocol) {
         tepCommandHelper.configureTunnelMonitorParams(monitorEnabled, monitorProtocol);
     }
 
+    @Override
     public void configureTunnelMonitorInterval(int interval) {
         tepCommandHelper.configureTunnelMonitorInterval(interval);
     }
-    
-    public boolean validateIP (final String ip){
-        if (ip == null || ip.equals("")) {
+
+    @Override
+    public boolean validateIP(final String ip) {
+        if ((ip == null) || ip.isEmpty() || "null".equals(ip) || "0.0.0.0".equals(ip)) {
             return false;
         }
-        final String PATTERN =
+        final String PTRN =
                 "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
-        Pattern pattern = Pattern.compile(PATTERN);
+        Pattern pattern = Pattern.compile(PTRN);
         Matcher matcher = pattern.matcher(ip);
         return matcher.matches();
     }
+
+    @Override
+    public Interface getInterface(String tunnelName) {
+        return dpnTepStateCache.getInterfaceFromCache(tunnelName);
+    }
 }