*/
package org.opendaylight.openflowplugin.impl.services;
-import com.google.common.util.concurrent.JdkFutureAdapters;
+import java.util.concurrent.Future;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
-import org.opendaylight.openflowplugin.api.openflow.device.Xid;
-
-import com.google.common.base.Function;
-import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.GroupConvertor;
+import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
+import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
+import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
+import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
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.GroupModInputBuilder;
+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.RpcResult;
import org.slf4j.Logger;
-import java.util.concurrent.Future;
+import org.slf4j.LoggerFactory;
-public class SalGroupServiceImpl extends CommonService implements SalGroupService {
+import javax.annotation.Nullable;
- public SalGroupServiceImpl(RequestContextStack requestContextStack, DeviceContext deviceContext) {
- super(requestContextStack, deviceContext);
+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) {
+ this.deviceContext = deviceContext;
+ addGroup = new GroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
+ updateGroup = new GroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
+ removeGroup = new GroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
}
- 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) {
- deviceContext.getDeviceGroupRegistry().store(input.getGroupId());
- return this.<AddGroupOutput, Void> handleServiceCall( PRIMARY_CONNECTION,
- new Function<DataCrate<AddGroupOutput>,ListenableFuture<RpcResult<Void>>>() {
-
- @Override
- public ListenableFuture<RpcResult<Void>> apply(final DataCrate<AddGroupOutput> data) {
- return convertAndSend(input, data);
- }
- });
+ addGroup.getDeviceContext().getDeviceGroupRegistry().store(input.getGroupId());
+ 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()) {
+ LOG.debug("group add finished without error, id={}", input.getGroupId().getValue());
+ addIfNecessaryToDS(input.getGroupId(), input);
+ }
+ }
+
+ @Override
+ public void onFailure(Throwable t) {
+ LOG.error("group add failed for id={}. Exception: {}", input.getGroupId().getValue(), t);
+ }
+ });
+
+ return resultFuture;
}
+
@Override
public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
- return this.<UpdateGroupOutput, Void> handleServiceCall(PRIMARY_CONNECTION,
- new Function<DataCrate<UpdateGroupOutput>, ListenableFuture<RpcResult<Void>>>() {
-
- @Override
- public ListenableFuture<RpcResult<Void>> apply(final DataCrate<UpdateGroupOutput> data) {
- return convertAndSend(input.getUpdatedGroup(), data);
- }
- });
+ 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()) {
+ LOG.debug("Group update succeded");
+ removeIfNecessaryFromDS(input.getOriginalGroup().getGroupId());
+ addIfNecessaryToDS(input.getUpdatedGroup().getGroupId(), input.getUpdatedGroup());
+ }
+ }
+
+ @Override
+ public void onFailure(Throwable t) {
+ LOG.debug("Group update failed for id={}. Exception: {}", input.getOriginalGroup().getGroupId(), t);
+ }
+ });
+ return resultFuture;
}
@Override
public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
- deviceContext.getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
- return this.<RemoveGroupOutput, Void> handleServiceCall(PRIMARY_CONNECTION,
- new Function<DataCrate<RemoveGroupOutput>, ListenableFuture<RpcResult<Void>>>() {
-
- @Override
- public ListenableFuture<RpcResult<Void>> apply(final DataCrate<RemoveGroupOutput> data) {
- return convertAndSend(input, data);
- }
- });
+ removeGroup.getDeviceContext().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
+ 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()) {
+ LOG.debug("Group remove succeded");
+ removeIfNecessaryFromDS(input.getGroupId());
+ }
+ }
+
+ @Override
+ public void onFailure(Throwable t) {
+ LOG.error("Group remove failed for id={}. Exception: {}", input.getGroupId(), t);
+ }
+ });
+ return resultFuture;
+ }
+
+ 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.getDeviceState().getNodeInstanceIdentifier());
+ itemLifecycleListener.onRemoved(groupPath);
+ }
+ }
+
+ 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.getDeviceState().getNodeInstanceIdentifier());
+ itemLifecycleListener.onAdded(groupPath, new GroupBuilder(data).build());
+ }
}
- <T> ListenableFuture<RpcResult<Void>> convertAndSend(final Group iputGroup, final DataCrate<T> data) {
- final GroupModInputBuilder ofGroupModInput = GroupConvertor.toGroupModInput(iputGroup, version, datapathId);
- final Xid xid = deviceContext.getNextXid();
- ofGroupModInput.setXid(xid.getValue());
- data.getRequestContext().setXid(xid);
- return JdkFutureAdapters.listenInPoolThread(provideConnectionAdapter(data.getiDConnection()).groupMod(ofGroupModInput.build()));
+ 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));
}
}