blob: 0f3241759e68d990dd2b8cf853ba1f956ec431e2 [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;
20import org.onosproject.net.pi.runtime.PiMeterBand;
21import org.onosproject.net.pi.runtime.PiMeterCellConfig;
22import org.onosproject.net.pi.runtime.PiMeterCellHandle;
23import org.onosproject.net.pi.runtime.PiMeterCellId;
24import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
25import p4.v1.P4RuntimeOuterClass;
26
27import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS;
28
29/**
30 * Codec for P4Runtime DirectMeterEntryCodec.
31 */
32public final class DirectMeterEntryCodec
33 extends AbstractEntityCodec<PiMeterCellConfig, PiMeterCellHandle,
34 P4RuntimeOuterClass.DirectMeterEntry, Object> {
35
36 @Override
37 protected P4RuntimeOuterClass.DirectMeterEntry encode(
38 PiMeterCellConfig piEntity, Object ignored, PiPipeconf pipeconf,
39 P4InfoBrowser browser)
40 throws CodecException {
Wailok Shum96642092021-08-06 16:23:36 +080041 P4RuntimeOuterClass.DirectMeterEntry.Builder builder =
42 P4RuntimeOuterClass.DirectMeterEntry.newBuilder()
Carmelo Cascone4c289b72019-01-22 15:30:45 -080043 .setTableEntry(CODECS.tableEntry().encode(
Wailok Shum96642092021-08-06 16:23:36 +080044 piEntity.cellId().tableEntry(), null, pipeconf));
45 // We keep the config field unset if it is reset scenario
46 P4RuntimeOuterClass.MeterConfig meterConfig = MeterEntryCodec.getP4Config(piEntity);
47 if (meterConfig != null) {
48 builder = builder.setConfig(meterConfig);
49 }
50 return builder.build();
Carmelo Cascone4c289b72019-01-22 15:30:45 -080051 }
52
53 @Override
54 protected P4RuntimeOuterClass.DirectMeterEntry encodeKey(
55 PiMeterCellHandle handle, Object metadata, PiPipeconf pipeconf,
56 P4InfoBrowser browser)
57 throws CodecException {
58 return keyMsgBuilder(handle.cellId(), pipeconf).build();
59 }
60
61 @Override
62 protected P4RuntimeOuterClass.DirectMeterEntry encodeKey(
63 PiMeterCellConfig piEntity, Object metadata,
64 PiPipeconf pipeconf, P4InfoBrowser browser)
65 throws CodecException {
66 return keyMsgBuilder(piEntity.cellId(), pipeconf).build();
67 }
68
69 private P4RuntimeOuterClass.DirectMeterEntry.Builder keyMsgBuilder(
70 PiMeterCellId cellId, PiPipeconf pipeconf)
71 throws CodecException {
72 return P4RuntimeOuterClass.DirectMeterEntry.newBuilder()
73 .setTableEntry(CODECS.tableEntry().encodeKey(
74 cellId.tableEntry(), null, pipeconf));
75 }
76
77 @Override
78 protected PiMeterCellConfig decode(
79 P4RuntimeOuterClass.DirectMeterEntry message, Object ignored,
80 PiPipeconf pipeconf, P4InfoBrowser browser)
81 throws CodecException {
82 return PiMeterCellConfig.builder()
83 .withMeterCellId(PiMeterCellId.ofDirect(
84 CODECS.tableEntry().decode(
85 message.getTableEntry(), null, pipeconf)))
86 .withMeterBand(new PiMeterBand(message.getConfig().getCir(),
87 message.getConfig().getCburst()))
88 .withMeterBand(new PiMeterBand(message.getConfig().getPir(),
89 message.getConfig().getPburst()))
90 .build();
91 }
92}