Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.provider.of.meter.util; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.meter.Band; |
| 21 | import org.onosproject.net.meter.DefaultMeterFeatures; |
| 22 | import org.onosproject.net.meter.Meter; |
| 23 | import org.onosproject.net.meter.MeterFeatures; |
| 24 | import org.projectfloodlight.openflow.protocol.OFMeterFeatures; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | import static org.onosproject.net.meter.Band.Type.DROP; |
| 32 | import static org.onosproject.net.meter.Band.Type.REMARK; |
| 33 | import static org.onosproject.net.meter.Meter.Unit.KB_PER_SEC; |
| 34 | import static org.onosproject.net.meter.Meter.Unit.PKTS_PER_SEC; |
| 35 | import static org.projectfloodlight.openflow.protocol.ver13.OFMeterBandTypeSerializerVer13.DROP_VAL; |
| 36 | import static org.projectfloodlight.openflow.protocol.ver13.OFMeterBandTypeSerializerVer13.DSCP_REMARK_VAL; |
| 37 | import static org.projectfloodlight.openflow.protocol.ver13.OFMeterFlagsSerializerVer13.*; |
| 38 | |
| 39 | /** |
| 40 | * OpenFlow builder of MeterFeatures. |
| 41 | */ |
| 42 | public class MeterFeaturesBuilder { |
| 43 | private static final Logger log = LoggerFactory.getLogger(MeterFeaturesBuilder.class); |
| 44 | |
| 45 | private final OFMeterFeatures ofMeterFeatures; |
| 46 | private DeviceId deviceId; |
| 47 | |
| 48 | public MeterFeaturesBuilder(OFMeterFeatures features, DeviceId deviceId) { |
| 49 | this.ofMeterFeatures = checkNotNull(features); |
| 50 | this.deviceId = deviceId; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * To build a MeterFeatures using the openflow object |
| 55 | * provided by the southbound. |
| 56 | * |
| 57 | * @return the meter features object |
| 58 | */ |
| 59 | public MeterFeatures build() { |
| 60 | /* |
| 61 | * We set the basic values before to extract the other information. |
| 62 | */ |
| 63 | MeterFeatures.Builder builder = DefaultMeterFeatures.builder() |
| 64 | .forDevice(deviceId) |
| 65 | .withMaxBands(ofMeterFeatures.getMaxBands()) |
| 66 | .withMaxColors(ofMeterFeatures.getMaxColor()) |
| 67 | .withMaxMeters(ofMeterFeatures.getMaxMeter()); |
| 68 | /* |
| 69 | * We extract the supported band types. |
| 70 | */ |
| 71 | Set<Band.Type> bands = Sets.newHashSet(); |
| 72 | if ((DROP_VAL & ofMeterFeatures.getCapabilities()) != 0) { |
| 73 | bands.add(DROP); |
| 74 | } |
| 75 | if ((DSCP_REMARK_VAL & ofMeterFeatures.getCapabilities()) != 0) { |
| 76 | bands.add(REMARK); |
| 77 | } |
| 78 | builder.withBandTypes(bands); |
| 79 | /* |
| 80 | * We extract the supported units; |
| 81 | */ |
| 82 | Set<Meter.Unit> units = Sets.newHashSet(); |
| 83 | if ((PKTPS_VAL & ofMeterFeatures.getCapabilities()) != 0) { |
| 84 | units.add(PKTS_PER_SEC); |
| 85 | } |
| 86 | if ((KBPS_VAL & ofMeterFeatures.getCapabilities()) != 0) { |
| 87 | units.add(KB_PER_SEC); |
| 88 | } |
| 89 | if (units.isEmpty()) { |
| 90 | units.add(PKTS_PER_SEC); |
| 91 | } |
| 92 | builder.withUnits(units); |
| 93 | /* |
| 94 | * Burst is supported ? |
| 95 | */ |
| 96 | builder.hasBurst((BURST_VAL & ofMeterFeatures.getCapabilities()) != 0); |
| 97 | /* |
| 98 | * Stats are supported ? |
| 99 | */ |
| 100 | builder.hasStats((STATS_VAL & ofMeterFeatures.getCapabilities()) != 0); |
| 101 | return builder.build(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * To build an empty meter features. |
| 106 | * @param deviceId the device id |
| 107 | * @return the meter features |
| 108 | */ |
| 109 | public static MeterFeatures noMeterFeatures(DeviceId deviceId) { |
| 110 | return DefaultMeterFeatures.noMeterFeatures(deviceId); |
| 111 | } |
| 112 | |
| 113 | } |