Fix unused import warnings
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / 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;
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.Collection;
14 import java.util.concurrent.Future;
15 import javax.annotation.Nullable;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
19 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
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.GroupBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
34 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.RpcError;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class SalGroupServiceImpl implements SalGroupService, ItemLifeCycleSource {
41     private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
42     private final GroupService<AddGroupInput, AddGroupOutput> addGroup;
43     private final GroupService<Group, UpdateGroupOutput> updateGroup;
44     private final GroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
45     private final DeviceContext deviceContext;
46     private ItemLifecycleListener itemLifecycleListener;
47
48     public SalGroupServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
49         this.deviceContext = deviceContext;
50         addGroup = new GroupService<>(requestContextStack, deviceContext, AddGroupOutput.class);
51         updateGroup = new GroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class);
52         removeGroup = new GroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class);
53     }
54
55     @Override
56     public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
57         this.itemLifecycleListener = itemLifecycleListener;
58     }
59
60     @Override
61     public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
62         addGroup.getDeviceContext().getDeviceGroupRegistry().store(input.getGroupId());
63         final ListenableFuture<RpcResult<AddGroupOutput>> resultFuture = addGroup.handleServiceCall(input);
64         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddGroupOutput>>() {
65             @Override
66             public void onSuccess(RpcResult<AddGroupOutput> result) {
67                 if (result.isSuccessful()) {
68                     if(LOG.isDebugEnabled()) {
69                         LOG.debug("group add with id={} finished without error", input.getGroupId().getValue());
70                     }
71                     addIfNecessaryToDS(input.getGroupId(), input);
72                 } else {
73                 LOG.error("group add with id={} failed, errors={}", input.getGroupId().getValue(),
74                         errorsToString(result.getErrors()));
75                  }
76             }
77
78             @Override
79             public void onFailure(Throwable t) {
80                 LOG.error("Service call for group add failed for id={}. Exception: {}",
81                         input.getGroupId().getValue(), t);
82             }
83         });
84
85         return resultFuture;
86     }
87
88
89     @Override
90     public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
91         final ListenableFuture<RpcResult<UpdateGroupOutput>> resultFuture = updateGroup.handleServiceCall(input.getUpdatedGroup());
92         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateGroupOutput>>() {
93             @Override
94             public void onSuccess(@Nullable RpcResult<UpdateGroupOutput> result) {
95                 if (result.isSuccessful()) {
96                     if(LOG.isDebugEnabled()) {
97                         LOG.debug("Group update for original id {} succeded", input.getOriginalGroup().getGroupId().getValue());
98                     }
99                     removeIfNecessaryFromDS(input.getOriginalGroup().getGroupId());
100                     addIfNecessaryToDS(input.getUpdatedGroup().getGroupId(), input.getUpdatedGroup());
101                 }else{
102                     LOG.error("group update failed with id={}, errors={}", input.getOriginalGroup().getGroupId(),
103                             errorsToString(result.getErrors()));
104                 }
105             }
106
107             @Override
108             public void onFailure(Throwable t) {
109                 LOG.error("Service call for group  update failed for id={}. Exception: {}",
110                         input.getOriginalGroup().getGroupId(), t);
111             }
112         });
113         return resultFuture;
114     }
115
116     @Override
117     public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
118         removeGroup.getDeviceContext().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
119         final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture = removeGroup.handleServiceCall(input);
120         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
121             @Override
122             public void onSuccess(@Nullable RpcResult<RemoveGroupOutput> result) {
123                 if (result.isSuccessful()) {
124                     if(LOG.isDebugEnabled()) {
125                         LOG.debug("Group remove for id {} succeded", input.getGroupId().getValue());
126                     }
127                     removeIfNecessaryFromDS(input.getGroupId());
128                 }else{
129                     LOG.error("group remove failed with id={}, errors={}", input.getGroupId().getValue(),
130                             errorsToString(result.getErrors()));
131                 }
132             }
133
134             @Override
135             public void onFailure(Throwable t) {
136                 LOG.error("Service call for group remove failed for id={}. Exception: {}",
137                         input.getGroupId().getValue(), t);
138             }
139         });
140         return resultFuture;
141     }
142
143     private void removeIfNecessaryFromDS(final GroupId groupId) {
144         if (itemLifecycleListener != null) {
145             KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
146                     = createGroupPath(groupId,
147                     deviceContext.getDeviceState().getNodeInstanceIdentifier());
148             itemLifecycleListener.onRemoved(groupPath);
149         }
150     }
151
152     private void addIfNecessaryToDS(final GroupId groupId, final Group data) {
153         if (itemLifecycleListener != null) {
154             KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey> groupPath
155                     = createGroupPath(groupId,
156                     deviceContext.getDeviceState().getNodeInstanceIdentifier());
157             itemLifecycleListener.onAdded(groupPath, new GroupBuilder(data).build());
158         }
159     }
160
161     static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group, GroupKey>
162     createGroupPath(final GroupId groupId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
163         return nodePath.augmentation(FlowCapableNode.class).
164                 child(org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group.class, new GroupKey(groupId));
165     }
166
167     private final String errorsToString(final Collection<RpcError> rpcErrors) {
168         final StringBuilder errors = new StringBuilder();
169         if ((null != rpcErrors) && (rpcErrors.size() > 0)) {
170             for (final RpcError rpcError : rpcErrors) {
171                 errors.append(rpcError.getMessage());
172             }
173         }
174         return errors.toString();
175     }
176 }