blob: 3be0244a82dca6dd49814c5f134de63a4dbb1a69 [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.PiPipeconf;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080020import org.onosproject.net.pi.runtime.PiMeterCellConfig;
21import org.onosproject.net.pi.runtime.PiMeterCellHandle;
22import org.onosproject.net.pi.runtime.PiMeterCellId;
23import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
24import p4.v1.P4RuntimeOuterClass;
25
26import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS;
27
28/**
29 * Codec for P4Runtime DirectMeterEntryCodec.
30 */
31public final class DirectMeterEntryCodec
32 extends AbstractEntityCodec<PiMeterCellConfig, PiMeterCellHandle,
33 P4RuntimeOuterClass.DirectMeterEntry, Object> {
34
35 @Override
36 protected P4RuntimeOuterClass.DirectMeterEntry encode(
37 PiMeterCellConfig piEntity, Object ignored, PiPipeconf pipeconf,
38 P4InfoBrowser browser)
39 throws CodecException {
Wailok Shum96642092021-08-06 16:23:36 +080040 P4RuntimeOuterClass.DirectMeterEntry.Builder builder =
41 P4RuntimeOuterClass.DirectMeterEntry.newBuilder()
Carmelo Cascone4c289b72019-01-22 15:30:45 -080042 .setTableEntry(CODECS.tableEntry().encode(
Wailok Shum96642092021-08-06 16:23:36 +080043 piEntity.cellId().tableEntry(), null, pipeconf));
44 // We keep the config field unset if it is reset scenario
45 P4RuntimeOuterClass.MeterConfig meterConfig = MeterEntryCodec.getP4Config(piEntity);
46 if (meterConfig != null) {
47 builder = builder.setConfig(meterConfig);
48 }
49 return builder.build();
Carmelo Cascone4c289b72019-01-22 15:30:45 -080050 }
51
52 @Override
53 protected P4RuntimeOuterClass.DirectMeterEntry encodeKey(
54 PiMeterCellHandle handle, Object metadata, PiPipeconf pipeconf,
55 P4InfoBrowser browser)
56 throws CodecException {
57 return keyMsgBuilder(handle.cellId(), pipeconf).build();
58 }
59
60 @Override
61 protected P4RuntimeOuterClass.DirectMeterEntry encodeKey(
62 PiMeterCellConfig piEntity, Object metadata,
63 PiPipeconf pipeconf, P4InfoBrowser browser)
64 throws CodecException {
65 return keyMsgBuilder(piEntity.cellId(), pipeconf).build();
66 }
67
68 private P4RuntimeOuterClass.DirectMeterEntry.Builder keyMsgBuilder(
69 PiMeterCellId cellId, PiPipeconf pipeconf)
70 throws CodecException {
71 return P4RuntimeOuterClass.DirectMeterEntry.newBuilder()
72 .setTableEntry(CODECS.tableEntry().encodeKey(
73 cellId.tableEntry(), null, pipeconf));
74 }
75
76 @Override
77 protected PiMeterCellConfig decode(
78 P4RuntimeOuterClass.DirectMeterEntry message, Object ignored,
79 PiPipeconf pipeconf, P4InfoBrowser browser)
80 throws CodecException {
Wailok Shum221d70d2021-08-22 19:44:56 +080081 PiMeterCellId cellId =
82 PiMeterCellId.ofDirect(
83 CODECS.tableEntry().decode(
84 message.getTableEntry(), null, pipeconf));
85 // When a field is unset, gRPC (P4RT) will return a default value
86 // So, if the meter config is unset, the value of rate and burst will be 0,
87 // while 0 is a meaningful value and not equals to UNSET in P4RT.
88 // We cannot extract the values directly.
89 P4RuntimeOuterClass.MeterConfig p4Config =
90 message.hasConfig() ? message.getConfig() : null;
91
92 return MeterEntryCodec.getPiMeterCellConfig(cellId, p4Config);
Carmelo Cascone4c289b72019-01-22 15:30:45 -080093 }
94}