Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.p4runtime.ctl.codec; |
| 18 | |
| 19 | import org.onosproject.net.pi.model.PiPipeconf; |
| 20 | import org.onosproject.net.pi.runtime.PiCounterCell; |
| 21 | import org.onosproject.net.pi.runtime.PiCounterCellHandle; |
| 22 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
| 23 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 24 | import p4.v1.P4RuntimeOuterClass; |
| 25 | |
| 26 | import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS; |
| 27 | |
| 28 | /** |
| 29 | * Codec for P4Runtime DirectCounterEntryCodec. |
| 30 | */ |
| 31 | public final class DirectCounterEntryCodec |
| 32 | extends AbstractEntityCodec<PiCounterCell, PiCounterCellHandle, |
| 33 | P4RuntimeOuterClass.DirectCounterEntry, Object> { |
| 34 | |
| 35 | @Override |
| 36 | protected P4RuntimeOuterClass.DirectCounterEntry encode( |
| 37 | PiCounterCell piEntity, Object ignored, PiPipeconf pipeconf, |
| 38 | P4InfoBrowser browser) |
| 39 | throws CodecException { |
| 40 | return keyMsgBuilder(piEntity.cellId(), pipeconf) |
| 41 | .setData(P4RuntimeOuterClass.CounterData.newBuilder() |
| 42 | .setByteCount(piEntity.data().bytes()) |
| 43 | .setPacketCount(piEntity.data().packets()) |
| 44 | .build()) |
| 45 | .build(); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | protected P4RuntimeOuterClass.DirectCounterEntry encodeKey( |
| 50 | PiCounterCellHandle 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.DirectCounterEntry encodeKey( |
| 58 | PiCounterCell piEntity, Object metadata, |
| 59 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 60 | throws CodecException { |
| 61 | return keyMsgBuilder(piEntity.cellId(), pipeconf).build(); |
| 62 | } |
| 63 | |
| 64 | private P4RuntimeOuterClass.DirectCounterEntry.Builder keyMsgBuilder( |
| 65 | PiCounterCellId cellId, PiPipeconf pipeconf) |
| 66 | throws CodecException { |
| 67 | return P4RuntimeOuterClass.DirectCounterEntry.newBuilder() |
| 68 | .setTableEntry(CODECS.tableEntry().encodeKey( |
| 69 | cellId.tableEntry(), null, pipeconf)); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected PiCounterCell decode( |
| 74 | P4RuntimeOuterClass.DirectCounterEntry message, Object ignored, |
| 75 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 76 | throws CodecException { |
| 77 | return new PiCounterCell( |
| 78 | PiCounterCellId.ofDirect( |
| 79 | CODECS.tableEntry().decode( |
| 80 | message.getTableEntry(), null, pipeconf)), |
| 81 | message.getData().getPacketCount(), |
| 82 | message.getData().getByteCount()); |
| 83 | } |
| 84 | } |