blob: 35845275cdcc6ba0055f21d3b7bd3c19ce4a240f [file] [log] [blame]
Carmelo Cascone4c289b72019-01-22 15:30:45 -08001/*
2 * Copyright 2019-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.p4runtime.ctl.codec;
18
19import org.onosproject.net.pi.model.PiMeterId;
20import org.onosproject.net.pi.model.PiPipeconf;
21import org.onosproject.net.pi.runtime.PiMeterBand;
22import org.onosproject.net.pi.runtime.PiMeterCellConfig;
23import org.onosproject.net.pi.runtime.PiMeterCellHandle;
24import org.onosproject.net.pi.runtime.PiMeterCellId;
25import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
26import p4.v1.P4RuntimeOuterClass;
27
28/**
29 * Codec for P4Runtime MeterEntry.
30 */
31public final class MeterEntryCodec
32 extends AbstractEntityCodec<PiMeterCellConfig, PiMeterCellHandle,
33 P4RuntimeOuterClass.MeterEntry, Object> {
34
pierventrec0914ec2021-08-27 15:25:02 +020035 static P4RuntimeOuterClass.MeterConfig getP4Config(PiMeterCellConfig piConfig) {
Wailok Shum6d42cff2021-08-22 19:40:13 +080036 // The config has no band, we don't have to create a P4RT meter config
37 if (piConfig.isDefaultConfig()) {
Wailok Shum96642092021-08-06 16:23:36 +080038 return null;
39 }
pierventrec0914ec2021-08-27 15:25:02 +020040
41 final PiMeterBand committedBand = piConfig.committedBand();
42 final PiMeterBand peakBand = piConfig.peakBand();
Carmelo Cascone4c289b72019-01-22 15:30:45 -080043 return P4RuntimeOuterClass.MeterConfig.newBuilder()
pierventrec0914ec2021-08-27 15:25:02 +020044 .setCir(committedBand.rate())
45 .setCburst(committedBand.burst())
46 .setPir(peakBand.rate())
47 .setPburst(peakBand.burst())
Carmelo Cascone4c289b72019-01-22 15:30:45 -080048 .build();
49 }
50
51 @Override
52 protected P4RuntimeOuterClass.MeterEntry encode(
53 PiMeterCellConfig piEntity, Object ignored, PiPipeconf pipeconf,
54 P4InfoBrowser browser)
pierventrec0914ec2021-08-27 15:25:02 +020055 throws P4InfoBrowser.NotFoundException {
Carmelo Cascone4c289b72019-01-22 15:30:45 -080056 final int meterId = browser.meters().getByName(
57 piEntity.cellId().meterId().id()).getPreamble().getId();
Wailok Shum96642092021-08-06 16:23:36 +080058 P4RuntimeOuterClass.MeterEntry.Builder builder =
59 P4RuntimeOuterClass.MeterEntry.newBuilder()
Carmelo Cascone4c289b72019-01-22 15:30:45 -080060 .setMeterId(meterId)
61 .setIndex(P4RuntimeOuterClass.Index.newBuilder()
Wailok Shum96642092021-08-06 16:23:36 +080062 .setIndex(piEntity.cellId().index()).build());
63 // We keep the config field unset if it is reset scenario
64 P4RuntimeOuterClass.MeterConfig meterConfig = getP4Config(piEntity);
65 if (meterConfig != null) {
66 builder = builder.setConfig(meterConfig);
67 }
68 return builder.build();
Carmelo Cascone4c289b72019-01-22 15:30:45 -080069 }
70
71 @Override
72 protected P4RuntimeOuterClass.MeterEntry encodeKey(
73 PiMeterCellHandle handle, Object metadata, PiPipeconf pipeconf,
74 P4InfoBrowser browser)
75 throws P4InfoBrowser.NotFoundException {
76 return keyMsgBuilder(handle.cellId(), browser).build();
77 }
78
79 @Override
80 protected P4RuntimeOuterClass.MeterEntry encodeKey(
81 PiMeterCellConfig piEntity, Object metadata, PiPipeconf pipeconf,
82 P4InfoBrowser browser)
83 throws P4InfoBrowser.NotFoundException {
84 return keyMsgBuilder(piEntity.cellId(), browser).build();
85 }
86
87 private P4RuntimeOuterClass.MeterEntry.Builder keyMsgBuilder(
88 PiMeterCellId cellId, P4InfoBrowser browser)
89 throws P4InfoBrowser.NotFoundException {
90 final int meterId = browser.meters().getByName(
91 cellId.meterId().id()).getPreamble().getId();
92 return P4RuntimeOuterClass.MeterEntry.newBuilder()
93 .setMeterId(meterId)
94 .setIndex(P4RuntimeOuterClass.Index.newBuilder()
95 .setIndex(cellId.index()).build());
96 }
97
98 @Override
99 protected PiMeterCellConfig decode(
100 P4RuntimeOuterClass.MeterEntry message, Object ignored,
101 PiPipeconf pipeconf, P4InfoBrowser browser)
102 throws P4InfoBrowser.NotFoundException {
103 final String meterName = browser.meters()
104 .getById(message.getMeterId())
105 .getPreamble()
106 .getName();
Wailok Shum221d70d2021-08-22 19:44:56 +0800107 PiMeterCellId cellId =
108 PiMeterCellId.ofIndirect(PiMeterId.of(meterName), message.getIndex().getIndex());
109 // When a field is unset, gRPC (P4RT) will return a default value
110 // So, if the meter config is unset, the value of rate and burst will be 0,
111 // while 0 is a meaningful value and not equals to UNSET in P4RT.
112 // We cannot extract the values directly.
113 P4RuntimeOuterClass.MeterConfig p4Config =
114 message.hasConfig() ? message.getConfig() : null;
115
116 return getPiMeterCellConfig(cellId, p4Config);
117 }
118
119 public static PiMeterCellConfig getPiMeterCellConfig(
120 PiMeterCellId cellId, P4RuntimeOuterClass.MeterConfig p4Config) {
121 PiMeterCellConfig.Builder builder =
122 PiMeterCellConfig.builder().withMeterCellId(cellId);
123 if (p4Config != null) {
pierventrec0914ec2021-08-27 15:25:02 +0200124 builder.withCommittedBand(p4Config.getCir(), p4Config.getCburst())
125 .withPeakBand(p4Config.getPir(), p4Config.getPburst());
Wailok Shum221d70d2021-08-22 19:44:56 +0800126 }
127 return builder.build();
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800128 }
129}