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.sal;
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 org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
16 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerGroupService;
17 import org.opendaylight.openflowplugin.impl.services.singlelayer.SingleLayerGroupService;
18 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
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.GroupKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class SalGroupServiceImpl implements SalGroupService {
39 private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
40 private final MultiLayerGroupService<AddGroupInput, AddGroupOutput> addGroup;
41 private final MultiLayerGroupService<Group, UpdateGroupOutput> updateGroup;
42 private final MultiLayerGroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
43 private final SingleLayerGroupService<AddGroupOutput> addGroupMessage;
44 private final SingleLayerGroupService<UpdateGroupOutput> updateGroupMessage;
45 private final SingleLayerGroupService<RemoveGroupOutput> removeGroupMessage;
47 private final DeviceContext deviceContext;
49 public SalGroupServiceImpl(final RequestContextStack requestContextStack,
50 final DeviceContext deviceContext,
51 final ConvertorExecutor convertorExecutor) {
52 this.deviceContext = deviceContext;
53 addGroup = new MultiLayerGroupService<>(requestContextStack,
58 updateGroup = new MultiLayerGroupService<>(requestContextStack,
60 UpdateGroupOutput.class,
63 removeGroup = new MultiLayerGroupService<>(requestContextStack,
65 RemoveGroupOutput.class,
68 addGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
69 updateGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
70 removeGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
74 public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
75 final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture =
76 addGroupMessage.canUseSingleLayerSerialization()
77 ? addGroupMessage.handleServiceCall(input)
78 : addGroup.handleServiceCall(input);
80 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
82 public void onSuccess(RpcResult<AddGroupOutput> result) {
83 if (result.isSuccessful()) {
84 if (LOG.isDebugEnabled()) {
85 LOG.debug("Group add with id={} finished without error", input.getGroupId().getValue());
87 deviceContext.getDeviceGroupRegistry().store(input.getGroupId());
89 if (LOG.isDebugEnabled()) {
90 LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
91 ErrorUtil.errorsToString(result.getErrors()));
97 public void onFailure(Throwable throwable) {
98 LOG.warn("Service call for adding group={} failed, reason: {}",
99 input.getGroupId().getValue(),
108 public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
109 final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture =
110 updateGroupMessage.canUseSingleLayerSerialization()
111 ? updateGroupMessage.handleServiceCall(input.getUpdatedGroup())
112 : updateGroup.handleServiceCall(input.getUpdatedGroup());
114 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
116 public void onSuccess(RpcResult<UpdateGroupOutput> result) {
117 if (result.isSuccessful()) {
118 if (LOG.isDebugEnabled()) {
119 LOG.debug("Group update with original id={} finished without error",
120 input.getOriginalGroup().getGroupId().getValue());
123 LOG.warn("Group update with original id={} failed, errors={}",
124 input.getOriginalGroup().getGroupId(), ErrorUtil.errorsToString(result.getErrors()));
125 LOG.debug("Group input={}", input.getUpdatedGroup());
130 public void onFailure(Throwable throwable) {
131 LOG.warn("Service call for updating group={} failed, reason: {}",
132 input.getOriginalGroup().getGroupId(), throwable);
139 public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
140 final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture =
141 removeGroupMessage.canUseSingleLayerSerialization()
142 ? removeGroupMessage.handleServiceCall(input)
143 : removeGroup.handleServiceCall(input);
145 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
147 public void onSuccess(RpcResult<RemoveGroupOutput> result) {
148 if (result.isSuccessful()) {
149 if (LOG.isDebugEnabled()) {
150 LOG.debug("Group remove with id={} finished without error", input.getGroupId().getValue());
152 removeGroup.getDeviceRegistry().getDeviceGroupRegistry().addMark(input.getGroupId());
154 LOG.warn("Group remove with id={} failed, errors={}", input.getGroupId().getValue(),
155 ErrorUtil.errorsToString(result.getErrors()));
156 LOG.debug("Group input={}", input);
161 public void onFailure(Throwable throwable) {
162 LOG.warn("Service call for removing group={} failed, reason: {}",
163 input.getGroupId().getValue(), throwable);
169 private static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn
170 .opendaylight.group.types.rev131018.groups.Group, GroupKey>
171 createGroupPath(final GroupId groupId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
172 return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn
173 .opendaylight.group.types.rev131018.groups.Group.class, new GroupKey(groupId));