blob: abfa4aa8bc3ee03dd7d8f22faa99b028b3ae3f8a [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;
24import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import java.util.Set;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31import static org.onosproject.net.meter.Band.Type.DROP;
32import static org.onosproject.net.meter.Band.Type.REMARK;
33import static org.onosproject.net.meter.Meter.Unit.KB_PER_SEC;
34import static org.onosproject.net.meter.Meter.Unit.PKTS_PER_SEC;
35import static org.projectfloodlight.openflow.protocol.ver13.OFMeterBandTypeSerializerVer13.DROP_VAL;
36import static org.projectfloodlight.openflow.protocol.ver13.OFMeterBandTypeSerializerVer13.DSCP_REMARK_VAL;
37import static org.projectfloodlight.openflow.protocol.ver13.OFMeterFlagsSerializerVer13.*;
38
39/**
40 * OpenFlow builder of MeterFeatures.
41 */
42public 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}