Fix errors in serializers and deserializers
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / SalGroupServiceImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.services.sal;
9
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;
41
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;
50
51     private final DeviceContext deviceContext;
52     private ItemLifecycleListener itemLifecycleListener;
53
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);
59
60         addGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
61         updateGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
62         removeGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
63     }
64
65     @Override
66     public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
67         this.itemLifecycleListener = itemLifecycleListener;
68     }
69
70     @Override
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);
75
76         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
77             @Override
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());
82                     }
83                     deviceContext.getDeviceGroupRegistry().store(input.getGroupId());
84                     addIfNecessaryToDS(input.getGroupId(), input);
85                 } else {
86                     if (LOG.isDebugEnabled()) {
87                         LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
88                             ErrorUtil.errorsToString(result.getErrors()));
89                     }
90                 }
91             }
92
93             @Override
94             public void onFailure(Throwable t) {
95                 LOG.warn("Service call for adding group={} failed, reason: {}", input.getGroupId().getValue(), t);
96             }
97         });
98         return resultFuture;
99     }
100
101
102     @Override
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());
107
108         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
109             @Override
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());
115                     }
116                     removeIfNecessaryFromDS(input.getOriginalGroup().getGroupId());
117                     addIfNecessaryToDS(input.getUpdatedGroup().getGroupId(), input.getUpdatedGroup());
118                 } else {
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());
122                 }
123             }
124
125             @Override
126             public void onFailure(Throwable t) {
127                 LOG.warn("Service call for updating group={} failed, reason: {}",
128                         input.getOriginalGroup().getGroupId(), t);
129             }
130         });
131         return resultFuture;
132     }
133
134     @Override
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);
139
140         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
141             @Override
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());
146                     }
147                     removeGroup.getDeviceRegistry().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
148                     removeIfNecessaryFromDS(input.getGroupId());
149                 } else {
150                     LOG.warn("Group remove with id={} failed, errors={}", input.getGroupId().getValue(),
151                         ErrorUtil.errorsToString(result.getErrors()));
152                     LOG.debug("Group input={}", input);
153                 }
154             }
155
156             @Override
157             public void onFailure(Throwable t) {
158                 LOG.warn("Service call for removing group={} failed, reason: {}",
159                         input.getGroupId().getValue(), t);
160             }
161         });
162         return resultFuture;
163     }
164
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);
171         }
172     }
173
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());
180         }
181     }
182
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));
187     }
188 }