Remove Itemlifecycle
[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 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.flow.inventory.rev130819.FlowCapableNode;
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.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class SalGroupServiceImpl implements SalGroupService {
39     private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
40     private final MultiLayerGroupService<AddGroupInput, AddGroupOutput> addGroup;
41     private final MultiLayerGroupService<Group, UpdateGroupOutput> updateGroup;
42     private final MultiLayerGroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
43     private final SingleLayerGroupService<AddGroupOutput> addGroupMessage;
44     private final SingleLayerGroupService<UpdateGroupOutput> updateGroupMessage;
45     private final SingleLayerGroupService<RemoveGroupOutput> removeGroupMessage;
46
47     private final DeviceContext deviceContext;
48
49     public SalGroupServiceImpl(final RequestContextStack requestContextStack,
50                                final DeviceContext deviceContext,
51                                final ConvertorExecutor convertorExecutor) {
52         this.deviceContext = deviceContext;
53         addGroup = new MultiLayerGroupService<>(requestContextStack,
54                                                 deviceContext,
55                                                 AddGroupOutput.class,
56                                                 convertorExecutor);
57
58         updateGroup = new MultiLayerGroupService<>(requestContextStack,
59                                                    deviceContext,
60                                                    UpdateGroupOutput.class,
61                                                    convertorExecutor);
62
63         removeGroup = new MultiLayerGroupService<>(requestContextStack,
64                                                    deviceContext,
65                                                    RemoveGroupOutput.class,
66                                                    convertorExecutor);
67
68         addGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
69         updateGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
70         removeGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
71     }
72
73     @Override
74     public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
75         final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture =
76             addGroupMessage.canUseSingleLayerSerialization()
77             ? addGroupMessage.handleServiceCall(input)
78             : addGroup.handleServiceCall(input);
79
80         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
81             @Override
82             public void onSuccess(RpcResult<AddGroupOutput> result) {
83                 if (result.isSuccessful()) {
84                     if (LOG.isDebugEnabled()) {
85                         LOG.debug("Group add with id={} finished without error", input.getGroupId().getValue());
86                     }
87                     deviceContext.getDeviceGroupRegistry().store(input.getGroupId());
88                 } else {
89                     if (LOG.isDebugEnabled()) {
90                         LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
91                             ErrorUtil.errorsToString(result.getErrors()));
92                     }
93                 }
94             }
95
96             @Override
97             public void onFailure(Throwable throwable) {
98                 LOG.warn("Service call for adding group={} failed, reason: {}",
99                           input.getGroupId().getValue(),
100                           throwable);
101             }
102         });
103         return resultFuture;
104     }
105
106
107     @Override
108     public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
109         final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture =
110             updateGroupMessage.canUseSingleLayerSerialization()
111             ? updateGroupMessage.handleServiceCall(input.getUpdatedGroup())
112             : updateGroup.handleServiceCall(input.getUpdatedGroup());
113
114         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
115             @Override
116             public void onSuccess(RpcResult<UpdateGroupOutput> result) {
117                 if (result.isSuccessful()) {
118                     if (LOG.isDebugEnabled()) {
119                         LOG.debug("Group update with original id={} finished without error",
120                             input.getOriginalGroup().getGroupId().getValue());
121                     }
122                 } else {
123                     LOG.warn("Group update with original id={} failed, errors={}",
124                         input.getOriginalGroup().getGroupId(), ErrorUtil.errorsToString(result.getErrors()));
125                     LOG.debug("Group input={}", input.getUpdatedGroup());
126                 }
127             }
128
129             @Override
130             public void onFailure(Throwable throwable) {
131                 LOG.warn("Service call for updating group={} failed, reason: {}",
132                         input.getOriginalGroup().getGroupId(), throwable);
133             }
134         });
135         return resultFuture;
136     }
137
138     @Override
139     public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
140         final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture =
141             removeGroupMessage.canUseSingleLayerSerialization()
142             ? removeGroupMessage.handleServiceCall(input)
143             : removeGroup.handleServiceCall(input);
144
145         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
146             @Override
147             public void onSuccess(RpcResult<RemoveGroupOutput> result) {
148                 if (result.isSuccessful()) {
149                     if (LOG.isDebugEnabled()) {
150                         LOG.debug("Group remove with id={} finished without error", input.getGroupId().getValue());
151                     }
152                     removeGroup.getDeviceRegistry().getDeviceGroupRegistry().addMark(input.getGroupId());
153                 } else {
154                     LOG.warn("Group remove with id={} failed, errors={}", input.getGroupId().getValue(),
155                         ErrorUtil.errorsToString(result.getErrors()));
156                     LOG.debug("Group input={}", input);
157                 }
158             }
159
160             @Override
161             public void onFailure(Throwable throwable) {
162                 LOG.warn("Service call for removing group={} failed, reason: {}",
163                         input.getGroupId().getValue(), throwable);
164             }
165         });
166         return resultFuture;
167     }
168
169     private static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn
170             .opendaylight.group.types.rev131018.groups.Group, GroupKey>
171         createGroupPath(final GroupId groupId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
172         return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn
173                 .opendaylight.group.types.rev131018.groups.Group.class, new GroupKey(groupId));
174     }
175 }