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 com.google.common.util.concurrent.MoreExecutors;
14 import java.util.concurrent.Future;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerGroupService;
19 import org.opendaylight.openflowplugin.impl.services.singlelayer.SingleLayerGroupService;
20 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
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.yangtools.yang.common.RpcResult;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class SalGroupServiceImpl implements SalGroupService {
35 private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
36 private final MultiLayerGroupService<AddGroupInput, AddGroupOutput> addGroup;
37 private final MultiLayerGroupService<Group, UpdateGroupOutput> updateGroup;
38 private final MultiLayerGroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
39 private final SingleLayerGroupService<AddGroupOutput> addGroupMessage;
40 private final SingleLayerGroupService<UpdateGroupOutput> updateGroupMessage;
41 private final SingleLayerGroupService<RemoveGroupOutput> removeGroupMessage;
43 private final DeviceContext deviceContext;
45 public SalGroupServiceImpl(final RequestContextStack requestContextStack,
46 final DeviceContext deviceContext,
47 final ConvertorExecutor convertorExecutor) {
48 this.deviceContext = deviceContext;
49 addGroup = new MultiLayerGroupService<>(requestContextStack,
54 updateGroup = new MultiLayerGroupService<>(requestContextStack,
56 UpdateGroupOutput.class,
59 removeGroup = new MultiLayerGroupService<>(requestContextStack,
61 RemoveGroupOutput.class,
64 addGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
65 updateGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
66 removeGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
70 public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
71 final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture =
72 addGroupMessage.canUseSingleLayerSerialization()
73 ? addGroupMessage.handleServiceCall(input)
74 : addGroup.handleServiceCall(input);
76 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
78 public void onSuccess(@Nonnull RpcResult<AddGroupOutput> result) {
79 if (result.isSuccessful()) {
80 LOG.debug("adding group successful {}", input.getGroupId().getValue());
82 if (LOG.isDebugEnabled()) {
83 LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
84 ErrorUtil.errorsToString(result.getErrors()));
90 public void onFailure(Throwable throwable) {
91 LOG.warn("Service call for adding group={} failed, reason: {}",
92 input.getGroupId().getValue(),
95 }, MoreExecutors.directExecutor());
101 public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
102 final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture =
103 updateGroupMessage.canUseSingleLayerSerialization()
104 ? updateGroupMessage.handleServiceCall(input.getUpdatedGroup())
105 : updateGroup.handleServiceCall(input.getUpdatedGroup());
107 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
109 public void onSuccess(@Nonnull RpcResult<UpdateGroupOutput> result) {
110 if (result.isSuccessful()) {
111 if (LOG.isDebugEnabled()) {
112 LOG.debug("Group update with original id={} finished without error",
113 input.getOriginalGroup().getGroupId().getValue());
116 LOG.warn("Group update with original id={} failed, errors={}",
117 input.getOriginalGroup().getGroupId(), ErrorUtil.errorsToString(result.getErrors()));
118 LOG.debug("Group input={}", input.getUpdatedGroup());
123 public void onFailure(Throwable throwable) {
124 LOG.warn("Service call for updating group={} failed, reason: {}",
125 input.getOriginalGroup().getGroupId(), throwable);
127 }, MoreExecutors.directExecutor());
132 public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
133 final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture =
134 removeGroupMessage.canUseSingleLayerSerialization()
135 ? removeGroupMessage.handleServiceCall(input)
136 : removeGroup.handleServiceCall(input);
138 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
140 public void onSuccess(@Nonnull RpcResult<RemoveGroupOutput> result) {
141 if (result.isSuccessful()) {
142 if (LOG.isDebugEnabled()) {
143 LOG.debug("Group remove with id={} finished without error", input.getGroupId().getValue());
146 LOG.warn("Group remove with id={} failed, errors={}", input.getGroupId().getValue(),
147 ErrorUtil.errorsToString(result.getErrors()));
148 LOG.debug("Group input={}", input);
153 public void onFailure(Throwable throwable) {
154 LOG.warn("Service call for removing group={} failed, reason: {}",
155 input.getGroupId().getValue(), throwable);
157 }, MoreExecutors.directExecutor());