*/
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;
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<AddMeterInput, AddMeterOutput> addMeter;
+ private final MeterService<Meter, UpdateMeterOutput> updateMeter;
+ private final MeterService<RemoveMeterInput, RemoveMeterOutput> 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<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
- getDeviceContext().getDeviceMeterRegistry().store(input.getMeterId());
- return handleServiceCall(new Function<RequestContext<AddMeterOutput>,
- ListenableFuture<RpcResult<AddMeterOutput>>>() {
+ addMeter.getDeviceContext().getDeviceMeterRegistry().store(input.getMeterId());
+
+ final ListenableFuture<RpcResult<AddMeterOutput>> resultFuture = addMeter.handleServiceCall(input);
+ Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
+
@Override
- public ListenableFuture<RpcResult<AddMeterOutput>> apply(final RequestContext<AddMeterOutput> requestContext) {
- return convertAndSend(input, requestContext);
+ public void onSuccess(@Nullable RpcResult<AddMeterOutput> 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 void onFailure(Throwable t) {
+ LOG.error("Meter add failed for id={}. Exception {}", input.getMeterId(), t);
}
});
+
+ return resultFuture;
}
@Override
public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
- return handleServiceCall(new Function<RequestContext<UpdateMeterOutput>,
- ListenableFuture<RpcResult<UpdateMeterOutput>>>() {
+ final ListenableFuture<RpcResult<UpdateMeterOutput>> resultFuture = updateMeter.handleServiceCall(input.getUpdatedMeter());
+
+ Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
+
+ @Override
+ public void onSuccess(@Nullable RpcResult<UpdateMeterOutput> 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 ListenableFuture<RpcResult<UpdateMeterOutput>> apply(final RequestContext<UpdateMeterOutput> requestContext) {
- return convertAndSend(input.getUpdatedMeter(), requestContext);
+ 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<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
- getDeviceContext().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
- return handleServiceCall(new Function<RequestContext<RemoveMeterOutput>,
- ListenableFuture<RpcResult<RemoveMeterOutput>>>() {
+ removeMeter.getDeviceContext().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
+ final ListenableFuture<RpcResult<RemoveMeterOutput>> resultFuture = removeMeter.handleServiceCall(input);
+ Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
+
@Override
- public ListenableFuture<RpcResult<RemoveMeterOutput>> apply(final RequestContext<RemoveMeterOutput> requestContext) {
- return convertAndSend(input, requestContext);
+ public void onSuccess(@Nullable RpcResult<RemoveMeterOutput> 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()));
+ }
}
- });
- }
-
- <T> ListenableFuture<RpcResult<T>> convertAndSend(final Meter iputMeter, final RequestContext<T> requestContext) {
- getMessageSpy().spyMessage(iputMeter.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
- final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
- final MeterModInputBuilder ofMeterModInput = MeterConvertor.toMeterModInput(iputMeter, getVersion());
- final Xid xid = requestContext.getXid();
- ofMeterModInput.setXid(xid.getValue());
+ @Override
+ public void onFailure(Throwable t) {
+ LOG.error("Service call for meter remove failed for id={}. Exception {}",input.getMeterId(),t);
+ }
+ });
- final MeterModInput meterModInput = ofMeterModInput.build();
- outboundQueue.commitEntry(xid.getValue(), meterModInput, new FutureCallback<OfHeader>() {
+ return resultFuture;
+ }
- RpcResultBuilder<T> rpcResultBuilder;
- @Override
- public void onSuccess(final OfHeader ofHeader) {
- rpcResultBuilder = RpcResultBuilder.<T>success();
- requestContext.setResult(rpcResultBuilder.build());
- RequestContextUtil.closeRequstContext(requestContext);
+ private void removeIfNecessaryFromDS(final MeterId meterId) {
+ if (itemLifecycleListener != null) {
+ KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> meterPath
+ = createMeterPath(meterId,
+ deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
+ itemLifecycleListener.onRemoved(meterPath);
+ }
+ }
- getMessageSpy().spyMessage(meterModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
- }
+ private void addIfNecessaryToDS(final MeterId meterId, final Meter data) {
+ if (itemLifecycleListener != null) {
+ KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> groupPath
+ = createMeterPath(meterId,
+ deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
+ itemLifecycleListener.onAdded(groupPath, new MeterBuilder(data).build());
+ }
+ }
- @Override
- public void onFailure(final Throwable throwable) {
- rpcResultBuilder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
- requestContext.setResult(rpcResultBuilder.build());
- RequestContextUtil.closeRequstContext(requestContext);
+ static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> createMeterPath(final MeterId meterId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
+ return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter.class, new MeterKey(meterId));
+ }
- getMessageSpy().spyMessage(meterModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
+ 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 requestContext.getFuture();
+ }
+ return errors.toString();
}
}