2 * Copyright (c) 2016, 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.interfacemanager.renderer.ovs.utilities;
10 import java.util.concurrent.BlockingQueue;
11 import java.util.concurrent.LinkedBlockingQueue;
12 import javax.annotation.PostConstruct;
13 import javax.annotation.PreDestroy;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.apache.aries.blueprint.annotation.service.Reference;
17 import org.opendaylight.genius.utils.batching.ActionableResource;
18 import org.opendaylight.genius.utils.batching.ActionableResources;
19 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 public class BatchingUtils implements AutoCloseable {
27 public enum EntityType {
33 private static final String DEFAULT_OPERATIONAL_RES_TYPE = "INTERFACEMGR-DEFAULT-OPERATIONAL";
34 private static final String DEFAULT_CONFIG_RES_TYPE = "INTERFACEMGR-DEFAULT-CONFIG";
35 private static final String TOPOLOGY_CONFIG_RES_TYPE = "INTERFACEMGR-TOPOLOGY-CONFIG";
37 private static final int DEFAULT_BATCH_SIZE = 1000;
38 private static final int DEFAULT_BATCH_INTERVAL = 500;
40 private final BlockingQueue<ActionableResource<?>> topologyConfigShardBufferQ = new LinkedBlockingQueue<>();
41 private final BlockingQueue<ActionableResource<?>> defaultConfigShardBufferQ = new LinkedBlockingQueue<>();
42 private final BlockingQueue<ActionableResource<?>> defaultOperationalShardBufferQ = new LinkedBlockingQueue<>();
44 private final DataBroker dataBroker;
45 private final ResourceBatchingManager resourceBatchingManager = ResourceBatchingManager.getInstance();
48 public BatchingUtils(@Reference DataBroker dataBroker) {
49 this.dataBroker = dataBroker;
54 int batchSize = Integer.getInteger("batch.size", DEFAULT_BATCH_SIZE);
55 int batchInterval = Integer.getInteger("batch.wait.time", DEFAULT_BATCH_INTERVAL);
57 resourceBatchingManager.registerBatchableResource(TOPOLOGY_CONFIG_RES_TYPE, topologyConfigShardBufferQ,
58 new InterfaceBatchHandler(dataBroker, LogicalDatastoreType.CONFIGURATION, batchSize, batchInterval));
59 resourceBatchingManager.registerBatchableResource(DEFAULT_CONFIG_RES_TYPE, defaultConfigShardBufferQ,
60 new InterfaceBatchHandler(dataBroker, LogicalDatastoreType.CONFIGURATION, batchSize, batchInterval));
61 resourceBatchingManager.registerBatchableResource(DEFAULT_OPERATIONAL_RES_TYPE, defaultOperationalShardBufferQ,
62 new InterfaceBatchHandler(dataBroker, LogicalDatastoreType.OPERATIONAL, batchSize, batchInterval));
68 resourceBatchingManager.deregisterBatchableResource(TOPOLOGY_CONFIG_RES_TYPE);
69 resourceBatchingManager.deregisterBatchableResource(DEFAULT_CONFIG_RES_TYPE);
70 resourceBatchingManager.deregisterBatchableResource(DEFAULT_OPERATIONAL_RES_TYPE);
73 <T extends DataObject> void update(InstanceIdentifier<T> path, T data, EntityType entityType) {
74 getQueue(entityType).add(ActionableResources.update(path, data));
77 public <T extends DataObject> void write(InstanceIdentifier<T> path, T data, EntityType entityType) {
78 getQueue(entityType).add(ActionableResources.create(path, data));
81 public BlockingQueue<ActionableResource<?>> getQueue(EntityType entityType) {
84 return defaultConfigShardBufferQ;
86 return topologyConfigShardBufferQ;
87 case DEFAULT_OPERATIONAL:
88 return defaultOperationalShardBufferQ;
94 public <T extends DataObject> void delete(InstanceIdentifier<T> path, EntityType entityType) {
95 getQueue(entityType).add(ActionableResources.delete(path));