*/
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 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.GroupConvertor;
+import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
+import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
+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.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;
-
-public class SalGroupServiceImpl extends CommonService implements SalGroupService {
-
-
- public SalGroupServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
- super(requestContextStack, deviceContext);
+import org.slf4j.LoggerFactory;
+
+public class SalGroupServiceImpl implements SalGroupService, ItemLifeCycleSource {
+ private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
+ private final GroupService<AddGroupInput, AddGroupOutput> addGroup;
+ private final GroupService<Group, UpdateGroupOutput> updateGroup;
+ private final GroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
+ private final DeviceContext deviceContext;
+ private ItemLifecycleListener itemLifecycleListener;
+
+ public SalGroupServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
+ this.deviceContext = deviceContext;
+ addGroup = new GroupService<>(requestContextStack, deviceContext, AddGroupOutput.class, convertorExecutor);
+ updateGroup = new GroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class, convertorExecutor);
+ removeGroup = new GroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class, convertorExecutor);
}
- private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalGroupServiceImpl.class);
+ @Override
+ public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
+ this.itemLifecycleListener = itemLifecycleListener;
+ }
@Override
public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
- getDeviceContext().getDeviceGroupRegistry().store(input.getGroupId());
- return handleServiceCall(new Function<RequestContext<AddGroupOutput>,
- ListenableFuture<RpcResult<AddGroupOutput>>>() {
+ final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture = addGroup.handleServiceCall(input);
+ Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
+ @Override
+ public void onSuccess(RpcResult<AddGroupOutput> result) {
+ if (result.isSuccessful()) {
+ if(LOG.isDebugEnabled()) {
+ LOG.debug("group add with id={} finished without error", input.getGroupId().getValue());
+ }
+ deviceContext.getDeviceGroupRegistry().store(input.getGroupId());
+ addIfNecessaryToDS(input.getGroupId(), input);
+ } else {
+ LOG.error("group add with id={} failed, errors={}", input.getGroupId().getValue(),
+ errorsToString(result.getErrors()));
+ }
+ }
+
@Override
- public ListenableFuture<RpcResult<AddGroupOutput>> apply(final RequestContext<AddGroupOutput> requestContext) {
- return convertAndSend(input, requestContext);
+ public void onFailure(Throwable t) {
+ LOG.error("Service call for group add failed for id={}. Exception: {}", input.getGroupId().getValue(), t);
}
});
+
+ return resultFuture;
}
+
@Override
public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
- return handleServiceCall(new Function<RequestContext<UpdateGroupOutput>,
- ListenableFuture<RpcResult<UpdateGroupOutput>>>() {
+ final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture = updateGroup.handleServiceCall(input.getUpdatedGroup());
+ Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
+ @Override
+ public void onSuccess(@Nullable RpcResult<UpdateGroupOutput> result) {
+ if (result.isSuccessful()) {
+ if(LOG.isDebugEnabled()) {
+ LOG.debug("Group update for original id {} succeded", input.getOriginalGroup().getGroupId().getValue());
+ }
+ removeIfNecessaryFromDS(input.getOriginalGroup().getGroupId());
+ addIfNecessaryToDS(input.getUpdatedGroup().getGroupId(), input.getUpdatedGroup());
+ }else{
+ LOG.error("group update failed with id={}, errors={}", input.getOriginalGroup().getGroupId(),
+ errorsToString(result.getErrors()));
+ }
+ }
@Override
- public ListenableFuture<RpcResult<UpdateGroupOutput>> apply(final RequestContext<UpdateGroupOutput> requestContext) {
- return convertAndSend(input.getUpdatedGroup(), requestContext);
+ public void onFailure(Throwable t) {
+ LOG.error("Service call for group update failed for id={}. Exception: {}",
+ input.getOriginalGroup().getGroupId(), t);
}
});
+ return resultFuture;
}
@Override
public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
- getDeviceContext().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
- return handleServiceCall(new Function<RequestContext<RemoveGroupOutput>,
- ListenableFuture<RpcResult<RemoveGroupOutput>>>() {
+ final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture = removeGroup.handleServiceCall(input);
+ Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
+ @Override
+ public void onSuccess(@Nullable RpcResult<RemoveGroupOutput> result) {
+ if (result.isSuccessful()) {
+ if(LOG.isDebugEnabled()) {
+ LOG.debug("Group remove for id {} succeded", input.getGroupId().getValue());
+ }
+ removeGroup.getDeviceRegistry().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
+ removeIfNecessaryFromDS(input.getGroupId());
+ }else{
+ LOG.error("group remove failed with id={}, errors={}", input.getGroupId().getValue(),
+ errorsToString(result.getErrors()));
+ }
+ }
@Override
- public ListenableFuture<RpcResult<RemoveGroupOutput>> apply(final RequestContext<RemoveGroupOutput> requestContext) {
- return convertAndSend(input, requestContext);
+ public void onFailure(Throwable t) {
+ LOG.error("Service call for group remove failed for id={}. Exception: {}",
+ input.getGroupId().getValue(), t);
}
});
+ return resultFuture;
}
- <T> ListenableFuture<RpcResult<T>> convertAndSend(final Group iputGroup, final RequestContext<T> requestContext) {
- final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
- getMessageSpy().spyMessage(iputGroup.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
- final GroupModInputBuilder ofGroupModInput = GroupConvertor.toGroupModInput(iputGroup, getVersion(), getDatapathId());
- final Xid xid = requestContext.getXid();
- ofGroupModInput.setXid(xid.getValue());
-
- final GroupModInput groupModInput = ofGroupModInput.build();
- outboundQueue.commitEntry(xid.getValue(), groupModInput, new FutureCallback<OfHeader>() {
-
- 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 GroupId groupId) {
+ if (itemLifecycleListener != null) {
+ KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
+ = createGroupPath(groupId,
+ deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
+ itemLifecycleListener.onRemoved(groupPath);
+ }
+ }
- getMessageSpy().spyMessage(groupModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
- }
+ private void addIfNecessaryToDS(final GroupId groupId, final Group data) {
+ if (itemLifecycleListener != null) {
+ KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
+ = createGroupPath(groupId,
+ deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
+ itemLifecycleListener.onAdded(groupPath, new GroupBuilder(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.group.types.rev131018.groups.Group, GroupKey>
+ createGroupPath(final GroupId groupId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
+ return nodePath.augmentation(FlowCapableNode.class).
+ child(org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group.class, new GroupKey(groupId));
+ }
- getMessageSpy().spyMessage(groupModInput.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();
}
-}
+}
\ No newline at end of file