blob: deac99347cda3a7264067699a5147d4d4b0b3b15 [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.PiCounterId;
20import org.onosproject.net.pi.model.PiPipeconf;
21import org.onosproject.net.pi.runtime.PiCounterCell;
22import org.onosproject.net.pi.runtime.PiCounterCellHandle;
23import org.onosproject.net.pi.runtime.PiCounterCellId;
24import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
25import p4.v1.P4RuntimeOuterClass;
26
27/**
28 * Codec for P4Runtime CounterEntry.
29 */
30public final class CounterEntryCodec
31 extends AbstractEntityCodec<PiCounterCell, PiCounterCellHandle,
32 P4RuntimeOuterClass.CounterEntry, Object> {
33
34 @Override
35 protected P4RuntimeOuterClass.CounterEntry encode(
36 PiCounterCell piEntity, Object ignored, PiPipeconf pipeconf,
37 P4InfoBrowser browser)
38 throws P4InfoBrowser.NotFoundException {
39 return keyMsgBuilder(piEntity.cellId(), browser)
40 .setData(P4RuntimeOuterClass.CounterData.newBuilder()
41 .setByteCount(piEntity.data().bytes())
42 .setPacketCount(piEntity.data().packets())
43 .build())
44 .build();
45 }
46
47 @Override
48 protected P4RuntimeOuterClass.CounterEntry encodeKey(
49 PiCounterCellHandle handle, Object metadata, PiPipeconf pipeconf,
50 P4InfoBrowser browser)
51 throws P4InfoBrowser.NotFoundException {
52 return keyMsgBuilder(handle.cellId(), browser).build();
53 }
54
55 @Override
56 protected P4RuntimeOuterClass.CounterEntry encodeKey(
57 PiCounterCell piEntity, Object metadata, PiPipeconf pipeconf,
58 P4InfoBrowser browser)
59 throws P4InfoBrowser.NotFoundException {
60 return keyMsgBuilder(piEntity.cellId(), browser).build();
61 }
62
63 private P4RuntimeOuterClass.CounterEntry.Builder keyMsgBuilder(
64 PiCounterCellId cellId, P4InfoBrowser browser)
65 throws P4InfoBrowser.NotFoundException {
66 final int counterId = browser.counters().getByName(
67 cellId.counterId().id()).getPreamble().getId();
68 return P4RuntimeOuterClass.CounterEntry.newBuilder()
69 .setCounterId(counterId)
70 .setIndex(P4RuntimeOuterClass.Index.newBuilder()
71 .setIndex(cellId.index()).build());
72 }
73
74 @Override
75 protected PiCounterCell decode(
76 P4RuntimeOuterClass.CounterEntry message, Object ignored,
77 PiPipeconf pipeconf, P4InfoBrowser browser)
78 throws P4InfoBrowser.NotFoundException {
79 final String counterName = browser.counters()
80 .getById(message.getCounterId())
81 .getPreamble()
82 .getName();
83 return new PiCounterCell(
84 PiCounterCellId.ofIndirect(
85 PiCounterId.of(counterName), message.getIndex().getIndex()),
86 message.getData().getPacketCount(),
87 message.getData().getByteCount());
88 }
89}