6173ddc53247ffe0e4269a0b4715446c0fa76847
[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
9 package org.opendaylight.openflowplugin.impl.datastore.multipart;
10
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
14 import org.opendaylight.yangtools.yang.binding.DataContainer;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public abstract class AbstractMultipartWriter<T extends DataContainer> {
21
22     private static final Logger LOG = LoggerFactory.getLogger(AbstractMultipartWriter.class);
23
24     private final TxFacade txFacade;
25     private final InstanceIdentifier<Node> instanceIdentifier;
26
27     AbstractMultipartWriter(final TxFacade txFacade, final InstanceIdentifier<Node> instanceIdentifier) {
28         this.txFacade = txFacade;
29         this.instanceIdentifier = instanceIdentifier;
30     }
31
32     /**
33      * Creates put operation using provided data in underlying transaction chain.
34      *
35      * @param path path
36      * @param data data
37      * @param <O> data type
38      */
39     protected <O extends DataObject> void writeToTransaction(final InstanceIdentifier<O> path,
40                                                              final O data,
41                                                              final boolean withParents) {
42         if (withParents) {
43             txFacade.writeToTransactionWithParentsSlow(LogicalDatastoreType.OPERATIONAL, path, data);
44         } else {
45             txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, path, data);
46         }
47     }
48
49     /**
50      * Get instance identifier.
51      *
52      * @return instance identifier
53      */
54     protected InstanceIdentifier<Node> getInstanceIdentifier() {
55         return instanceIdentifier;
56     }
57
58     /**
59      * Write dataContainer.
60      *
61      * @param dataContainer dataContainer
62      * @param withParents write missing parents if needed (slower)
63      * @return true if we have correct dataContainer type
64      */
65     public boolean write(final DataContainer dataContainer, final boolean withParents) {
66         if (getType().isInstance(dataContainer)) {
67             LOG.debug("Writing multipart data of type {} for node {}", getType(), getInstanceIdentifier());
68             storeStatistics(getType().cast(dataContainer), withParents);
69             return true;
70         }
71
72         LOG.debug("Failed to write multipart data of type {} for node {}", getType(), getInstanceIdentifier());
73         return false;
74     }
75
76     /**
77      * Get type of writer.
78      *
79      * @return type of writer
80      */
81     protected abstract Class<T> getType();
82
83     /**
84      * Write statistics.
85      *
86      * @param statistics statistics
87      * @param withParents write missing parents if needed (slower)
88      */
89     protected abstract void storeStatistics(T statistics, boolean withParents);
90
91
92 }