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 com.google.protobuf.ByteString; |
| 20 | import org.onlab.util.ImmutableByteSequence; |
| 21 | import org.onosproject.net.pi.model.PiActionId; |
| 22 | import org.onosproject.net.pi.model.PiActionParamId; |
| 23 | import org.onosproject.net.pi.model.PiPipeconf; |
| 24 | import org.onosproject.net.pi.runtime.PiAction; |
| 25 | import org.onosproject.net.pi.runtime.PiActionParam; |
| 26 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 27 | import p4.config.v1.P4InfoOuterClass; |
| 28 | import p4.v1.P4RuntimeOuterClass; |
| 29 | |
| 30 | import static java.lang.String.format; |
Daniele Moro | 8eb7c0a | 2021-05-17 14:49:31 +0200 | [diff] [blame] | 31 | import static org.onlab.util.ImmutableByteSequence.copyAndFit; |
| 32 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 33 | import static org.onosproject.p4runtime.ctl.codec.Utils.assertSize; |
| 34 | |
| 35 | /** |
| 36 | * Codec for P4Runtime Action. |
| 37 | */ |
| 38 | public final class ActionCodec |
| 39 | extends AbstractCodec<PiAction, P4RuntimeOuterClass.Action, Object> { |
| 40 | |
| 41 | @Override |
| 42 | protected P4RuntimeOuterClass.Action encode( |
| 43 | PiAction piAction, Object ignored, PiPipeconf pipeconf, P4InfoBrowser browser) |
| 44 | throws CodecException, P4InfoBrowser.NotFoundException { |
| 45 | final int actionId = browser.actions() |
| 46 | .getByName(piAction.id().toString()).getPreamble().getId(); |
| 47 | final P4RuntimeOuterClass.Action.Builder actionMsgBuilder = |
| 48 | P4RuntimeOuterClass.Action.newBuilder().setActionId(actionId); |
| 49 | for (PiActionParam p : piAction.parameters()) { |
| 50 | final P4InfoOuterClass.Action.Param paramInfo = browser.actionParams(actionId) |
| 51 | .getByName(p.id().toString()); |
Yi Tseng | 3821399 | 2022-03-17 16:04:06 -0700 | [diff] [blame^] | 52 | final ByteString paramValue; |
| 53 | if (browser.isTypeString(paramInfo.getTypeName())) { |
| 54 | paramValue = ByteString.copyFrom(p.value().asReadOnlyBuffer()); |
| 55 | } else { |
| 56 | paramValue = ByteString.copyFrom(p.value().canonical().asReadOnlyBuffer()); |
Daniele Moro | c9f115a | 2020-12-07 20:56:30 +0100 | [diff] [blame] | 57 | // Check size only if the param type is not a sdn_string |
| 58 | assertSize(format("param '%s' of action '%s'", p.id(), piAction.id()), |
Yi Tseng | 3821399 | 2022-03-17 16:04:06 -0700 | [diff] [blame^] | 59 | paramValue, paramInfo.getBitwidth()); |
Daniele Moro | c9f115a | 2020-12-07 20:56:30 +0100 | [diff] [blame] | 60 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 61 | actionMsgBuilder.addParams(P4RuntimeOuterClass.Action.Param.newBuilder() |
| 62 | .setParamId(paramInfo.getId()) |
| 63 | .setValue(paramValue) |
| 64 | .build()); |
| 65 | } |
| 66 | return actionMsgBuilder.build(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | protected PiAction decode( |
| 71 | P4RuntimeOuterClass.Action message, Object ignored, |
| 72 | PiPipeconf pipeconf, P4InfoBrowser browser) |
Daniele Moro | 8eb7c0a | 2021-05-17 14:49:31 +0200 | [diff] [blame] | 73 | throws P4InfoBrowser.NotFoundException, CodecException { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 74 | final P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo = |
| 75 | browser.actionParams(message.getActionId()); |
| 76 | final String actionName = browser.actions() |
| 77 | .getById(message.getActionId()) |
| 78 | .getPreamble().getName(); |
| 79 | final PiAction.Builder builder = PiAction.builder() |
| 80 | .withId(PiActionId.of(actionName)); |
| 81 | for (P4RuntimeOuterClass.Action.Param p : message.getParamsList()) { |
Daniele Moro | 787a064 | 2021-02-23 15:28:07 +0100 | [diff] [blame] | 82 | final P4InfoOuterClass.Action.Param actionParam = paramInfo.getById(p.getParamId()); |
| 83 | final ImmutableByteSequence value; |
| 84 | if (browser.isTypeString(actionParam.getTypeName())) { |
Daniele Moro | 8eb7c0a | 2021-05-17 14:49:31 +0200 | [diff] [blame] | 85 | value = copyFrom(new String(p.getValue().toByteArray())); |
Daniele Moro | 787a064 | 2021-02-23 15:28:07 +0100 | [diff] [blame] | 86 | } else { |
Daniele Moro | 8eb7c0a | 2021-05-17 14:49:31 +0200 | [diff] [blame] | 87 | try { |
| 88 | value = copyAndFit(p.getValue().asReadOnlyByteBuffer(), |
| 89 | actionParam.getBitwidth()); |
| 90 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 91 | throw new CodecException(e.getMessage()); |
| 92 | } |
Daniele Moro | 787a064 | 2021-02-23 15:28:07 +0100 | [diff] [blame] | 93 | } |
Daniele Moro | 8eb7c0a | 2021-05-17 14:49:31 +0200 | [diff] [blame] | 94 | builder.withParameter(new PiActionParam( |
| 95 | PiActionParamId.of(actionParam.getName()), value)); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 96 | } |
| 97 | return builder.build(); |
| 98 | } |
| 99 | } |