Switch to MD-SAL APIs
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / datastore / multipart / AbstractMultipartWriter.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.datastore.multipart;
9
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.yang.binding.DataContainer;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public abstract class AbstractMultipartWriter<T extends DataContainer> {
20
21     private static final Logger LOG = LoggerFactory.getLogger(AbstractMultipartWriter.class);
22
23     private final TxFacade txFacade;
24     private final InstanceIdentifier<Node> instanceIdentifier;
25
26     AbstractMultipartWriter(final TxFacade txFacade, final InstanceIdentifier<Node> instanceIdentifier) {
27         this.txFacade = txFacade;
28         this.instanceIdentifier = instanceIdentifier;
29     }
30
31     /**
32      * Creates put operation using provided data in underlying transaction chain.
33      *
34      * @param path path
35      * @param data data
36      * @param <O> data type
37      */
38     protected <O extends DataObject> void writeToTransaction(final InstanceIdentifier<O> path,
39                                                              final O data,
40                                                              final boolean withParents) {
41         if (withParents) {
42             txFacade.writeToTransactionWithParentsSlow(LogicalDatastoreType.OPERATIONAL, path, data);
43         } else {
44             txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, path, data);
45         }
46     }
47
48     /**
49      * Get instance identifier.
50      *
51      * @return instance identifier
52      */
53     protected InstanceIdentifier<Node> getInstanceIdentifier() {
54         return instanceIdentifier;
55     }
56
57     /**
58      * Write dataContainer.
59      *
60      * @param dataContainer dataContainer
61      * @param withParents write missing parents if needed (slower)
62      * @return true if we have correct dataContainer type
63      */
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);
68             return true;
69         }
70
71         LOG.debug("Failed to write multipart data of type {} for node {}", getType(), getInstanceIdentifier());
72         return false;
73     }
74
75     /**
76      * Get type of writer.
77      *
78      * @return type of writer
79      */
80     protected abstract Class<T> getType();
81
82     /**
83      * Write statistics.
84      *
85      * @param statistics statistics
86      * @param withParents write missing parents if needed (slower)
87      */
88     protected abstract void storeStatistics(T statistics, boolean withParents);
89
90
91 }