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 javax.annotation.Nullable;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
18 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
19 import org.opendaylight.openflowplugin.impl.services.singlelayer.SingleLayerGroupService;
20 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerGroupService;
21 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class SalGroupServiceImpl implements SalGroupService, ItemLifeCycleSource {
43 private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
44 private final MultiLayerGroupService<AddGroupInput, AddGroupOutput> addGroup;
45 private final MultiLayerGroupService<Group, UpdateGroupOutput> updateGroup;
46 private final MultiLayerGroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
47 private final SingleLayerGroupService<AddGroupOutput> addGroupMessage;
48 private final SingleLayerGroupService<UpdateGroupOutput> updateGroupMessage;
49 private final SingleLayerGroupService<RemoveGroupOutput> removeGroupMessage;
51 private final DeviceContext deviceContext;
52 private ItemLifecycleListener itemLifecycleListener;
54 public SalGroupServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
55 this.deviceContext = deviceContext;
56 addGroup = new MultiLayerGroupService<>(requestContextStack, deviceContext, AddGroupOutput.class, convertorExecutor);
57 updateGroup = new MultiLayerGroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class, convertorExecutor);
58 removeGroup = new MultiLayerGroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class, convertorExecutor);
60 addGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
61 updateGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
62 removeGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
66 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
67 this.itemLifecycleListener = itemLifecycleListener;
71 public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
72 final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture = addGroupMessage.canUseSingleLayerSerialization()
73 ? addGroupMessage.handleServiceCall(input)
74 : addGroup.handleServiceCall(input);
76 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
78 public void onSuccess(RpcResult<AddGroupOutput> result) {
79 if (result.isSuccessful()) {
80 if (LOG.isDebugEnabled()) {
81 LOG.debug("Group add with id={} finished without error", input.getGroupId().getValue());
83 deviceContext.getDeviceGroupRegistry().store(input.getGroupId());
84 addIfNecessaryToDS(input.getGroupId(), input);
86 if (LOG.isDebugEnabled()) {
87 LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
88 ErrorUtil.errorsToString(result.getErrors()));
94 public void onFailure(Throwable t) {
95 LOG.warn("Service call for adding group={} failed, reason: {}", input.getGroupId().getValue(), t);
103 public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
104 final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture = updateGroupMessage.canUseSingleLayerSerialization()
105 ? updateGroupMessage.handleServiceCall(input.getUpdatedGroup())
106 : updateGroup.handleServiceCall(input.getUpdatedGroup());
108 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
110 public void onSuccess(RpcResult<UpdateGroupOutput> result) {
111 if (result.isSuccessful()) {
112 if (LOG.isDebugEnabled()) {
113 LOG.debug("Group update with original id={} finished without error",
114 input.getOriginalGroup().getGroupId().getValue());
116 removeIfNecessaryFromDS(input.getOriginalGroup().getGroupId());
117 addIfNecessaryToDS(input.getUpdatedGroup().getGroupId(), input.getUpdatedGroup());
119 LOG.warn("Group update with original id={} failed, errors={}",
120 input.getOriginalGroup().getGroupId(), ErrorUtil.errorsToString(result.getErrors()));
121 LOG.debug("Group input={}", input.getUpdatedGroup());
126 public void onFailure(Throwable t) {
127 LOG.warn("Service call for updating group={} failed, reason: {}",
128 input.getOriginalGroup().getGroupId(), t);
135 public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
136 final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture = removeGroupMessage.canUseSingleLayerSerialization()
137 ? removeGroupMessage.handleServiceCall(input)
138 : removeGroup.handleServiceCall(input);
140 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
142 public void onSuccess(RpcResult<RemoveGroupOutput> result) {
143 if (result.isSuccessful()) {
144 if (LOG.isDebugEnabled()) {
145 LOG.debug("Group remove with id={} finished without error", input.getGroupId().getValue());
147 removeGroup.getDeviceRegistry().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
148 removeIfNecessaryFromDS(input.getGroupId());
150 LOG.warn("Group remove with id={} failed, errors={}", input.getGroupId().getValue(),
151 ErrorUtil.errorsToString(result.getErrors()));
152 LOG.debug("Group input={}", input);
157 public void onFailure(Throwable t) {
158 LOG.warn("Service call for removing group={} failed, reason: {}",
159 input.getGroupId().getValue(), t);
165 private void removeIfNecessaryFromDS(final GroupId groupId) {
166 if (itemLifecycleListener != null) {
167 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
168 = createGroupPath(groupId,
169 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
170 itemLifecycleListener.onRemoved(groupPath);
174 private void addIfNecessaryToDS(final GroupId groupId, final Group data) {
175 if (itemLifecycleListener != null) {
176 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
177 = createGroupPath(groupId,
178 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
179 itemLifecycleListener.onAdded(groupPath, new GroupBuilder(data).build());
183 private static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey>
184 createGroupPath(final GroupId groupId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
185 return nodePath.augmentation(FlowCapableNode.class).
186 child(org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group.class, new GroupKey(groupId));