2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.openflowplugin.impl.datastore.multipart;
10 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
11 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
13 import org.opendaylight.yangtools.binding.DataContainer;
14 import org.opendaylight.yangtools.binding.DataObject;
15 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 public abstract class AbstractMultipartWriter<T extends DataContainer> {
21 private static final Logger LOG = LoggerFactory.getLogger(AbstractMultipartWriter.class);
23 private final TxFacade txFacade;
24 private final InstanceIdentifier<Node> instanceIdentifier;
26 AbstractMultipartWriter(final TxFacade txFacade, final InstanceIdentifier<Node> instanceIdentifier) {
27 this.txFacade = txFacade;
28 this.instanceIdentifier = instanceIdentifier;
32 * Creates put operation using provided data in underlying transaction chain.
36 * @param <O> data type
38 protected <O extends DataObject> void writeToTransaction(final InstanceIdentifier<O> path,
40 final boolean withParents) {
42 txFacade.writeToTransactionWithParentsSlow(LogicalDatastoreType.OPERATIONAL, path, data);
44 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, path, data);
49 * Get instance identifier.
51 * @return instance identifier
53 protected InstanceIdentifier<Node> getInstanceIdentifier() {
54 return instanceIdentifier;
58 * Write dataContainer.
60 * @param dataContainer dataContainer
61 * @param withParents write missing parents if needed (slower)
62 * @return true if we have correct dataContainer type
64 public boolean write(final DataContainer dataContainer, final boolean withParents) {
65 if (getType().isInstance(dataContainer)) {
66 LOG.debug("Writing multipart data of type {} for node {}", getType(), getInstanceIdentifier());
67 storeStatistics(getType().cast(dataContainer), withParents);
71 LOG.debug("Failed to write multipart data of type {} for node {}", getType(), getInstanceIdentifier());
78 * @return type of writer
80 protected abstract Class<T> getType();
85 * @param statistics statistics
86 * @param withParents write missing parents if needed (slower)
88 protected abstract void storeStatistics(T statistics, boolean withParents);