blob: 3bedfbf411548f25c7412f8dfe474bb237417880 [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 {
41 return P4RuntimeOuterClass.DirectMeterEntry.newBuilder()
42 .setTableEntry(CODECS.tableEntry().encode(
43 piEntity.cellId().tableEntry(), null, pipeconf))
44 .setConfig(MeterEntryCodec.getP4Config(piEntity))
45 .build();
46 }
47
48 @Override
49 protected P4RuntimeOuterClass.DirectMeterEntry encodeKey(
50 PiMeterCellHandle handle, Object metadata, PiPipeconf pipeconf,
51 P4InfoBrowser browser)
52 throws CodecException {
53 return keyMsgBuilder(handle.cellId(), pipeconf).build();
54 }
55
56 @Override
57 protected P4RuntimeOuterClass.DirectMeterEntry encodeKey(
58 PiMeterCellConfig piEntity, Object metadata,
59 PiPipeconf pipeconf, P4InfoBrowser browser)
60 throws CodecException {
61 return keyMsgBuilder(piEntity.cellId(), pipeconf).build();
62 }
63
64 private P4RuntimeOuterClass.DirectMeterEntry.Builder keyMsgBuilder(
65 PiMeterCellId cellId, PiPipeconf pipeconf)
66 throws CodecException {
67 return P4RuntimeOuterClass.DirectMeterEntry.newBuilder()
68 .setTableEntry(CODECS.tableEntry().encodeKey(
69 cellId.tableEntry(), null, pipeconf));
70 }
71
72 @Override
73 protected PiMeterCellConfig decode(
74 P4RuntimeOuterClass.DirectMeterEntry message, Object ignored,
75 PiPipeconf pipeconf, P4InfoBrowser browser)
76 throws CodecException {
77 return PiMeterCellConfig.builder()
78 .withMeterCellId(PiMeterCellId.ofDirect(
79 CODECS.tableEntry().decode(
80 message.getTableEntry(), null, pipeconf)))
81 .withMeterBand(new PiMeterBand(message.getConfig().getCir(),
82 message.getConfig().getCburst()))
83 .withMeterBand(new PiMeterBand(message.getConfig().getPir(),
84 message.getConfig().getPburst()))
85 .build();
86 }
87}