blob: 35f9ddea5a3342fa090658fd91001ef196c40c54 [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
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.toprak409289d2017-10-27 10:04:05 +0300109
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 Ortizaa8de492016-12-01 00:21:36 +0100127 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}