X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowplugin-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fimpl%2Fservices%2FSalMeterServiceImpl.java;h=b053d99cb8fc361361d93ccbebe1d0be955b5f36;hb=4bd43c307a24836ab1f7d6829d93d50e868e8ded;hp=f7061b9e4e109bea2fc7e2b60e60d667398a1fe8;hpb=5fe645a6c8f38d05f794f2d908d346b413d8d5c8;p=openflowplugin.git diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/SalMeterServiceImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/SalMeterServiceImpl.java index f7061b9e4e..b053d99cb8 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/SalMeterServiceImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/SalMeterServiceImpl.java @@ -7,18 +7,21 @@ */ package org.opendaylight.openflowplugin.impl.services; -import com.google.common.base.Function; import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.SettableFuture; +import java.util.Collection; import java.util.concurrent.Future; -import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue; +import javax.annotation.Nullable; import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext; -import org.opendaylight.openflowplugin.api.openflow.device.RequestContext; import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack; -import org.opendaylight.openflowplugin.api.openflow.device.Xid; -import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy; -import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.MeterConvertor; +import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource; +import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener; +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.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; import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput; @@ -27,80 +30,150 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.Sal import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; +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.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -public class SalMeterServiceImpl extends CommonService implements SalMeterService { - - private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalMeterServiceImpl.class); +public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource { + private static final Logger LOG = LoggerFactory.getLogger(SalMeterServiceImpl.class); + private final MeterService addMeter; + private final MeterService updateMeter; + private final MeterService removeMeter; + private ItemLifecycleListener itemLifecycleListener; + private final DeviceContext deviceContext; public SalMeterServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) { - super(requestContextStack, deviceContext); + this.deviceContext = deviceContext; + addMeter = new MeterService<>(requestContextStack, deviceContext, AddMeterOutput.class); + updateMeter = new MeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class); + removeMeter = new MeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class); + } + + @Override + public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) { + this.itemLifecycleListener = itemLifecycleListener; } @Override public Future> addMeter(final AddMeterInput input) { - getDeviceContext().getDeviceMeterRegistry().store(input.getMeterId()); - return handleServiceCall(new Function, ListenableFuture>>() { + addMeter.getDeviceRegistry().getDeviceMeterRegistry().store(input.getMeterId()); + + final ListenableFuture> resultFuture = addMeter.handleServiceCall(input); + Futures.addCallback(resultFuture, new FutureCallback>() { + + @Override + public void onSuccess(@Nullable RpcResult result) { + if (result.isSuccessful()) { + 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())); + } + } + @Override - public ListenableFuture> apply(final RequestContext requestContext) { - return convertAndSend(input, requestContext); + public void onFailure(Throwable t) { + LOG.error("Meter add failed for id={}. Exception {}", input.getMeterId(), t); } }); + + return resultFuture; } @Override public Future> updateMeter(final UpdateMeterInput input) { - return handleServiceCall(new Function, ListenableFuture>>() { + final ListenableFuture> resultFuture = updateMeter.handleServiceCall(input.getUpdatedMeter()); + + Futures.addCallback(resultFuture, new FutureCallback>() { + @Override - public ListenableFuture> apply(final RequestContext requestContext) { - return convertAndSend(input.getUpdatedMeter(), requestContext); + public void onSuccess(@Nullable RpcResult result) { + if (result.isSuccessful()) { + 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("Service call for meter update failed. for id={}. Exception {}.", + input.getOriginalMeter().getMeterId(),t); } }); + return resultFuture; } @Override public Future> removeMeter(final RemoveMeterInput input) { - getDeviceContext().getDeviceMeterRegistry().markToBeremoved(input.getMeterId()); - return handleServiceCall(new Function, ListenableFuture>>() { + removeMeter.getDeviceRegistry().getDeviceMeterRegistry().markToBeremoved(input.getMeterId()); + final ListenableFuture> resultFuture = removeMeter.handleServiceCall(input); + Futures.addCallback(resultFuture, new FutureCallback>() { + @Override - public ListenableFuture> apply(final RequestContext requestContext) { - return convertAndSend(input, requestContext); + public void onSuccess(@Nullable RpcResult result) { + if (result.isSuccessful()) { + 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("Service call for meter remove failed for id={}. Exception {}",input.getMeterId(),t); } }); + + return resultFuture; } - ListenableFuture> convertAndSend(final Meter iputMeter, final RequestContext requestContext) { - getMessageSpy().spyMessage(iputMeter.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS); - final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider(); + private void removeIfNecessaryFromDS(final MeterId meterId) { + if (itemLifecycleListener != null) { + KeyedInstanceIdentifier meterPath + = createMeterPath(meterId, + deviceContext.getDeviceInfo().getNodeInstanceIdentifier()); + itemLifecycleListener.onRemoved(meterPath); + } + } - final MeterModInputBuilder ofMeterModInput = MeterConvertor.toMeterModInput(iputMeter, getVersion()); - final Xid xid = requestContext.getXid(); - ofMeterModInput.setXid(xid.getValue()); - final SettableFuture> settableFuture = SettableFuture.create(); - final MeterModInput meterModInput = ofMeterModInput.build(); - outboundQueue.commitEntry(xid.getValue(), meterModInput, new FutureCallback() { - @Override - public void onSuccess(final OfHeader ofHeader) { - RequestContextUtil.closeRequstContext(requestContext); - getMessageSpy().spyMessage(meterModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS); + private void addIfNecessaryToDS(final MeterId meterId, final Meter data) { + if (itemLifecycleListener != null) { + KeyedInstanceIdentifier groupPath + = createMeterPath(meterId, + deviceContext.getDeviceInfo().getNodeInstanceIdentifier()); + itemLifecycleListener.onAdded(groupPath, new MeterBuilder(data).build()); + } + } - settableFuture.set(RpcResultBuilder.success().build()); - } + static KeyedInstanceIdentifier createMeterPath(final MeterId meterId, final KeyedInstanceIdentifier nodePath) { + return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter.class, new MeterKey(meterId)); + } - @Override - public void onFailure(final Throwable throwable) { - RpcResultBuilder rpcResultBuilder = RpcResultBuilder.failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable); - RequestContextUtil.closeRequstContext(requestContext); - getMessageSpy().spyMessage(meterModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE); - settableFuture.set(rpcResultBuilder.build()); + private final String errorsToString(final Collection rpcErrors) { + final StringBuilder errors = new StringBuilder(); + if ((null != rpcErrors) && (rpcErrors.size() > 0)) { + for (final RpcError rpcError : rpcErrors) { + errors.append(rpcError.getMessage()); } - }); - return settableFuture; + } + return errors.toString(); } }