2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.genius.itm.impl;
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.genius.utils.batching.ActionableResource;
13 import org.opendaylight.genius.utils.batching.ActionableResourceImpl;
14 import org.opendaylight.genius.utils.batching.DefaultBatchHandler;
15 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import java.util.concurrent.BlockingQueue;
22 import java.util.concurrent.LinkedBlockingQueue;
24 public class ITMBatchingUtils {
25 private static final Logger LOG = LoggerFactory.getLogger((Class)ITMBatchingUtils.class);
26 public static final int BATCH_SIZE = 1000;
27 public static final int PERIODICITY = 500;
28 public static Integer batchSize;
29 public static Integer batchInterval;
30 private static DataBroker dataBroker;
31 private static BlockingQueue<ActionableResource> defaultOperationalShardBufferQ;
32 private static BlockingQueue<ActionableResource> defaultConfigShardBufferQ;
34 // This could extend in future
35 public enum EntityType {
40 public static DataBroker getBroker() {
44 public static void setBroker(DataBroker broker) {
48 public static void registerWithBatchManager(DataBroker dataBroker) {
49 ITMBatchingUtils.setBroker(dataBroker);
51 if (Integer.getInteger("batch.size") != null) {
52 batchSize = Integer.getInteger("batch.size");
55 if (Integer.getInteger("batch.wait.time") != null) {
56 batchInterval = Integer.getInteger("batch.wait.time");
58 ResourceBatchingManager resBatchingManager = ResourceBatchingManager.getInstance();
59 resBatchingManager.registerBatchableResource("ITM-DEFAULT-OPERATIONAL", defaultOperationalShardBufferQ, new DefaultBatchHandler(dataBroker, LogicalDatastoreType.OPERATIONAL, batchSize, batchInterval));
60 resBatchingManager.registerBatchableResource("ITM-DEFAULT-CONFIG", defaultConfigShardBufferQ, new DefaultBatchHandler(dataBroker, LogicalDatastoreType.CONFIGURATION, batchSize, batchInterval));
64 public static <T extends DataObject> void update(InstanceIdentifier<T> path, T data, EntityType entityType) {
65 ActionableResourceImpl actResource = new ActionableResourceImpl(path.toString());
66 actResource.setAction(ActionableResource.UPDATE);
67 actResource.setInstanceIdentifier(path);
68 actResource.setInstance(data);
69 LOG.debug("Adding to the Queue to batch the update DS Operation - Id {} data {}", path, data);
70 getQueue(entityType).add(actResource);
73 public static <T extends DataObject> void write(InstanceIdentifier<T> path, T data, EntityType entityType) {
74 ActionableResourceImpl actResource = new ActionableResourceImpl(path.toString());
75 actResource.setAction(ActionableResource.CREATE);
76 actResource.setInstanceIdentifier(path);
77 actResource.setInstance(data);
78 LOG.debug("Adding to the Queue to batch the write DS Operation - Id {} data {}", path, data);
79 getQueue(entityType).add(actResource);
82 public static BlockingQueue<ActionableResource> getQueue(EntityType entityType){
84 case DEFAULT_OPERATIONAL:return defaultOperationalShardBufferQ;
85 case DEFAULT_CONFIG: return defaultConfigShardBufferQ;
90 public static <T extends DataObject> void delete(InstanceIdentifier<T> path, EntityType entityType) {
91 ActionableResourceImpl actResource = new ActionableResourceImpl(path.toString());
92 actResource.setAction(ActionableResource.DELETE);
93 actResource.setInstanceIdentifier(path);
94 actResource.setInstance(null);
95 LOG.debug("Adding to the Queue to batch the delete DS Operation - Id {}", path);
96 getQueue(entityType).add(actResource);
100 defaultOperationalShardBufferQ = new LinkedBlockingQueue<>();
101 defaultConfigShardBufferQ = new LinkedBlockingQueue<>();