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.Collection;
11 import java.util.concurrent.Future;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
20 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
21 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
22 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.RpcError;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import javax.annotation.Nullable;
49 public class SalGroupServiceImpl implements SalGroupService, ItemLifeCycleSource {
50 private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
51 private final GroupService<AddGroupInput, AddGroupOutput> addGroup;
52 private final GroupService<Group, UpdateGroupOutput> updateGroup;
53 private final GroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
54 private final DeviceContext deviceContext;
55 private ItemLifecycleListener itemLifecycleListener;
57 public SalGroupServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
58 this.deviceContext = deviceContext;
59 addGroup = new GroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
60 updateGroup = new GroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
61 removeGroup = new GroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
65 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
66 this.itemLifecycleListener = itemLifecycleListener;
70 public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
71 addGroup.getDeviceContext().getDeviceGroupRegistry().store(input.getGroupId());
72 final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture = addGroup.handleServiceCall(input);
73 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
75 public void onSuccess(RpcResult<AddGroupOutput> result) {
76 if (result.isSuccessful()) {
77 if(LOG.isDebugEnabled()) {
78 LOG.debug("group add with id={} finished without error", input.getGroupId().getValue());
80 addIfNecessaryToDS(input.getGroupId(), input);
82 LOG.error("group add with id={} failed, errors={}", input.getGroupId().getValue(),
83 errorsToString(result.getErrors()));
88 public void onFailure(Throwable t) {
89 LOG.error("Service call for group add failed for id={}. Exception: {}",
90 input.getGroupId().getValue(), t);
99 public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
100 final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture = updateGroup.handleServiceCall(input.getUpdatedGroup());
101 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
103 public void onSuccess(@Nullable RpcResult<UpdateGroupOutput> result) {
104 if (result.isSuccessful()) {
105 if(LOG.isDebugEnabled()) {
106 LOG.debug("Group update for original id {} succeded", input.getOriginalGroup().getGroupId().getValue());
108 removeIfNecessaryFromDS(input.getOriginalGroup().getGroupId());
109 addIfNecessaryToDS(input.getUpdatedGroup().getGroupId(), input.getUpdatedGroup());
111 LOG.error("group update failed with id={}, errors={}", input.getOriginalGroup().getGroupId(),
112 errorsToString(result.getErrors()));
117 public void onFailure(Throwable t) {
118 LOG.error("Service call for group update failed for id={}. Exception: {}",
119 input.getOriginalGroup().getGroupId(), t);
126 public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
127 removeGroup.getDeviceContext().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
128 final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture = removeGroup.handleServiceCall(input);
129 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
131 public void onSuccess(@Nullable RpcResult<RemoveGroupOutput> result) {
132 if (result.isSuccessful()) {
133 if(LOG.isDebugEnabled()) {
134 LOG.debug("Group remove for id {} succeded", input.getGroupId().getValue());
136 removeIfNecessaryFromDS(input.getGroupId());
138 LOG.error("group remove failed with id={}, errors={}", input.getGroupId().getValue(),
139 errorsToString(result.getErrors()));
144 public void onFailure(Throwable t) {
145 LOG.error("Service call for group remove failed for id={}. Exception: {}",
146 input.getGroupId().getValue(), t);
152 private void removeIfNecessaryFromDS(final GroupId groupId) {
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.onRemoved(groupPath);
161 private void addIfNecessaryToDS(final GroupId groupId, final Group data) {
162 if (itemLifecycleListener != null) {
163 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
164 = createGroupPath(groupId,
165 deviceContext.getDeviceState().getNodeInstanceIdentifier());
166 itemLifecycleListener.onAdded(groupPath, new GroupBuilder(data).build());
170 static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey>
171 createGroupPath(final GroupId groupId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
172 return nodePath.augmentation(FlowCapableNode.class).
173 child(org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group.class, new GroupKey(groupId));
176 private final String errorsToString(final Collection<RpcError> rpcErrors) {
177 final StringBuilder errors = new StringBuilder();
178 if ((null != rpcErrors) && (rpcErrors.size() > 0)) {
179 for (final RpcError rpcError : rpcErrors) {
180 errors.append(rpcError.getMessage());
183 return errors.toString();