Add single layer deserialization support
[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.util.RequestInputUtils;
19 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
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 = new MultipartRequestMeterFeaturesCaseBuilder().build();
50
51     public MeterFeaturesService(RequestContextStack requestContextStack, DeviceContext deviceContext, AtomicLong compatibilityXidSeed) {
52         super(requestContextStack, deviceContext, compatibilityXidSeed);
53     }
54
55     @Override
56     protected OfHeader buildRequest(final Xid xid, final GetMeterFeaturesInput input) throws ServiceException {
57         MultipartRequestInputBuilder mprInput =
58                 RequestInputUtils.createMultipartHeader(MultipartType.OFPMPMETERFEATURES, xid.getValue(), getVersion());
59         mprInput.setMultipartRequestBody(METER_FEATURES_CASE);
60         return mprInput.build();
61     }
62
63     @Override
64     public GetMeterFeaturesOutput buildTxCapableResult(TransactionId emulatedTxId) {
65         return new GetMeterFeaturesOutputBuilder().setTransactionId(emulatedTxId).build();
66     }
67
68     @Override
69     public MeterFeaturesUpdated transformToNotification(List<MultipartReply> result, TransactionId emulatedTxId) {
70         final int mpSize = result.size();
71         Preconditions.checkArgument(mpSize == 1, "unexpected (!=1) mp-reply size received: {}", mpSize);
72
73         MeterFeaturesUpdatedBuilder notification = new MeterFeaturesUpdatedBuilder();
74         notification.setId(getDeviceInfo().getNodeId());
75         notification.setMoreReplies(Boolean.FALSE);
76         notification.setTransactionId(emulatedTxId);
77
78         MultipartReplyMeterFeaturesCase caseBody = (MultipartReplyMeterFeaturesCase) result.get(0).getMultipartReplyBody();
79         MultipartReplyMeterFeatures replyBody = caseBody.getMultipartReplyMeterFeatures();
80         notification.setMaxBands(replyBody.getMaxBands());
81         notification.setMaxColor(replyBody.getMaxColor());
82         notification.setMaxMeter(new Counter32(replyBody.getMaxMeter()));
83         notification.setMeterCapabilitiesSupported(extractMeterCapabilities(replyBody.getCapabilities()));
84         notification.setMeterBandSupported(extractSupportedMeterBand(replyBody, replyBody.getBandTypes()));
85
86         return notification.build();
87     }
88
89     @VisibleForTesting
90     protected List<Class<? extends MeterBand>> extractSupportedMeterBand(MultipartReplyMeterFeatures replyBody, MeterBandTypeBitmap bandTypes) {
91         List<Class<? extends MeterBand>> supportedMeterBand = new ArrayList<>();
92         if (bandTypes.isOFPMBTDROP()) {
93             supportedMeterBand.add(MeterBandDrop.class);
94         }
95         if (replyBody.getBandTypes().isOFPMBTDSCPREMARK()) {
96             supportedMeterBand.add(MeterBandDscpRemark.class);
97         }
98         return supportedMeterBand;
99     }
100
101     @VisibleForTesting
102     protected static List<Class<? extends MeterCapability>> extractMeterCapabilities(MeterFlags capabilities) {
103         List<Class<? extends MeterCapability>> supportedCapabilities = new ArrayList<>();
104
105         if (capabilities.isOFPMFBURST()) {
106             supportedCapabilities.add(MeterBurst.class);
107         }
108         if (capabilities.isOFPMFKBPS()) {
109             supportedCapabilities.add(MeterKbps.class);
110         }
111         if (capabilities.isOFPMFPKTPS()) {
112             supportedCapabilities.add(MeterPktps.class);
113         }
114         if (capabilities.isOFPMFSTATS()) {
115             supportedCapabilities.add(MeterStats.class);
116         }
117         return supportedCapabilities;
118     }
119 }