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