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 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;
24 public final class ITMBatchingUtils {
26 private static final Logger LOG = LoggerFactory.getLogger(ITMBatchingUtils.class);
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<>();
34 private static DataBroker dataBroker;
36 // This could extend in future
37 public enum EntityType {
43 private ITMBatchingUtils() {
46 public static DataBroker getBroker() {
50 public static void setBroker(DataBroker broker) {
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));
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));
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));
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));
87 public static BlockingQueue<ActionableResource> getQueue(EntityType entityType) {
89 case DEFAULT_OPERATIONAL:
90 return DEFAULT_OPERATIONAL_SHARD_BUFFER_Q;
92 return DEFAULT_CONFIG_SHARD_BUFFER_Q;
94 return TOPOLOGY_CONFIG_SHARD_BUFFER_Q;
96 throw new IllegalArgumentException(
97 "Entity type " + entityType + " is neither operational or config, getQueue operation failed");
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));