blob: 4fc61aa81643bc4c88902bf06413ecec1c888933 [file] [log] [blame]
Jordi Ortizaa8de492016-12-01 00:21:36 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jordi Ortizaa8de492016-12-01 00:21:36 +01003 *
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 */
16package org.onosproject.provider.of.meter.util;
17
18import com.google.common.collect.Sets;
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.meter.Band;
21import org.onosproject.net.meter.DefaultMeterFeatures;
22import org.onosproject.net.meter.Meter;
23import org.onosproject.net.meter.MeterFeatures;
cansu.toprak409289d2017-10-27 10:04:05 +030024import org.onosproject.net.meter.MeterFeaturesFlag;
Jordi Ortizaa8de492016-12-01 00:21:36 +010025import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
cansu.toprak409289d2017-10-27 10:04:05 +030026import org.projectfloodlight.openflow.protocol.OFVersion;
Jordi Ortizaa8de492016-12-01 00:21:36 +010027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.Set;
31
32import static com.google.common.base.Preconditions.checkNotNull;
33import static org.onosproject.net.meter.Band.Type.DROP;
34import static org.onosproject.net.meter.Band.Type.REMARK;
35import static org.onosproject.net.meter.Meter.Unit.KB_PER_SEC;
36import static org.onosproject.net.meter.Meter.Unit.PKTS_PER_SEC;
cansu.toprak409289d2017-10-27 10:04:05 +030037import static org.onosproject.net.meter.MeterFeaturesFlag.ACTION_SET;
38import static org.onosproject.net.meter.MeterFeaturesFlag.ANY_POSITION;
39import static org.onosproject.net.meter.MeterFeaturesFlag.MULTI_LIST;
Jordi Ortizaa8de492016-12-01 00:21:36 +010040import static org.projectfloodlight.openflow.protocol.ver13.OFMeterBandTypeSerializerVer13.DROP_VAL;
41import static org.projectfloodlight.openflow.protocol.ver13.OFMeterBandTypeSerializerVer13.DSCP_REMARK_VAL;
42import static org.projectfloodlight.openflow.protocol.ver13.OFMeterFlagsSerializerVer13.*;
cansu.toprak409289d2017-10-27 10:04:05 +030043import static org.projectfloodlight.openflow.protocol.ver15.OFMeterFeatureFlagsSerializerVer15.ACTION_SET_VAL;
44import static org.projectfloodlight.openflow.protocol.ver15.OFMeterFeatureFlagsSerializerVer15.ANY_POSITION_VAL;
45import static org.projectfloodlight.openflow.protocol.ver15.OFMeterFeatureFlagsSerializerVer15.MULTI_LIST_VAL;
Jordi Ortizaa8de492016-12-01 00:21:36 +010046
47/**
48 * OpenFlow builder of MeterFeatures.
49 */
50public class MeterFeaturesBuilder {
51 private static final Logger log = LoggerFactory.getLogger(MeterFeaturesBuilder.class);
52
53 private final OFMeterFeatures ofMeterFeatures;
54 private DeviceId deviceId;
55
Wailok Shumf013a782021-07-26 16:51:01 +080056 private static final long OF_METER_START_INDEX = 1L;
57
Jordi Ortizaa8de492016-12-01 00:21:36 +010058 public MeterFeaturesBuilder(OFMeterFeatures features, DeviceId deviceId) {
59 this.ofMeterFeatures = checkNotNull(features);
60 this.deviceId = deviceId;
61 }
62
63 /**
64 * To build a MeterFeatures using the openflow object
65 * provided by the southbound.
66 *
67 * @return the meter features object
68 */
69 public MeterFeatures build() {
70 /*
71 * We set the basic values before to extract the other information.
72 */
73 MeterFeatures.Builder builder = DefaultMeterFeatures.builder()
74 .forDevice(deviceId)
75 .withMaxBands(ofMeterFeatures.getMaxBands())
76 .withMaxColors(ofMeterFeatures.getMaxColor())
Wailok Shumf013a782021-07-26 16:51:01 +080077 .withStartIndex(OF_METER_START_INDEX)
78 .withEndIndex(ofMeterFeatures.getMaxMeter());
Jordi Ortizaa8de492016-12-01 00:21:36 +010079 /*
80 * We extract the supported band types.
81 */
82 Set<Band.Type> bands = Sets.newHashSet();
83 if ((DROP_VAL & ofMeterFeatures.getCapabilities()) != 0) {
84 bands.add(DROP);
85 }
86 if ((DSCP_REMARK_VAL & ofMeterFeatures.getCapabilities()) != 0) {
87 bands.add(REMARK);
88 }
89 builder.withBandTypes(bands);
90 /*
91 * We extract the supported units;
92 */
93 Set<Meter.Unit> units = Sets.newHashSet();
94 if ((PKTPS_VAL & ofMeterFeatures.getCapabilities()) != 0) {
95 units.add(PKTS_PER_SEC);
96 }
97 if ((KBPS_VAL & ofMeterFeatures.getCapabilities()) != 0) {
98 units.add(KB_PER_SEC);
99 }
100 if (units.isEmpty()) {
101 units.add(PKTS_PER_SEC);
102 }
103 builder.withUnits(units);
104 /*
105 * Burst is supported ?
106 */
107 builder.hasBurst((BURST_VAL & ofMeterFeatures.getCapabilities()) != 0);
108 /*
109 * Stats are supported ?
110 */
111 builder.hasStats((STATS_VAL & ofMeterFeatures.getCapabilities()) != 0);
cansu.toprak409289d2017-10-27 10:04:05 +0300112
113 /*
114 * Along with the OF1.5, we extract meter features flags
115 */
116 if (ofMeterFeatures.getVersion().wireVersion >= OFVersion.OF_15.wireVersion) {
117 Set<MeterFeaturesFlag> meterFeaturesFlags = Sets.newHashSet();
118 if ((ACTION_SET_VAL & ofMeterFeatures.getFeatures()) != 0) {
119 meterFeaturesFlags.add(ACTION_SET);
120 }
121 if ((ANY_POSITION_VAL & ofMeterFeatures.getFeatures()) != 0) {
122 meterFeaturesFlags.add(ANY_POSITION);
123 }
124 if ((MULTI_LIST_VAL & ofMeterFeatures.getFeatures()) != 0) {
125 meterFeaturesFlags.add(MULTI_LIST);
126 }
127 builder.withFeatures(meterFeaturesFlags);
128 }
129
Jordi Ortizaa8de492016-12-01 00:21:36 +0100130 return builder.build();
131 }
132
133 /**
134 * To build an empty meter features.
135 * @param deviceId the device id
136 * @return the meter features
137 */
138 public static MeterFeatures noMeterFeatures(DeviceId deviceId) {
139 return DefaultMeterFeatures.noMeterFeatures(deviceId);
140 }
141
142}