2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.impl.services;
10 import java.util.concurrent.Future;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
19 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
20 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
21 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import javax.annotation.Nullable;
47 public class SalGroupServiceImpl implements SalGroupService, ItemLifeCycleSource {
48 private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
49 private final GroupService<AddGroupInput, AddGroupOutput> addGroup;
50 private final GroupService<Group, UpdateGroupOutput> updateGroup;
51 private final GroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
52 private final DeviceContext deviceContext;
53 private ItemLifecycleListener itemLifecycleListener;
55 public SalGroupServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
56 this.deviceContext = deviceContext;
57 addGroup = new GroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
58 updateGroup = new GroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
59 removeGroup = new GroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
63 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
64 this.itemLifecycleListener = itemLifecycleListener;
68 public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
69 addGroup.getDeviceContext().getDeviceGroupRegistry().store(input.getGroupId());
70 final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture = addGroup.handleServiceCall(input);
71 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
73 public void onSuccess(RpcResult<AddGroupOutput> result) {
74 if (result.isSuccessful()) {
75 LOG.debug("group add finished without error, id={}", input.getGroupId().getValue());
76 addIfNecessaryToDS(input.getGroupId(), input);
81 public void onFailure(Throwable t) {
82 LOG.error("group add failed for id={}. Exception: {}", input.getGroupId().getValue(), t);
91 public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
92 final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture = updateGroup.handleServiceCall(input.getUpdatedGroup());
93 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
95 public void onSuccess(@Nullable RpcResult<UpdateGroupOutput> result) {
96 if (result.isSuccessful()) {
97 LOG.debug("Group update succeded");
98 removeIfNecessaryFromDS(input.getOriginalGroup().getGroupId());
99 addIfNecessaryToDS(input.getUpdatedGroup().getGroupId(), input.getUpdatedGroup());
104 public void onFailure(Throwable t) {
105 LOG.debug("Group update failed for id={}. Exception: {}", input.getOriginalGroup().getGroupId(), t);
112 public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
113 removeGroup.getDeviceContext().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
114 final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture = removeGroup.handleServiceCall(input);
115 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
117 public void onSuccess(@Nullable RpcResult<RemoveGroupOutput> result) {
118 if (result.isSuccessful()) {
119 LOG.debug("Group remove succeded");
120 removeIfNecessaryFromDS(input.getGroupId());
125 public void onFailure(Throwable t) {
126 LOG.error("Group remove failed for id={}. Exception: {}", input.getGroupId(), t);
132 private void removeIfNecessaryFromDS(final GroupId groupId) {
133 if (itemLifecycleListener != null) {
134 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
135 = createGroupPath(groupId,
136 deviceContext.getDeviceState().getNodeInstanceIdentifier());
137 itemLifecycleListener.onRemoved(groupPath);
141 private void addIfNecessaryToDS(final GroupId groupId, final Group data) {
142 if (itemLifecycleListener != null) {
143 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
144 = createGroupPath(groupId,
145 deviceContext.getDeviceState().getNodeInstanceIdentifier());
146 itemLifecycleListener.onAdded(groupPath, new GroupBuilder(data).build());
150 static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> createGroupPath(final GroupId groupId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
151 return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group.class, new GroupKey(groupId));