Abstract service
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalMeterServiceImpl.java
index 9bc608eae0df8d53db131a57551a3302d283e616..b053d99cb8fc361361d93ccbebe1d0be955b5f36 100644 (file)
@@ -7,11 +7,12 @@
  */
 package org.opendaylight.openflowplugin.impl.services;
 
-import java.util.concurrent.Future;
-
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
+import java.util.Collection;
+import java.util.concurrent.Future;
+import javax.annotation.Nullable;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
@@ -19,7 +20,6 @@ import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleLi
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
@@ -32,12 +32,11 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.Upd
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.annotation.Nullable;
-
 public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource {
     private static final Logger LOG = LoggerFactory.getLogger(SalMeterServiceImpl.class);
     private final MeterService<AddMeterInput, AddMeterOutput> addMeter;
@@ -60,7 +59,7 @@ public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource
 
     @Override
     public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
-        addMeter.getDeviceContext().getDeviceMeterRegistry().store(input.getMeterId());
+        addMeter.getDeviceRegistry().getDeviceMeterRegistry().store(input.getMeterId());
 
         final ListenableFuture<RpcResult<AddMeterOutput>> resultFuture = addMeter.handleServiceCall(input);
         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
@@ -68,8 +67,13 @@ public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource
             @Override
             public void onSuccess(@Nullable RpcResult<AddMeterOutput> result) {
                 if (result.isSuccessful()) {
-                    LOG.debug("Meter add finished without error, id={}", input.getMeterId());
+                    if(LOG.isDebugEnabled()) {
+                        LOG.debug("Meter add finished without error, id={}", input.getMeterId());
+                    }
                     addIfNecessaryToDS(input.getMeterId(),input);
+                }else{
+                    LOG.error("Meter add with id {} failed with error {}", input.getMeterId(),
+                            errorsToString(result.getErrors()));
                 }
             }
 
@@ -91,17 +95,23 @@ public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource
             @Override
             public void onSuccess(@Nullable RpcResult<UpdateMeterOutput> result) {
                 if (result.isSuccessful()) {
-                    LOG.debug("Meter update finished without error, id={}", input.getOriginalMeter().getMeterId());
+                    if(LOG.isDebugEnabled()) {
+                        LOG.debug("Meter update finished without error, id={}", input.getOriginalMeter().getMeterId());
+                    }
                     if (itemLifecycleListener != null) {
                         removeIfNecessaryFromDS(input.getOriginalMeter().getMeterId());
                         addIfNecessaryToDS(input.getUpdatedMeter().getMeterId(),input.getUpdatedMeter());
                     }
+                }else{
+                    LOG.error("Meter update with id {} failed with error {}", input.getOriginalMeter().getMeterId(),
+                            errorsToString(result.getErrors()));
                 }
             }
 
             @Override
             public void onFailure(Throwable t) {
-                LOG.error("Meter update failed. for id={}. Exception {}.",input.getOriginalMeter().getMeterId(),t);
+                LOG.error("Service call for meter update failed. for id={}. Exception {}.",
+                        input.getOriginalMeter().getMeterId(),t);
             }
         });
         return resultFuture;
@@ -109,21 +119,26 @@ public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource
 
     @Override
     public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
-        removeMeter.getDeviceContext().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
+        removeMeter.getDeviceRegistry().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
         final ListenableFuture<RpcResult<RemoveMeterOutput>> resultFuture = removeMeter.handleServiceCall(input);
         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
 
             @Override
             public void onSuccess(@Nullable RpcResult<RemoveMeterOutput> result) {
                 if (result.isSuccessful()) {
-                    LOG.debug("Meter remove finished without error, id={}", input.getMeterId());
+                    if(LOG.isDebugEnabled()) {
+                        LOG.debug("Meter remove finished without error, id={}", input.getMeterId());
+                    }
                     removeIfNecessaryFromDS(input.getMeterId());
+                }else{
+                    LOG.error("Meter remove with id {} failed with error {}", input.getMeterId(),
+                            errorsToString(result.getErrors()));
                 }
             }
 
             @Override
             public void onFailure(Throwable t) {
-                LOG.error("Meter remove failed for id={}. Exception {}",input.getMeterId(),t);
+                LOG.error("Service call for meter remove failed for id={}. Exception {}",input.getMeterId(),t);
             }
         });
 
@@ -134,7 +149,7 @@ public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource
         if (itemLifecycleListener != null) {
             KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> meterPath
                     = createMeterPath(meterId,
-                    deviceContext.getDeviceState().getNodeInstanceIdentifier());
+                    deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
             itemLifecycleListener.onRemoved(meterPath);
         }
     }
@@ -143,7 +158,7 @@ public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource
         if (itemLifecycleListener != null) {
             KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> groupPath
                     = createMeterPath(meterId,
-                    deviceContext.getDeviceState().getNodeInstanceIdentifier());
+                    deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
             itemLifecycleListener.onAdded(groupPath, new MeterBuilder(data).build());
         }
     }
@@ -152,4 +167,13 @@ public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource
         return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter.class, new MeterKey(meterId));
     }
 
+    private final String errorsToString(final Collection<RpcError> rpcErrors) {
+        final StringBuilder errors = new StringBuilder();
+        if ((null != rpcErrors) && (rpcErrors.size() > 0)) {
+            for (final RpcError rpcError : rpcErrors) {
+                errors.append(rpcError.getMessage());
+            }
+        }
+        return errors.toString();
+    }
 }