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