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 com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.Future;
14 import javax.annotation.Nullable;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
18 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
19 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class SalGroupServiceImpl implements SalGroupService, ItemLifeCycleSource {
41 private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
42 private final GroupService<AddGroupInput, AddGroupOutput> addGroup;
43 private final GroupService<Group, UpdateGroupOutput> updateGroup;
44 private final GroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
45 private final DeviceContext deviceContext;
46 private ItemLifecycleListener itemLifecycleListener;
48 public SalGroupServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
49 this.deviceContext = deviceContext;
50 addGroup = new GroupService<>(requestContextStack, deviceContext, AddGroupOutput.class, convertorExecutor);
51 updateGroup = new GroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class, convertorExecutor);
52 removeGroup = new GroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class, convertorExecutor);
56 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
57 this.itemLifecycleListener = itemLifecycleListener;
61 public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
62 final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture = addGroup.handleServiceCall(input);
63 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
65 public void onSuccess(RpcResult<AddGroupOutput> result) {
66 if (result.isSuccessful()) {
67 if (LOG.isDebugEnabled()) {
68 LOG.debug("Group add with id={} finished without error", input.getGroupId().getValue());
70 deviceContext.getDeviceGroupRegistry().store(input.getGroupId());
71 addIfNecessaryToDS(input.getGroupId(), input);
73 if (LOG.isDebugEnabled()) {
74 LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
75 ErrorUtil.errorsToString(result.getErrors()));
81 public void onFailure(Throwable t) {
82 LOG.warn("Service call for adding group={} failed, reason: {}", input.getGroupId().getValue(), t);
90 public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
91 final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture = updateGroup.handleServiceCall(input.getUpdatedGroup());
92 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
94 public void onSuccess(@Nullable RpcResult<UpdateGroupOutput> result) {
95 if (result.isSuccessful()) {
96 if (LOG.isDebugEnabled()) {
97 LOG.debug("Group update with original id={} finished without error",
98 input.getOriginalGroup().getGroupId().getValue());
100 removeIfNecessaryFromDS(input.getOriginalGroup().getGroupId());
101 addIfNecessaryToDS(input.getUpdatedGroup().getGroupId(), input.getUpdatedGroup());
103 if (LOG.isDebugEnabled()) {
104 LOG.debug("Group update with original id={} failed, errors={}",
105 input.getOriginalGroup().getGroupId(), ErrorUtil.errorsToString(result.getErrors()));
111 public void onFailure(Throwable t) {
112 LOG.warn("Service call for updating group={} failed, reason: {}",
113 input.getOriginalGroup().getGroupId(), t);
120 public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
121 final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture = removeGroup.handleServiceCall(input);
122 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
124 public void onSuccess(@Nullable RpcResult<RemoveGroupOutput> result) {
125 if (result.isSuccessful()) {
126 if (LOG.isDebugEnabled()) {
127 LOG.debug("Group remove with id={} finished without error", input.getGroupId().getValue());
129 removeGroup.getDeviceRegistry().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
130 removeIfNecessaryFromDS(input.getGroupId());
132 if (LOG.isDebugEnabled()) {
133 LOG.debug("Group remove with id={} failed, errors={}", input.getGroupId().getValue(),
134 ErrorUtil.errorsToString(result.getErrors()));
140 public void onFailure(Throwable t) {
141 LOG.warn("Service call for removing group={} failed, reason: {}",
142 input.getGroupId().getValue(), t);
148 private void removeIfNecessaryFromDS(final GroupId groupId) {
149 if (itemLifecycleListener != null) {
150 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
151 = createGroupPath(groupId,
152 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
153 itemLifecycleListener.onRemoved(groupPath);
157 private void addIfNecessaryToDS(final GroupId groupId, final Group data) {
158 if (itemLifecycleListener != null) {
159 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
160 = createGroupPath(groupId,
161 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
162 itemLifecycleListener.onAdded(groupPath, new GroupBuilder(data).build());
166 private static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey>
167 createGroupPath(final GroupId groupId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
168 return nodePath.augmentation(FlowCapableNode.class).
169 child(org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group.class, new GroupKey(groupId));