MDSAL-API Migration
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / impl / ITMBatchingUtils.java
1 /*
2  * Copyright (c) 2017, 2018 Ericsson India Global Services Pvt Ltd. 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.genius.itm.impl;
9
10 import java.util.concurrent.BlockingQueue;
11 import java.util.concurrent.LinkedBlockingQueue;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.genius.utils.batching.ActionableResource;
14 import org.opendaylight.genius.utils.batching.ActionableResources;
15 import org.opendaylight.genius.utils.batching.DefaultBatchHandler;
16 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public final class ITMBatchingUtils {
25
26     private static final Logger LOG = LoggerFactory.getLogger(ITMBatchingUtils.class);
27
28
29     private static final BlockingQueue<ActionableResource> DEFAULT_OPERATIONAL_SHARD_BUFFER_Q
30             = new LinkedBlockingQueue<>();
31     private static final BlockingQueue<ActionableResource> DEFAULT_CONFIG_SHARD_BUFFER_Q = new LinkedBlockingQueue<>();
32     private static final BlockingQueue<ActionableResource> TOPOLOGY_CONFIG_SHARD_BUFFER_Q = new LinkedBlockingQueue<>();
33
34     private static DataBroker dataBroker;
35
36     // This could extend in future
37     public enum EntityType  {
38         DEFAULT_OPERATIONAL,
39         DEFAULT_CONFIG,
40         TOPOLOGY_CONFIG
41     }
42
43     private ITMBatchingUtils() {
44     }
45
46     public static DataBroker getBroker() {
47         return dataBroker;
48     }
49
50     public static void setBroker(DataBroker broker) {
51         dataBroker = broker;
52     }
53
54     public static void registerWithBatchManager(DataBroker broker,Integer batchSize,Integer batchInterval) {
55         ITMBatchingUtils.setBroker(broker);
56         ResourceBatchingManager resBatchingManager = ResourceBatchingManager.getInstance();
57         resBatchingManager.registerBatchableResource("ITM-DEFAULT-OPERATIONAL", DEFAULT_OPERATIONAL_SHARD_BUFFER_Q,
58                                                      new DefaultBatchHandler(broker, LogicalDatastoreType.OPERATIONAL,
59                                                                              batchSize, batchInterval));
60         resBatchingManager.registerBatchableResource("ITM-DEFAULT-CONFIG", DEFAULT_CONFIG_SHARD_BUFFER_Q,
61                                                      new DefaultBatchHandler(broker, LogicalDatastoreType.CONFIGURATION,
62                                                                              batchSize, batchInterval));
63         resBatchingManager.registerBatchableResource("ITM-TOPOLOGY-CONFIG", TOPOLOGY_CONFIG_SHARD_BUFFER_Q,
64                                                      new DefaultBatchHandler(broker, LogicalDatastoreType.CONFIGURATION,
65                                                                              batchSize, batchInterval));
66     }
67
68
69     public static <T extends DataObject> void update(InstanceIdentifier<T> path, T data, EntityType entityType) {
70         LOG.debug("Adding to the Queue to batch the update DS Operation - Id {} data {}", path, data);
71         getQueue(entityType).add(ActionableResources.update(path, data));
72     }
73
74     public static <T extends DataObject> void updateContainer(InstanceIdentifier<T> path, T data,
75                                                               EntityType entityType) {
76         LOG.debug("Adding to the Queue to batch the update DS Operation - Id {} data {}", path, data);
77         getQueue(entityType).add(ActionableResources.updateContainer(path, data));
78     }
79
80
81     public static <T extends DataObject> void write(InstanceIdentifier<T> path, T data, EntityType entityType) {
82         LOG.debug("Adding to the Queue to batch the write DS Operation - Id {} data {}", path, data);
83         getQueue(entityType).add(ActionableResources.create(path, data));
84     }
85
86     @NonNull
87     public static BlockingQueue<ActionableResource> getQueue(EntityType entityType) {
88         switch (entityType) {
89             case DEFAULT_OPERATIONAL:
90                 return DEFAULT_OPERATIONAL_SHARD_BUFFER_Q;
91             case DEFAULT_CONFIG:
92                 return DEFAULT_CONFIG_SHARD_BUFFER_Q;
93             case TOPOLOGY_CONFIG:
94                 return TOPOLOGY_CONFIG_SHARD_BUFFER_Q;
95             default:
96                 throw new IllegalArgumentException(
97                     "Entity type " + entityType + " is neither operational or config, getQueue operation failed");
98         }
99     }
100
101     public static <T extends DataObject> void delete(InstanceIdentifier<T> path, EntityType entityType) {
102         LOG.debug("Adding to the Queue to batch the delete DS Operation - Id {}", path);
103         getQueue(entityType).add(ActionableResources.delete(path));
104     }
105 }