528601939c5d8623ef841673885549dd6e905e4f
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / datastore / multipart / FlowStatsMultipartWriter.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 java.util.Objects;
12 import org.opendaylight.openflowplugin.api.openflow.device.DeviceRegistry;
13 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
14 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
15 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
16 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowAndStatisticsMapList;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29
30 public class FlowStatsMultipartWriter extends AbstractMultipartWriter<FlowAndStatisticsMapList> {
31
32     private final DeviceRegistry registry;
33     private final short version;
34
35     public FlowStatsMultipartWriter(final TxFacade txFacade,
36                                     final InstanceIdentifier<Node> instanceIdentifier,
37                                     final DeviceRegistry registry,
38                                     final short version) {
39         super(txFacade, instanceIdentifier);
40         this.registry = registry;
41         this.version = version;
42     }
43
44     @Override
45     protected Class<FlowAndStatisticsMapList> getType() {
46         return FlowAndStatisticsMapList.class;
47     }
48
49     @Override
50     public void storeStatistics(final FlowAndStatisticsMapList statistics, final boolean withParents) {
51         statistics.getFlowAndStatisticsMapList()
52             .forEach(stat -> {
53                 final FlowBuilder flow = new FlowBuilder(stat)
54                     .addAugmentation(
55                         FlowStatisticsData.class,
56                         new FlowStatisticsDataBuilder()
57                             .setFlowStatistics(new FlowStatisticsBuilder(stat).build())
58                             .build());
59
60                 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(version, flow.build());
61                 registry.getDeviceFlowRegistry().store(flowRegistryKey);
62
63                 final FlowDescriptor flowDescriptor = registry
64                         .getDeviceFlowRegistry()
65                         .retrieveDescriptor(flowRegistryKey);
66
67                 if (Objects.nonNull(flowDescriptor)) {
68                     final FlowKey key = new FlowKey(flowDescriptor
69                             .getFlowId());
70
71                     writeToTransaction(
72                             getInstanceIdentifier()
73                                     .augmentation(FlowCapableNode.class)
74                                     .child(Table.class, new TableKey(stat.getTableId()))
75                                     .child(Flow.class, key),
76                             flow
77                                     .setId(key.getId())
78                                     .setKey(key)
79                                     .build(),
80                             withParents);
81                 }
82             });
83     }
84
85 }