Merge "System ready integration for OFP"
[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.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;
31
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;
40
41     private final DeviceContext deviceContext;
42
43     public SalGroupServiceImpl(final RequestContextStack requestContextStack,
44                                final DeviceContext deviceContext,
45                                final ConvertorExecutor convertorExecutor) {
46         this.deviceContext = deviceContext;
47         addGroup = new MultiLayerGroupService<>(requestContextStack,
48                                                 deviceContext,
49                                                 AddGroupOutput.class,
50                                                 convertorExecutor);
51
52         updateGroup = new MultiLayerGroupService<>(requestContextStack,
53                                                    deviceContext,
54                                                    UpdateGroupOutput.class,
55                                                    convertorExecutor);
56
57         removeGroup = new MultiLayerGroupService<>(requestContextStack,
58                                                    deviceContext,
59                                                    RemoveGroupOutput.class,
60                                                    convertorExecutor);
61
62         addGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
63         updateGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
64         removeGroupMessage = new SingleLayerGroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
65     }
66
67     @Override
68     public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
69         final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture =
70             addGroupMessage.canUseSingleLayerSerialization()
71             ? addGroupMessage.handleServiceCall(input)
72             : addGroup.handleServiceCall(input);
73
74         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
75             @Override
76             public void onSuccess(RpcResult<AddGroupOutput> result) {
77                 if (result.isSuccessful()) {
78                     if (LOG.isDebugEnabled()) {
79                         LOG.debug("Group add with id={} finished without error", input.getGroupId().getValue());
80                     }
81                     deviceContext.getDeviceGroupRegistry().store(input.getGroupId());
82                 } else {
83                     if (LOG.isDebugEnabled()) {
84                         LOG.debug("Group add with id={} failed, errors={}", input.getGroupId().getValue(),
85                             ErrorUtil.errorsToString(result.getErrors()));
86                     }
87                 }
88             }
89
90             @Override
91             public void onFailure(Throwable throwable) {
92                 LOG.warn("Service call for adding group={} failed, reason: {}",
93                           input.getGroupId().getValue(),
94                           throwable);
95             }
96         });
97         return resultFuture;
98     }
99
100
101     @Override
102     public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
103         final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture =
104             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                 } else {
117                     LOG.warn("Group update with original id={} failed, errors={}",
118                         input.getOriginalGroup().getGroupId(), ErrorUtil.errorsToString(result.getErrors()));
119                     LOG.debug("Group input={}", input.getUpdatedGroup());
120                 }
121             }
122
123             @Override
124             public void onFailure(Throwable throwable) {
125                 LOG.warn("Service call for updating group={} failed, reason: {}",
126                         input.getOriginalGroup().getGroupId(), throwable);
127             }
128         });
129         return resultFuture;
130     }
131
132     @Override
133     public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
134         final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture =
135             removeGroupMessage.canUseSingleLayerSerialization()
136             ? removeGroupMessage.handleServiceCall(input)
137             : removeGroup.handleServiceCall(input);
138
139         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
140             @Override
141             public void onSuccess(RpcResult<RemoveGroupOutput> result) {
142                 if (result.isSuccessful()) {
143                     if (LOG.isDebugEnabled()) {
144                         LOG.debug("Group remove with id={} finished without error", input.getGroupId().getValue());
145                     }
146                     removeGroup.getDeviceRegistry().getDeviceGroupRegistry().addMark(input.getGroupId());
147                 } else {
148                     LOG.warn("Group remove with id={} failed, errors={}", input.getGroupId().getValue(),
149                         ErrorUtil.errorsToString(result.getErrors()));
150                     LOG.debug("Group input={}", input);
151                 }
152             }
153
154             @Override
155             public void onFailure(Throwable throwable) {
156                 LOG.warn("Service call for removing group={} failed, reason: {}",
157                         input.getGroupId().getValue(), throwable);
158             }
159         });
160         return resultFuture;
161     }
162
163 }