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 javax.annotation.Nonnull;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerGroupService;
18 import org.opendaylight.openflowplugin.impl.services.singlelayer.SingleLayerGroupService;
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.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.yangtools.yang.common.RpcResult;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class SalGroupServiceImpl implements SalGroupService {
34 private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
35 private final MultiLayerGroupService<AddGroupInput, AddGroupOutput> addGroup;
36 private final MultiLayerGroupService<Group, UpdateGroupOutput> updateGroup;
37 private final MultiLayerGroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
38 private final SingleLayerGroupService<AddGroupOutput> addGroupMessage;
39 private final SingleLayerGroupService<UpdateGroupOutput> updateGroupMessage;
40 private final SingleLayerGroupService<RemoveGroupOutput> removeGroupMessage;
42 private final DeviceContext deviceContext;
44 public SalGroupServiceImpl(final RequestContextStack requestContextStack,
45 final DeviceContext deviceContext,
46 final ConvertorExecutor convertorExecutor) {
47 this.deviceContext = deviceContext;
48 addGroup = new MultiLayerGroupService<>(requestContextStack,
53 updateGroup = new MultiLayerGroupService<>(requestContextStack,
55 UpdateGroupOutput.class,
58 removeGroup = new MultiLayerGroupService<>(requestContextStack,
60 RemoveGroupOutput.class,
63 addGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
64 updateGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
65 removeGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
69 public ListenableFuture<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
70 final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture =
71 addGroupMessage.canUseSingleLayerSerialization()
72 ? addGroupMessage.handleServiceCall(input)
73 : addGroup.handleServiceCall(input);
75 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
77 public void onSuccess(@Nonnull RpcResult<AddGroupOutput> result) {
78 if (result.isSuccessful()) {
79 LOG.debug("adding group successful {}", input.getGroupId().getValue());
81 if (LOG.isDebugEnabled()) {
82 LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
83 ErrorUtil.errorsToString(result.getErrors()));
89 public void onFailure(Throwable throwable) {
90 LOG.warn("Service call for adding group={} failed",
91 input.getGroupId().getValue(),
94 }, MoreExecutors.directExecutor());
100 public ListenableFuture<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
101 final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture =
102 updateGroupMessage.canUseSingleLayerSerialization()
103 ? updateGroupMessage.handleServiceCall(input.getUpdatedGroup())
104 : updateGroup.handleServiceCall(input.getUpdatedGroup());
106 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
108 public void onSuccess(@Nonnull RpcResult<UpdateGroupOutput> result) {
109 if (result.isSuccessful()) {
110 if (LOG.isDebugEnabled()) {
111 LOG.debug("Group update with original id={} finished without error",
112 input.getOriginalGroup().getGroupId().getValue());
115 LOG.warn("Group update with original id={} failed, errors={}",
116 input.getOriginalGroup().getGroupId(), ErrorUtil.errorsToString(result.getErrors()));
117 LOG.debug("Group input={}", input.getUpdatedGroup());
122 public void onFailure(Throwable throwable) {
123 LOG.warn("Service call for updating group={} failed",
124 input.getOriginalGroup().getGroupId(), throwable);
126 }, MoreExecutors.directExecutor());
131 public ListenableFuture<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
132 final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture =
133 removeGroupMessage.canUseSingleLayerSerialization()
134 ? removeGroupMessage.handleServiceCall(input)
135 : removeGroup.handleServiceCall(input);
137 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
139 public void onSuccess(@Nonnull RpcResult<RemoveGroupOutput> result) {
140 if (result.isSuccessful()) {
141 if (LOG.isDebugEnabled()) {
142 LOG.debug("Group remove with id={} finished without error", input.getGroupId().getValue());
145 LOG.warn("Group remove with id={} failed, errors={}", input.getGroupId().getValue(),
146 ErrorUtil.errorsToString(result.getErrors()));
147 LOG.debug("Group input={}", input);
152 public void onFailure(Throwable throwable) {
153 LOG.warn("Service call for removing group={} failed",
154 input.getGroupId().getValue(), throwable);
156 }, MoreExecutors.directExecutor());