Add single layer deserialization support
[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.openflowplugin.impl.services.AbstractMultipartService;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
15 import org.opendaylight.yangtools.yang.binding.DataContainer;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public abstract class AbstractMultipartWriter<T extends DataContainer> {
22
23     private static final Logger LOG = LoggerFactory.getLogger(AbstractMultipartService.class);
24
25     private final TxFacade txFacade;
26     private final InstanceIdentifier<Node> instanceIdentifier;
27
28     AbstractMultipartWriter(final TxFacade txFacade, final InstanceIdentifier<Node> instanceIdentifier) {
29         this.txFacade = txFacade;
30         this.instanceIdentifier = instanceIdentifier;
31     }
32
33     /**
34      * Creates put operation using provided data in underlying transaction chain.
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      * @return instance identifier
52      */
53     protected InstanceIdentifier<Node> getInstanceIdentifier() {
54         return instanceIdentifier;
55     }
56
57     /**
58      * Write dataContainer
59      * @param dataContainer dataContainer
60      * @param withParents write missing parents if needed (slower)
61      * @return true if we have correct dataContainer type
62      */
63     public boolean write(final DataContainer dataContainer, final boolean withParents) {
64         if (getType().isInstance(dataContainer)) {
65             LOG.debug("Writing multipart data of type {} for node {}", getType(), getInstanceIdentifier());
66             storeStatistics(getType().cast(dataContainer), withParents);
67             return true;
68         }
69
70         LOG.debug("Failed to write multipart data of type {} for node {}", getType(), getInstanceIdentifier());
71         return false;
72     }
73
74     /**
75      * Get type of writer
76      * @return type of writer
77      */
78     protected abstract Class<T> getType();
79
80     /**
81      * Write statistics
82      * @param statistics statistics
83      * @param withParents write missing parents if needed (slower)
84      */
85     protected abstract void storeStatistics(final T statistics,
86                                             final boolean withParents);
87
88
89 }