Merge "Bug 6372 table-miss-enforcer - DTCL instead of DTL"
[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.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.RpcError;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class SalGroupServiceImpl implements SalGroupService, ItemLifeCycleSource {
42     private static final Logger LOG = LoggerFactory.getLogger(SalGroupServiceImpl.class);
43     private final GroupService<AddGroupInput, AddGroupOutput> addGroup;
44     private final GroupService<Group, UpdateGroupOutput> updateGroup;
45     private final GroupService<RemoveGroupInput, RemoveGroupOutput> removeGroup;
46     private final DeviceContext deviceContext;
47     private ItemLifecycleListener itemLifecycleListener;
48
49     public SalGroupServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
50         this.deviceContext = deviceContext;
51         addGroup = new GroupService<>(requestContextStack, deviceContext, AddGroupOutput.class, convertorExecutor);
52         updateGroup = new GroupService<>(requestContextStack, deviceContext, UpdateGroupOutput.class, convertorExecutor);
53         removeGroup = new GroupService<>(requestContextStack, deviceContext, RemoveGroupOutput.class, convertorExecutor);
54     }
55
56     @Override
57     public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
58         this.itemLifecycleListener = itemLifecycleListener;
59     }
60
61     @Override
62     public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
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                     deviceContext.getDeviceGroupRegistry().store(input.getGroupId());
72                     addIfNecessaryToDS(input.getGroupId(), input);
73                 } else {
74                     LOG.error("group add with id={} failed, errors={}", input.getGroupId().getValue(),
75                             errorsToString(result.getErrors()));
76                 }
77             }
78
79             @Override
80             public void onFailure(Throwable t) {
81                 LOG.error("Service call for group add failed for id={}. Exception: {}", 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         final ListenableFuture<RpcResult<RemoveGroupOutput>> resultFuture = removeGroup.handleServiceCall(input);
119         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveGroupOutput>>() {
120             @Override
121             public void onSuccess(@Nullable RpcResult<RemoveGroupOutput> result) {
122                 if (result.isSuccessful()) {
123                     if(LOG.isDebugEnabled()) {
124                         LOG.debug("Group remove for id {} succeded", input.getGroupId().getValue());
125                     }
126                     removeGroup.getDeviceRegistry().getDeviceGroupRegistry().markToBeremoved(input.getGroupId());
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.getDeviceInfo().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.getDeviceInfo().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 }