Use ByteBuf.readRetainedSlice()
[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.impl.registry.flow.FlowRegistryKeyFactory;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowAndStatisticsMapList;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24
25 public class FlowStatsMultipartWriter extends AbstractMultipartWriter<FlowAndStatisticsMapList> {
26     private final DeviceRegistry registry;
27
28     public FlowStatsMultipartWriter(final TxFacade txFacade,
29                                     final InstanceIdentifier<Node> instanceIdentifier,
30                                     final DeviceRegistry registry) {
31         super(txFacade, instanceIdentifier);
32         this.registry = registry;
33     }
34
35     @Override
36     protected Class<FlowAndStatisticsMapList> getType() {
37         return FlowAndStatisticsMapList.class;
38     }
39
40     @Override
41     public void storeStatistics(final FlowAndStatisticsMapList statistics, final boolean withParents) {
42         statistics.nonnullFlowAndStatisticsMapList()
43             .forEach(stat -> {
44                 final var flowBuilder = new FlowBuilder(stat)
45                         .withKey(FlowRegistryKeyFactory.DUMMY_FLOW_KEY)
46                         .addAugmentation(new FlowStatisticsDataBuilder()
47                             .setFlowStatistics(new FlowStatisticsBuilder(stat).build())
48                             .build());
49
50                 final var flowRegistry = registry.getDeviceFlowRegistry();
51                 final var flowRegistryKey = flowRegistry.createKey(flowBuilder.build());
52                 flowRegistry.store(flowRegistryKey);
53
54                 final var flowDescriptor = flowRegistry.retrieveDescriptor(flowRegistryKey);
55                 if (flowDescriptor != null) {
56                     final FlowKey key = new FlowKey(flowDescriptor.getFlowId());
57
58                     writeToTransaction(
59                             getInstanceIdentifier()
60                                     .augmentation(FlowCapableNode.class)
61                                     .child(Table.class, new TableKey(stat.getTableId()))
62                                     .child(Flow.class, key),
63                             flowBuilder
64                                     .setId(key.getId())
65                                     .withKey(key)
66                                     .build(),
67                             withParents);
68                 }
69             });
70     }
71
72 }