Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / MeterFeaturesService.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.statistics.services;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.concurrent.atomic.AtomicLong;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
19 import org.opendaylight.openflowplugin.impl.services.util.RequestInputUtils;
20 import org.opendaylight.openflowplugin.impl.statistics.services.compatibility.AbstractCompatibleStatService;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdatedBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBand;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBandDrop;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBandDscpRemark;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBurst;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterCapability;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterKbps;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterPktps;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterStats;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterBandTypeBitmap;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterFeaturesCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterFeaturesCaseBuilder;
46
47 final class MeterFeaturesService
48         extends AbstractCompatibleStatService<GetMeterFeaturesInput, GetMeterFeaturesOutput, MeterFeaturesUpdated> {
49     private static final MultipartRequestMeterFeaturesCase METER_FEATURES_CASE =
50             new MultipartRequestMeterFeaturesCaseBuilder().build();
51
52     MeterFeaturesService(final RequestContextStack requestContextStack,
53                                 final DeviceContext deviceContext,
54                                 final AtomicLong compatibilityXidSeed) {
55         super(requestContextStack, deviceContext, compatibilityXidSeed);
56     }
57
58     @Override
59     protected OfHeader buildRequest(final Xid xid, final GetMeterFeaturesInput input) {
60         MultipartRequestInputBuilder mprInput =
61                 RequestInputUtils.createMultipartHeader(MultipartType.OFPMPMETERFEATURES, xid.getValue(), getVersion());
62         mprInput.setMultipartRequestBody(METER_FEATURES_CASE);
63         return mprInput.build();
64     }
65
66     @Override
67     public GetMeterFeaturesOutput buildTxCapableResult(final TransactionId emulatedTxId) {
68         return new GetMeterFeaturesOutputBuilder().setTransactionId(emulatedTxId).build();
69     }
70
71     @Override
72     public MeterFeaturesUpdated transformToNotification(final List<MultipartReply> result,
73             final TransactionId emulatedTxId) {
74         final int mpSize = result.size();
75         Preconditions.checkArgument(mpSize == 1, "unexpected (!=1) mp-reply size received: %s", mpSize);
76
77         MeterFeaturesUpdatedBuilder notification = new MeterFeaturesUpdatedBuilder();
78         notification.setId(getDeviceInfo().getNodeId());
79         notification.setMoreReplies(Boolean.FALSE);
80         notification.setTransactionId(emulatedTxId);
81
82         MultipartReplyMeterFeaturesCase caseBody =
83                 (MultipartReplyMeterFeaturesCase) result.get(0).getMultipartReplyBody();
84         MultipartReplyMeterFeatures replyBody = caseBody.getMultipartReplyMeterFeatures();
85         notification.setMaxBands(replyBody.getMaxBands());
86         notification.setMaxColor(replyBody.getMaxColor());
87         notification.setMaxMeter(new Counter32(replyBody.getMaxMeter()));
88         notification.setMeterCapabilitiesSupported(extractMeterCapabilities(replyBody.getCapabilities()));
89         notification.setMeterBandSupported(extractSupportedMeterBand(replyBody, replyBody.getBandTypes()));
90
91         return notification.build();
92     }
93
94     @VisibleForTesting
95     protected static Set<MeterBand> extractSupportedMeterBand(
96             final MultipartReplyMeterFeatures replyBody, final MeterBandTypeBitmap bandTypes) {
97         final var supportedMeterBand = ImmutableSet.<MeterBand>builder();
98         if (bandTypes.getOFPMBTDROP()) {
99             supportedMeterBand.add(MeterBandDrop.VALUE);
100         }
101         if (replyBody.getBandTypes().getOFPMBTDSCPREMARK()) {
102             supportedMeterBand.add(MeterBandDscpRemark.VALUE);
103         }
104         return supportedMeterBand.build();
105     }
106
107     @VisibleForTesting
108     protected static Set<MeterCapability> extractMeterCapabilities(final MeterFlags capabilities) {
109         final var supportedCapabilities = ImmutableSet.<MeterCapability>builder();
110         if (capabilities.getOFPMFBURST()) {
111             supportedCapabilities.add(MeterBurst.VALUE);
112         }
113         if (capabilities.getOFPMFKBPS()) {
114             supportedCapabilities.add(MeterKbps.VALUE);
115         }
116         if (capabilities.getOFPMFPKTPS()) {
117             supportedCapabilities.add(MeterPktps.VALUE);
118         }
119         if (capabilities.getOFPMFSTATS()) {
120             supportedCapabilities.add(MeterStats.VALUE);
121         }
122         return supportedCapabilities.build();
123     }
124 }