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.Collection;
14 import java.util.concurrent.Future;
15 import javax.annotation.Nullable;
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.rpc.ItemLifeCycleSource;
19 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
34 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.RpcError;
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) {
49 this.deviceContext = deviceContext;
50 addGroup = new GroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
51 updateGroup = new GroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
52 removeGroup = new GroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
56 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
57 this.itemLifecycleListener = itemLifecycleListener;
61 public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
62 addGroup.getDeviceContext().getDeviceGroupRegistry().store(input.getGroupId());
63 final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture = addGroup.handleServiceCall(input);
64 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
66 public void onSuccess(RpcResult<AddGroupOutput> result) {
67 if (result.isSuccessful()) {
68 if(LOG.isDebugEnabled()) {
69 LOG.debug("group add with id={} finished without error", input.getGroupId().getValue());
71 addIfNecessaryToDS(input.getGroupId(), input);
73 LOG.error("group add with id={} failed, errors={}", input.getGroupId().getValue(),
74 errorsToString(result.getErrors()));
79 public void onFailure(Throwable t) {
80 LOG.error("Service call for group add failed for id={}. Exception: {}",
81 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 for original id {} succeded", input.getOriginalGroup().getGroupId().getValue());
99 removeIfNecessaryFromDS(input.getOriginalGroup().getGroupId());
100 addIfNecessaryToDS(input.getUpdatedGroup().getGroupId(), input.getUpdatedGroup());
102 LOG.error("group update failed with id={}, errors={}", input.getOriginalGroup().getGroupId(),
103 errorsToString(result.getErrors()));
108 public void onFailure(Throwable t) {
109 LOG.error("Service call for group update failed for id={}. Exception: {}",
110 input.getOriginalGroup().getGroupId(), t);
117 public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
118 removeGroup.getDeviceContext().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
119 final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture = removeGroup.handleServiceCall(input);
120 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
122 public void onSuccess(@Nullable RpcResult<RemoveGroupOutput> result) {
123 if (result.isSuccessful()) {
124 if(LOG.isDebugEnabled()) {
125 LOG.debug("Group remove for id {} succeded", input.getGroupId().getValue());
127 removeIfNecessaryFromDS(input.getGroupId());
129 LOG.error("group remove failed with id={}, errors={}", input.getGroupId().getValue(),
130 errorsToString(result.getErrors()));
135 public void onFailure(Throwable t) {
136 LOG.error("Service call for group remove failed for id={}. Exception: {}",
137 input.getGroupId().getValue(), t);
143 private void removeIfNecessaryFromDS(final GroupId groupId) {
144 if (itemLifecycleListener != null) {
145 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
146 = createGroupPath(groupId,
147 deviceContext.getDeviceState().getNodeInstanceIdentifier());
148 itemLifecycleListener.onRemoved(groupPath);
152 private void addIfNecessaryToDS(final GroupId groupId, final Group data) {
153 if (itemLifecycleListener != null) {
154 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
155 = createGroupPath(groupId,
156 deviceContext.getDeviceState().getNodeInstanceIdentifier());
157 itemLifecycleListener.onAdded(groupPath, new GroupBuilder(data).build());
161 static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey>
162 createGroupPath(final GroupId groupId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
163 return nodePath.augmentation(FlowCapableNode.class).
164 child(org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group.class, new GroupKey(groupId));
167 private final String errorsToString(final Collection<RpcError> rpcErrors) {
168 final StringBuilder errors = new StringBuilder();
169 if ((null != rpcErrors) && (rpcErrors.size() > 0)) {
170 for (final RpcError rpcError : rpcErrors) {
171 errors.append(rpcError.getMessage());
174 return errors.toString();