blob: 3831699efb9a0f34fc62dae2e6a73bb4ea2f9450 [file] [log] [blame]
Frank Wangd7e3b4b2017-09-24 13:37:54 +09001/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.net.pi.impl;
18
19import org.onosproject.net.Device;
20import org.onosproject.net.meter.Band;
21import org.onosproject.net.meter.Meter;
22import org.onosproject.net.pi.model.PiPipeconf;
23import org.onosproject.net.pi.runtime.PiMeterBand;
24import org.onosproject.net.pi.runtime.PiMeterCellConfig;
25import org.onosproject.net.pi.runtime.PiMeterCellId;
26import org.onosproject.net.pi.service.PiTranslationException;
27
28import static org.onosproject.net.meter.MeterCellId.MeterCellType.PIPELINE_INDEPENDENT;
29
30/**
31 * Implementation of meter translation logic.
32 */
33final class PiMeterTranslatorImpl {
34
35 private PiMeterTranslatorImpl() {
36 // Hides constructor.
37 }
38
39 private static final int TRTCM_RATES = 2;
40
41 /**
42 * Returns a PI meter config equivalent to the given meter, for the given pipeconf and device.
43 *
44 * @param meter meter
45 * @param pipeconf pipeconf
46 * @param device device
47 * @return PI meter configs
48 * @throws PiTranslationException if the meter cannot be translated
49 */
50 static PiMeterCellConfig translate(Meter meter, PiPipeconf pipeconf, Device device) throws PiTranslationException {
51
52 if (meter.meterCellId().type() != PIPELINE_INDEPENDENT) {
53 throw new PiTranslationException("PI meter cell type must be PIPELINE_INDEPENDENT!");
54 }
55
56 // FIXME: we might want to move this check to P4Runtime driver or protocol layer.
57 // In general, This check is more of P4Runtime limitation, we should do this check in the low level layer.
58 if (meter.bands().size() > TRTCM_RATES) {
59 throw new PiTranslationException("PI meter can not have more than 2 bands!");
60 }
61
62
63 PiMeterCellConfig.Builder builder = PiMeterCellConfig.builder();
64 for (Band band : meter.bands()) {
65 if (band.type() != Band.Type.NONE) {
66 throw new PiTranslationException("PI meter can not have band with other types except NONE!");
67 }
68
69 PiMeterBand piMeterBand = new PiMeterBand(band.rate(), band.burst());
70 builder.withMeterBand(piMeterBand);
71 }
72
73 return builder.withMeterCellId((PiMeterCellId) meter.meterCellId()).build();
74 }
75}