Fix issues related to checkstyle compliance in module mdsalutil-impl
[genius.git] / mdsalutil / mdsalutil-impl / src / main / java / org / opendaylight / genius / mdsalutil / internal / FlowBatchingUtils.java
1 /*
2  * Copyright (c) 2016, 2017 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.mdsalutil.internal;
9
10 import java.util.concurrent.BlockingQueue;
11 import java.util.concurrent.LinkedBlockingQueue;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.genius.utils.batching.ActionableResource;
14 import org.opendaylight.genius.utils.batching.ActionableResourceImpl;
15 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
16 import org.opendaylight.genius.utils.batching.ResourceHandler;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class FlowBatchingUtils {
23     private static final Logger LOG = LoggerFactory.getLogger(FlowBatchingUtils.class);
24     public static final int BATCH_SIZE = 1000;
25     public static final int PERIODICITY = 500;
26     public static Integer batchSize;
27     public static Integer batchInterval;
28     private static DataBroker dataBroker;
29     private static BlockingQueue<ActionableResource> inventoryConfigShardBufferQ;
30
31     public static DataBroker getBroker() {
32         return dataBroker;
33     }
34
35     public static void setBroker(DataBroker broker) {
36         dataBroker = broker;
37     }
38
39     public static void registerWithBatchManager(ResourceHandler resourceHandler, DataBroker dataBroker) {
40         FlowBatchingUtils.setBroker(dataBroker);
41         batchSize = 1000;
42         if (Integer.getInteger("batch.size") != null) {
43             batchSize = Integer.getInteger("batch.size");
44         }
45         batchInterval = 500;
46         if (Integer.getInteger("batch.wait.time") != null) {
47             batchInterval = Integer.getInteger("batch.wait.time");
48         }
49         ResourceBatchingManager resBatchingManager = ResourceBatchingManager.getInstance();
50         resBatchingManager.registerBatchableResource("MDSALUTIL-INVENTORY-CONFIG", inventoryConfigShardBufferQ,
51                 resourceHandler);
52     }
53
54     static <T extends DataObject> void update(InstanceIdentifier<T> path, T data) {
55         ActionableResourceImpl actResource = new ActionableResourceImpl(path.toString());
56         actResource.setAction(ActionableResource.UPDATE);
57         actResource.setInstanceIdentifier(path);
58         actResource.setInstance(data);
59         inventoryConfigShardBufferQ.add(actResource);
60     }
61
62     public static <T extends DataObject> void write(InstanceIdentifier<T> path, T data) {
63         ActionableResourceImpl actResource = new ActionableResourceImpl(path.toString());
64         actResource.setAction(ActionableResource.CREATE);
65         actResource.setInstanceIdentifier(path);
66         actResource.setInstance(data);
67         inventoryConfigShardBufferQ.add(actResource);
68     }
69
70     static <T extends DataObject> void delete(InstanceIdentifier<T> path) {
71         ActionableResourceImpl actResource = new ActionableResourceImpl(path.toString());
72         actResource.setAction(ActionableResource.DELETE);
73         actResource.setInstanceIdentifier(path);
74         actResource.setInstance(null);
75         inventoryConfigShardBufferQ.add(actResource);
76     }
77
78     static {
79         inventoryConfigShardBufferQ = new LinkedBlockingQueue<>();
80     }
81 }