1 package org.opendaylight.controller.forwardingrulesmanager_mdsal.consumer.impl;
3 import java.util.ArrayList;
4 import java.util.EnumSet;
5 import java.util.HashMap;
6 import java.util.HashSet;
7 import java.util.Iterator;
11 import java.util.Map.Entry;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
15 import org.opendaylight.controller.clustering.services.CacheConfigException;
16 import org.opendaylight.controller.clustering.services.CacheExistException;
17 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
18 import org.opendaylight.controller.clustering.services.IClusterServices;
19 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
20 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
21 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction;
22 import org.opendaylight.controller.sal.common.util.Rpcs;
23 import org.opendaylight.controller.sal.core.IContainer;
24 import org.opendaylight.controller.sal.core.Node;
25 import org.opendaylight.controller.sal.utils.GlobalConstants;
26 import org.opendaylight.controller.sal.utils.Status;
27 import org.opendaylight.controller.sal.utils.StatusCode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.config.rev131024.Groups;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.config.rev131024.groups.Group;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.config.rev131024.groups.GroupKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupAdded;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemoved;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdated;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupListener;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes.GroupType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
41 import org.opendaylight.yangtools.concepts.Registration;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.binding.NotificationListener;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 @SuppressWarnings("unused")
50 public class GroupConsumerImpl {
52 protected static final Logger logger = LoggerFactory.getLogger(GroupConsumerImpl.class);
53 private GroupEventListener groupEventListener = new GroupEventListener();
54 private Registration<NotificationListener> groupListener;
55 private SalGroupService groupService;
56 private GroupDataCommitHandler commitHandler;
58 private ConcurrentMap<GroupKey, Group> originalSwGroupView;
59 private ConcurrentMap<GroupKey, Group> installedSwGroupView;
61 private ConcurrentMap<Node, List<Group>> nodeGroups;
62 private ConcurrentMap<GroupKey, Group> inactiveGroups;
64 private IClusterContainerServices clusterGroupContainerService = null;
65 private IContainer container;
67 public GroupConsumerImpl() {
68 InstanceIdentifier<? extends DataObject> path = InstanceIdentifier.builder().node(Groups.class)
69 .node(Group.class).toInstance();
70 groupService = FRMConsumerImpl.getProviderSession().getRpcService(SalGroupService.class);
72 clusterGroupContainerService = FRMConsumerImpl.getClusterContainerService();
73 container = FRMConsumerImpl.getContainer();
75 if (!(cacheStartup())) {
76 logger.error("Unanle to allocate/retrieve group cache");
77 System.out.println("Unable to allocate/retrieve group cache");
80 if (null == groupService) {
81 logger.error("Consumer SAL Group Service is down or NULL. FRM may not function as intended");
82 System.out.println("Consumer SAL Group Service is down or NULL.");
87 groupListener = FRMConsumerImpl.getNotificationService().registerNotificationListener(groupEventListener);
89 if (null == groupListener) {
90 logger.error("Listener to listen on group data modifcation events");
91 System.out.println("Listener to listen on group data modifcation events.");
95 commitHandler = new GroupDataCommitHandler();
96 FRMConsumerImpl.getDataProviderService().registerCommitHandler(path, commitHandler);
99 private boolean allocateGroupCaches() {
100 if (this.clusterGroupContainerService == null) {
101 logger.warn("Group: Un-initialized clusterGroupContainerService, can't create cache");
106 clusterGroupContainerService.createCache("frm.originalSwGroupView",
107 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
109 clusterGroupContainerService.createCache("frm.installedSwGroupView",
110 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
112 clusterGroupContainerService.createCache("frm.inactiveGroups",
113 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
115 clusterGroupContainerService.createCache("frm.nodeGroups",
116 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
118 // TODO for cluster mode
120 * clusterGroupContainerService.createCache(WORK_STATUS_CACHE,
121 * EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL,
122 * IClusterServices.cacheMode.ASYNC));
124 * clusterGroupContainerService.createCache(WORK_ORDER_CACHE,
125 * EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL,
126 * IClusterServices.cacheMode.ASYNC));
129 } catch (CacheConfigException cce) {
130 logger.error("Group CacheConfigException");
133 } catch (CacheExistException cce) {
134 logger.error(" Group CacheExistException");
140 private void nonClusterGroupObjectCreate() {
141 originalSwGroupView = new ConcurrentHashMap<GroupKey, Group>();
142 installedSwGroupView = new ConcurrentHashMap<GroupKey, Group>();
143 nodeGroups = new ConcurrentHashMap<Node, List<Group>>();
144 inactiveGroups = new ConcurrentHashMap<GroupKey, Group>();
147 @SuppressWarnings({ "unchecked" })
148 private boolean retrieveGroupCaches() {
149 ConcurrentMap<?, ?> map;
151 if (this.clusterGroupContainerService == null) {
152 logger.warn("Group: un-initialized clusterGroupContainerService, can't retrieve cache");
153 nonClusterGroupObjectCreate();
157 map = clusterGroupContainerService.getCache("frm.originalSwGroupView");
159 originalSwGroupView = (ConcurrentMap<GroupKey, Group>) map;
161 logger.error("Retrieval of cache(originalSwGroupView) failed");
165 map = clusterGroupContainerService.getCache("frm.installedSwGroupView");
167 installedSwGroupView = (ConcurrentMap<GroupKey, Group>) map;
169 logger.error("Retrieval of cache(installedSwGroupView) failed");
173 map = clusterGroupContainerService.getCache("frm.inactiveGroups");
175 inactiveGroups = (ConcurrentMap<GroupKey, Group>) map;
177 logger.error("Retrieval of cache(inactiveGroups) failed");
181 map = clusterGroupContainerService.getCache("frm.nodeGroups");
183 nodeGroups = (ConcurrentMap<Node, List<Group>>) map;
185 logger.error("Retrieval of cache(nodeGroup) failed");
192 private boolean cacheStartup() {
193 if (allocateGroupCaches()) {
194 if (retrieveGroupCaches()) {
202 public Status validateGroup(Group group, FRMUtil.operation operation) {
203 String containerName;
205 Iterator<Bucket> bucketIterator;
206 boolean returnResult;
207 Buckets groupBuckets;
210 containerName = group.getContainerName();
212 if (null == containerName) {
213 containerName = GlobalConstants.DEFAULT.toString();
214 } else if (!FRMUtil.isNameValid(containerName)) {
215 logger.error("Container Name is invalid %s" + containerName);
216 return new Status(StatusCode.BADREQUEST, "Container Name is invalid");
219 groupName = group.getGroupName();
220 if (!FRMUtil.isNameValid(groupName)) {
221 logger.error("Group Name is invalid %s" + groupName);
222 return new Status(StatusCode.BADREQUEST, "Group Name is invalid");
225 returnResult = doesGroupEntryExists(group.getKey(), groupName, containerName);
227 if (FRMUtil.operation.ADD == operation && returnResult) {
228 logger.error("Record with same Group Name exists");
229 return new Status(StatusCode.BADREQUEST, "Group record exists");
230 } else if (!returnResult) {
231 logger.error("Group record does not exist");
232 return new Status(StatusCode.BADREQUEST, "Group record does not exist");
235 if (!(group.getGroupType().getIntValue() >= GroupType.GroupAll.getIntValue() && group.getGroupType()
236 .getIntValue() <= GroupType.GroupFf.getIntValue())) {
237 logger.error("Invalid Group type %d" + group.getGroupType().getIntValue());
238 return new Status(StatusCode.BADREQUEST, "Invalid Group type");
241 groupBuckets = group.getBuckets();
243 if (null != groupBuckets && null != groupBuckets.getBucket()) {
244 bucketIterator = groupBuckets.getBucket().iterator();
246 while (bucketIterator.hasNext()) {
247 if (!(FRMUtil.validateActions(bucketIterator.next().getAction()))) {
248 logger.error("Error in action bucket");
249 return new Status(StatusCode.BADREQUEST, "Invalid Group bucket contents");
255 return new Status(StatusCode.SUCCESS);
259 private boolean doesGroupEntryExists(GroupKey key, String groupName, String containerName) {
260 if (!originalSwGroupView.containsKey(key)) {
264 for (ConcurrentMap.Entry<GroupKey, Group> entry : originalSwGroupView.entrySet()) {
265 if (entry.getValue().getGroupName().equals(groupName)) {
266 if (entry.getValue().getContainerName().equals(containerName)) {
275 * Update Group entries to the southbound plugin/inventory and our internal
281 private Status updateGroup(InstanceIdentifier<?> path, Group groupUpdateDataObject) {
282 GroupKey groupKey = groupUpdateDataObject.getKey();
283 Status groupOperationStatus = validateGroup(groupUpdateDataObject, FRMUtil.operation.UPDATE);
285 if (!groupOperationStatus.isSuccess()) {
286 logger.error("Group data object validation failed %s" + groupUpdateDataObject.getGroupName());
287 return groupOperationStatus;
290 originalSwGroupView.remove(groupKey);
291 originalSwGroupView.put(groupKey, groupUpdateDataObject);
293 if (groupUpdateDataObject.isInstall()) {
294 UpdateGroupInputBuilder groupData = new UpdateGroupInputBuilder();
295 // TODO how to get original group and modified group.
297 if (installedSwGroupView.containsKey(groupKey)) {
298 installedSwGroupView.remove(groupKey);
301 installedSwGroupView.put(groupKey, groupUpdateDataObject);
302 groupService.updateGroup(groupData.build());
305 return groupOperationStatus;
309 * Adds Group to the southbound plugin and our internal database
314 private Status addGroup(InstanceIdentifier<?> path, Group groupAddDataObject) {
315 GroupKey groupKey = groupAddDataObject.getKey();
316 Status groupOperationStatus = validateGroup(groupAddDataObject, FRMUtil.operation.ADD);
318 if (!groupOperationStatus.isSuccess()) {
319 logger.error("Group data object validation failed %s" + groupAddDataObject.getGroupName());
320 return groupOperationStatus;
323 originalSwGroupView.put(groupKey, groupAddDataObject);
325 if (groupAddDataObject.isInstall()) {
326 AddGroupInputBuilder groupData = new AddGroupInputBuilder();
327 groupData.setBuckets(groupAddDataObject.getBuckets());
328 groupData.setContainerName(groupAddDataObject.getContainerName());
329 groupData.setGroupId(groupAddDataObject.getGroupId());
330 groupData.setGroupType(groupAddDataObject.getGroupType());
331 groupData.setNode(groupAddDataObject.getNode());
332 installedSwGroupView.put(groupKey, groupAddDataObject);
333 groupService.addGroup(groupData.build());
336 return groupOperationStatus;
339 private RpcResult<Void> commitToPlugin(internalTransaction transaction) {
340 for (Entry<InstanceIdentifier<?>, Group> entry : transaction.additions.entrySet()) {
342 if (!addGroup(entry.getKey(), entry.getValue()).isSuccess()) {
343 return Rpcs.getRpcResult(false, null, null);
346 for (@SuppressWarnings("unused")
347 Entry<InstanceIdentifier<?>, Group> entry : transaction.additions.entrySet()) {
349 if (!updateGroup(entry.getKey(), entry.getValue()).isSuccess()) {
350 return Rpcs.getRpcResult(false, null, null);
354 for (InstanceIdentifier<?> removal : transaction.removals) {
355 // removeFlow(removal);
358 return Rpcs.getRpcResult(true, null, null);
361 private final class GroupDataCommitHandler implements DataCommitHandler<InstanceIdentifier<?>, DataObject> {
363 @SuppressWarnings("unchecked")
365 public DataCommitTransaction<InstanceIdentifier<?>, DataObject> requestCommit(DataModification<InstanceIdentifier<?>, DataObject> modification) {
366 // We should verify transaction
367 System.out.println("Coming in GroupDatacommitHandler");
368 internalTransaction transaction = new internalTransaction(modification);
369 transaction.prepareUpdate();
374 private final class internalTransaction implements DataCommitTransaction<InstanceIdentifier<?>, DataObject> {
376 private final DataModification<InstanceIdentifier<?>, DataObject> modification;
379 public DataModification<InstanceIdentifier<?>, DataObject> getModification() {
383 public internalTransaction(DataModification<InstanceIdentifier<?>, DataObject> modification) {
384 this.modification = modification;
387 Map<InstanceIdentifier<?>, Group> additions = new HashMap<>();
388 Map<InstanceIdentifier<?>, Group> updates = new HashMap<>();
389 Set<InstanceIdentifier<?>> removals = new HashSet<>();
392 * We create a plan which flows will be added, which will be updated and
393 * which will be removed based on our internal state.
396 void prepareUpdate() {
398 Set<Entry<InstanceIdentifier<?>, DataObject>> puts = modification.getUpdatedConfigurationData().entrySet();
399 for (Entry<InstanceIdentifier<?>, DataObject> entry : puts) {
400 if (entry.getValue() instanceof Group) {
401 Group group = (Group) entry.getValue();
402 preparePutEntry(entry.getKey(), group);
407 removals = modification.getRemovedConfigurationData();
410 private void preparePutEntry(InstanceIdentifier<?> key, Group group) {
412 Group original = originalSwGroupView.get(key);
413 if (original != null) {
414 // It is update for us
416 updates.put(key, group);
418 // It is addition for us
420 additions.put(key, group);
425 * We are OK to go with execution of plan
429 public RpcResult<Void> finish() throws IllegalStateException {
431 RpcResult<Void> rpcStatus = commitToPlugin(this);
432 // We return true if internal transaction is successful.
433 // return Rpcs.getRpcResult(true, null, Collections.emptySet());
439 * We should rollback our preparation
443 public RpcResult<Void> rollback() throws IllegalStateException {
444 // NOOP - we did not modified any internal state during
445 // requestCommit phase
446 // return Rpcs.getRpcResult(true, null, Collections.emptySet());
447 return Rpcs.getRpcResult(true, null, null);
453 final class GroupEventListener implements SalGroupListener {
455 List<GroupAdded> addedGroups = new ArrayList<>();
456 List<GroupRemoved> removedGroups = new ArrayList<>();
457 List<GroupUpdated> updatedGroups = new ArrayList<>();
460 public void onGroupAdded(GroupAdded notification) {
461 System.out.println("added Group..........................");
462 addedGroups.add(notification);
466 public void onGroupRemoved(GroupRemoved notification) {
467 // TODO Auto-generated method stub
472 public void onGroupUpdated(GroupUpdated notification) {
473 // TODO Auto-generated method stub