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 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.group.service.rev130918.AddGroupInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class SalGroupServiceImpl implements SalGroupService {
33 private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
34 private final MultiLayerGroupService<AddGroupInput, AddGroupOutput> addGroup;
35 private final MultiLayerGroupService<Group, UpdateGroupOutput> updateGroup;
36 private final MultiLayerGroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
37 private final SingleLayerGroupService<AddGroupOutput> addGroupMessage;
38 private final SingleLayerGroupService<UpdateGroupOutput> updateGroupMessage;
39 private final SingleLayerGroupService<RemoveGroupOutput> removeGroupMessage;
41 private final DeviceContext deviceContext;
43 public SalGroupServiceImpl(final RequestContextStack requestContextStack,
44 final DeviceContext deviceContext,
45 final ConvertorExecutor convertorExecutor) {
46 this.deviceContext = deviceContext;
47 addGroup = new MultiLayerGroupService<>(requestContextStack,
52 updateGroup = new MultiLayerGroupService<>(requestContextStack,
54 UpdateGroupOutput.class,
57 removeGroup = new MultiLayerGroupService<>(requestContextStack,
59 RemoveGroupOutput.class,
62 addGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
63 updateGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
64 removeGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
68 public ListenableFuture<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
69 final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture =
70 addGroupMessage.canUseSingleLayerSerialization()
71 ? addGroupMessage.handleServiceCall(input)
72 : addGroup.handleServiceCall(input);
74 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
76 public void onSuccess(RpcResult<AddGroupOutput> result) {
77 if (result.isSuccessful()) {
78 LOG.debug("adding group successful {}", input.getGroupId().getValue());
80 if (LOG.isDebugEnabled()) {
81 LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
82 ErrorUtil.errorsToString(result.getErrors()));
88 public void onFailure(Throwable throwable) {
89 LOG.warn("Service call for adding group={} failed",
90 input.getGroupId().getValue(),
93 }, MoreExecutors.directExecutor());
99 public ListenableFuture<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
100 final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture =
101 updateGroupMessage.canUseSingleLayerSerialization()
102 ? updateGroupMessage.handleServiceCall(input.getUpdatedGroup())
103 : updateGroup.handleServiceCall(input.getUpdatedGroup());
105 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
107 public void onSuccess(RpcResult<UpdateGroupOutput> result) {
108 if (result.isSuccessful()) {
109 if (LOG.isDebugEnabled()) {
110 LOG.debug("Group update with original id={} finished without error",
111 input.getOriginalGroup().getGroupId().getValue());
114 LOG.warn("Group update with original id={} failed, errors={}",
115 input.getOriginalGroup().getGroupId(), ErrorUtil.errorsToString(result.getErrors()));
116 LOG.debug("Group input={}", input.getUpdatedGroup());
121 public void onFailure(Throwable throwable) {
122 LOG.warn("Service call for updating group={} failed",
123 input.getOriginalGroup().getGroupId(), throwable);
125 }, MoreExecutors.directExecutor());
130 public ListenableFuture<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
131 final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture =
132 removeGroupMessage.canUseSingleLayerSerialization()
133 ? removeGroupMessage.handleServiceCall(input)
134 : removeGroup.handleServiceCall(input);
136 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
138 public void onSuccess(RpcResult<RemoveGroupOutput> result) {
139 if (result.isSuccessful()) {
140 if (LOG.isDebugEnabled()) {
141 LOG.debug("Group remove with id={} finished without error", input.getGroupId().getValue());
144 LOG.warn("Group remove with id={} failed, errors={}", input.getGroupId().getValue(),
145 ErrorUtil.errorsToString(result.getErrors()));
146 LOG.debug("Group input={}", input);
151 public void onFailure(Throwable throwable) {
152 LOG.warn("Service call for removing group={} failed",
153 input.getGroupId().getValue(), throwable);
155 }, MoreExecutors.directExecutor());