Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 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; |
cansu.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 24 | import org.onosproject.net.meter.MeterFeaturesFlag; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 25 | import org.projectfloodlight.openflow.protocol.OFMeterFeatures; |
cansu.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.protocol.OFVersion; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | import static org.onosproject.net.meter.Band.Type.DROP; |
| 34 | import static org.onosproject.net.meter.Band.Type.REMARK; |
| 35 | import static org.onosproject.net.meter.Meter.Unit.KB_PER_SEC; |
| 36 | import static org.onosproject.net.meter.Meter.Unit.PKTS_PER_SEC; |
cansu.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 37 | import static org.onosproject.net.meter.MeterFeaturesFlag.ACTION_SET; |
| 38 | import static org.onosproject.net.meter.MeterFeaturesFlag.ANY_POSITION; |
| 39 | import static org.onosproject.net.meter.MeterFeaturesFlag.MULTI_LIST; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 40 | import static org.projectfloodlight.openflow.protocol.ver13.OFMeterBandTypeSerializerVer13.DROP_VAL; |
| 41 | import static org.projectfloodlight.openflow.protocol.ver13.OFMeterBandTypeSerializerVer13.DSCP_REMARK_VAL; |
| 42 | import static org.projectfloodlight.openflow.protocol.ver13.OFMeterFlagsSerializerVer13.*; |
cansu.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 43 | import static org.projectfloodlight.openflow.protocol.ver15.OFMeterFeatureFlagsSerializerVer15.ACTION_SET_VAL; |
| 44 | import static org.projectfloodlight.openflow.protocol.ver15.OFMeterFeatureFlagsSerializerVer15.ANY_POSITION_VAL; |
| 45 | import static org.projectfloodlight.openflow.protocol.ver15.OFMeterFeatureFlagsSerializerVer15.MULTI_LIST_VAL; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * OpenFlow builder of MeterFeatures. |
| 49 | */ |
| 50 | public class MeterFeaturesBuilder { |
| 51 | private static final Logger log = LoggerFactory.getLogger(MeterFeaturesBuilder.class); |
| 52 | |
| 53 | private final OFMeterFeatures ofMeterFeatures; |
| 54 | private DeviceId deviceId; |
| 55 | |
| 56 | public MeterFeaturesBuilder(OFMeterFeatures features, DeviceId deviceId) { |
| 57 | this.ofMeterFeatures = checkNotNull(features); |
| 58 | this.deviceId = deviceId; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * To build a MeterFeatures using the openflow object |
| 63 | * provided by the southbound. |
| 64 | * |
| 65 | * @return the meter features object |
| 66 | */ |
| 67 | public MeterFeatures build() { |
| 68 | /* |
| 69 | * We set the basic values before to extract the other information. |
| 70 | */ |
| 71 | MeterFeatures.Builder builder = DefaultMeterFeatures.builder() |
| 72 | .forDevice(deviceId) |
| 73 | .withMaxBands(ofMeterFeatures.getMaxBands()) |
| 74 | .withMaxColors(ofMeterFeatures.getMaxColor()) |
| 75 | .withMaxMeters(ofMeterFeatures.getMaxMeter()); |
| 76 | /* |
| 77 | * We extract the supported band types. |
| 78 | */ |
| 79 | Set<Band.Type> bands = Sets.newHashSet(); |
| 80 | if ((DROP_VAL & ofMeterFeatures.getCapabilities()) != 0) { |
| 81 | bands.add(DROP); |
| 82 | } |
| 83 | if ((DSCP_REMARK_VAL & ofMeterFeatures.getCapabilities()) != 0) { |
| 84 | bands.add(REMARK); |
| 85 | } |
| 86 | builder.withBandTypes(bands); |
| 87 | /* |
| 88 | * We extract the supported units; |
| 89 | */ |
| 90 | Set<Meter.Unit> units = Sets.newHashSet(); |
| 91 | if ((PKTPS_VAL & ofMeterFeatures.getCapabilities()) != 0) { |
| 92 | units.add(PKTS_PER_SEC); |
| 93 | } |
| 94 | if ((KBPS_VAL & ofMeterFeatures.getCapabilities()) != 0) { |
| 95 | units.add(KB_PER_SEC); |
| 96 | } |
| 97 | if (units.isEmpty()) { |
| 98 | units.add(PKTS_PER_SEC); |
| 99 | } |
| 100 | builder.withUnits(units); |
| 101 | /* |
| 102 | * Burst is supported ? |
| 103 | */ |
| 104 | builder.hasBurst((BURST_VAL & ofMeterFeatures.getCapabilities()) != 0); |
| 105 | /* |
| 106 | * Stats are supported ? |
| 107 | */ |
| 108 | builder.hasStats((STATS_VAL & ofMeterFeatures.getCapabilities()) != 0); |
cansu.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * Along with the OF1.5, we extract meter features flags |
| 112 | */ |
| 113 | if (ofMeterFeatures.getVersion().wireVersion >= OFVersion.OF_15.wireVersion) { |
| 114 | Set<MeterFeaturesFlag> meterFeaturesFlags = Sets.newHashSet(); |
| 115 | if ((ACTION_SET_VAL & ofMeterFeatures.getFeatures()) != 0) { |
| 116 | meterFeaturesFlags.add(ACTION_SET); |
| 117 | } |
| 118 | if ((ANY_POSITION_VAL & ofMeterFeatures.getFeatures()) != 0) { |
| 119 | meterFeaturesFlags.add(ANY_POSITION); |
| 120 | } |
| 121 | if ((MULTI_LIST_VAL & ofMeterFeatures.getFeatures()) != 0) { |
| 122 | meterFeaturesFlags.add(MULTI_LIST); |
| 123 | } |
| 124 | builder.withFeatures(meterFeaturesFlags); |
| 125 | } |
| 126 | |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 127 | return builder.build(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * To build an empty meter features. |
| 132 | * @param deviceId the device id |
| 133 | * @return the meter features |
| 134 | */ |
| 135 | public static MeterFeatures noMeterFeatures(DeviceId deviceId) { |
| 136 | return DefaultMeterFeatures.noMeterFeatures(deviceId); |
| 137 | } |
| 138 | |
| 139 | } |