Handle nullable lists
[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 javax.annotation.Nonnull;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.genius.utils.batching.ActionableResource;
16 import org.opendaylight.genius.utils.batching.ActionableResourceImpl;
17 import org.opendaylight.genius.utils.batching.DefaultBatchHandler;
18 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
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     private static final int DEF_BATCH_SIZE = 1000;
29     private static final int DEF_PERIODICITY = 500;
30
31     private static final BlockingQueue<ActionableResource> DEFAULT_OPERATIONAL_SHARD_BUFFER_Q
32             = new LinkedBlockingQueue<>();
33     private static final BlockingQueue<ActionableResource> DEFAULT_CONFIG_SHARD_BUFFER_Q = new LinkedBlockingQueue<>();
34     private static final BlockingQueue<ActionableResource> TOPOLOGY_CONFIG_SHARD_BUFFER_Q = new LinkedBlockingQueue<>();
35
36     private static DataBroker dataBroker;
37
38     // This could extend in future
39     public enum EntityType  {
40         DEFAULT_OPERATIONAL,
41         DEFAULT_CONFIG,
42         TOPOLOGY_CONFIG
43     }
44
45     private ITMBatchingUtils() {
46     }
47
48     public static DataBroker getBroker() {
49         return dataBroker;
50     }
51
52     public static void setBroker(DataBroker broker) {
53         dataBroker = broker;
54     }
55
56     public static void registerWithBatchManager(DataBroker broker) {
57         ITMBatchingUtils.setBroker(broker);
58         Integer batchSize = Integer.getInteger("batch.size", DEF_BATCH_SIZE);
59         Integer batchInterval = Integer.getInteger("batch.wait.time", DEF_PERIODICITY);
60         ResourceBatchingManager resBatchingManager = ResourceBatchingManager.getInstance();
61         resBatchingManager.registerBatchableResource("ITM-DEFAULT-OPERATIONAL", DEFAULT_OPERATIONAL_SHARD_BUFFER_Q,
62                                                      new DefaultBatchHandler(broker, LogicalDatastoreType.OPERATIONAL,
63                                                                              batchSize, batchInterval));
64         resBatchingManager.registerBatchableResource("ITM-DEFAULT-CONFIG", DEFAULT_CONFIG_SHARD_BUFFER_Q,
65                                                      new DefaultBatchHandler(broker, LogicalDatastoreType.CONFIGURATION,
66                                                                              batchSize, batchInterval));
67         resBatchingManager.registerBatchableResource("ITM-TOPOLOGY-CONFIG", TOPOLOGY_CONFIG_SHARD_BUFFER_Q,
68                                                      new DefaultBatchHandler(broker, LogicalDatastoreType.CONFIGURATION,
69                                                                              batchSize, batchInterval));
70     }
71
72
73     public static <T extends DataObject> void update(InstanceIdentifier<T> path, T data, EntityType entityType) {
74         ActionableResourceImpl actResource = new ActionableResourceImpl(path.toString());
75         actResource.setAction(ActionableResource.UPDATE);
76         actResource.setInstanceIdentifier(path);
77         actResource.setInstance(data);
78         LOG.debug("Adding to the Queue to batch the update DS Operation - Id {} data {}", path, data);
79         getQueue(entityType).add(actResource);
80     }
81
82     public static <T extends DataObject> void write(InstanceIdentifier<T> path, T data, EntityType entityType) {
83         ActionableResourceImpl actResource = new ActionableResourceImpl(path.toString());
84         actResource.setAction(ActionableResource.CREATE);
85         actResource.setInstanceIdentifier(path);
86         actResource.setInstance(data);
87         LOG.debug("Adding to the Queue to batch the write DS Operation - Id {} data {}", path, data);
88         getQueue(entityType).add(actResource);
89     }
90
91     @Nonnull
92     public static BlockingQueue<ActionableResource> getQueue(EntityType entityType) {
93         switch (entityType) {
94             case DEFAULT_OPERATIONAL : return DEFAULT_OPERATIONAL_SHARD_BUFFER_Q;
95             case DEFAULT_CONFIG : return DEFAULT_CONFIG_SHARD_BUFFER_Q;
96             case TOPOLOGY_CONFIG: return TOPOLOGY_CONFIG_SHARD_BUFFER_Q;
97             default:
98                 throw new IllegalArgumentException(
99                     "Entity type " + entityType + " is neither operational or config, getQueue operation failed");
100         }
101     }
102
103     public static <T extends DataObject> void delete(InstanceIdentifier<T> path, EntityType entityType) {
104         ActionableResourceImpl actResource = new ActionableResourceImpl(path.toString());
105         actResource.setAction(ActionableResource.DELETE);
106         actResource.setInstanceIdentifier(path);
107         actResource.setInstance(null);
108         LOG.debug("Adding to the Queue to batch the delete DS Operation - Id {}", path);
109         getQueue(entityType).add(actResource);
110     }
111 }