2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.statistics.services;
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.util.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.rev130715.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;
46 final class MeterFeaturesService
47 extends AbstractCompatibleStatService<GetMeterFeaturesInput, GetMeterFeaturesOutput, MeterFeaturesUpdated> {
48 private static final MultipartRequestMeterFeaturesCase METER_FEATURES_CASE =
49 new MultipartRequestMeterFeaturesCaseBuilder().build();
51 MeterFeaturesService(RequestContextStack requestContextStack,
52 DeviceContext deviceContext,
53 AtomicLong compatibilityXidSeed) {
54 super(requestContextStack, deviceContext, compatibilityXidSeed);
58 protected OfHeader buildRequest(final Xid xid, final GetMeterFeaturesInput input) {
59 MultipartRequestInputBuilder mprInput =
60 RequestInputUtils.createMultipartHeader(MultipartType.OFPMPMETERFEATURES, xid.getValue(), getVersion());
61 mprInput.setMultipartRequestBody(METER_FEATURES_CASE);
62 return mprInput.build();
66 public GetMeterFeaturesOutput buildTxCapableResult(TransactionId emulatedTxId) {
67 return new GetMeterFeaturesOutputBuilder().setTransactionId(emulatedTxId).build();
71 public MeterFeaturesUpdated transformToNotification(List<MultipartReply> result, TransactionId emulatedTxId) {
72 final int mpSize = result.size();
73 Preconditions.checkArgument(mpSize == 1, "unexpected (!=1) mp-reply size received: {}", mpSize);
75 MeterFeaturesUpdatedBuilder notification = new MeterFeaturesUpdatedBuilder();
76 notification.setId(getDeviceInfo().getNodeId());
77 notification.setMoreReplies(Boolean.FALSE);
78 notification.setTransactionId(emulatedTxId);
80 MultipartReplyMeterFeaturesCase caseBody =
81 (MultipartReplyMeterFeaturesCase) result.get(0).getMultipartReplyBody();
82 MultipartReplyMeterFeatures replyBody = caseBody.getMultipartReplyMeterFeatures();
83 notification.setMaxBands(replyBody.getMaxBands());
84 notification.setMaxColor(replyBody.getMaxColor());
85 notification.setMaxMeter(new Counter32(replyBody.getMaxMeter()));
86 notification.setMeterCapabilitiesSupported(extractMeterCapabilities(replyBody.getCapabilities()));
87 notification.setMeterBandSupported(extractSupportedMeterBand(replyBody, replyBody.getBandTypes()));
89 return notification.build();
93 protected List<Class<? extends MeterBand>> extractSupportedMeterBand(MultipartReplyMeterFeatures replyBody,
94 MeterBandTypeBitmap bandTypes) {
95 List<Class<? extends MeterBand>> supportedMeterBand = new ArrayList<>();
96 if (bandTypes.isOFPMBTDROP()) {
97 supportedMeterBand.add(MeterBandDrop.class);
99 if (replyBody.getBandTypes().isOFPMBTDSCPREMARK()) {
100 supportedMeterBand.add(MeterBandDscpRemark.class);
102 return supportedMeterBand;
106 protected static List<Class<? extends MeterCapability>> extractMeterCapabilities(MeterFlags capabilities) {
107 List<Class<? extends MeterCapability>> supportedCapabilities = new ArrayList<>();
109 if (capabilities.isOFPMFBURST()) {
110 supportedCapabilities.add(MeterBurst.class);
112 if (capabilities.isOFPMFKBPS()) {
113 supportedCapabilities.add(MeterKbps.class);
115 if (capabilities.isOFPMFPKTPS()) {
116 supportedCapabilities.add(MeterPktps.class);
118 if (capabilities.isOFPMFSTATS()) {
119 supportedCapabilities.add(MeterStats.class);
121 return supportedCapabilities;