2 * Copyright (c) 2017, 2018 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 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;
24 public final class ITMBatchingUtils {
26 private static final Logger LOG = LoggerFactory.getLogger(ITMBatchingUtils.class);
28 private static final int DEF_BATCH_SIZE = 1000;
29 private static final int DEF_PERIODICITY = 500;
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<>();
36 private static DataBroker dataBroker;
38 // This could extend in future
39 public enum EntityType {
45 private ITMBatchingUtils() {
48 public static DataBroker getBroker() {
52 public static void setBroker(DataBroker broker) {
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));
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);
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);
92 public static BlockingQueue<ActionableResource> getQueue(EntityType 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;
98 throw new IllegalArgumentException(
99 "Entity type " + entityType + " is neither operational or config, getQueue operation failed");
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);