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.Message; |
| 20 | import org.onosproject.net.pi.model.PiPipeconf; |
| 21 | import org.onosproject.net.pi.runtime.PiEntity; |
| 22 | import org.onosproject.net.pi.runtime.PiHandle; |
| 23 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * Abstract implementation of a specialized codec that translates PI runtime |
| 29 | * entities and their handles into P4Runtime protobuf messages and vice versa. |
| 30 | * Supports also encoding to "key" P4Runtime Entity messages used in read and |
| 31 | * delete operations. |
| 32 | * |
| 33 | * @param <P> PI runtime class |
| 34 | * @param <H> PI handle class |
| 35 | * @param <M> P4Runtime protobuf message class |
| 36 | * @param <X> metadata class |
| 37 | */ |
| 38 | public abstract class AbstractEntityCodec |
| 39 | <P extends PiEntity, H extends PiHandle, M extends Message, X> |
| 40 | extends AbstractCodec<P, M, X> { |
| 41 | |
| 42 | protected abstract M encodeKey(H handle, X metadata, PiPipeconf pipeconf, |
| 43 | P4InfoBrowser browser) |
| 44 | throws CodecException, P4InfoBrowser.NotFoundException; |
| 45 | |
| 46 | protected abstract M encodeKey(P piEntity, X metadata, PiPipeconf pipeconf, |
| 47 | P4InfoBrowser browser) |
| 48 | throws CodecException, P4InfoBrowser.NotFoundException; |
| 49 | |
| 50 | /** |
| 51 | * Returns a P4Runtime protobuf message representing the P4Runtime.Entity |
| 52 | * "key" for the given PI handle, metadata and pipeconf. |
| 53 | * |
| 54 | * @param handle PI handle instance |
| 55 | * @param metadata metadata |
| 56 | * @param pipeconf pipeconf |
| 57 | * @return P4Runtime protobuf message |
| 58 | * @throws CodecException if the given PI entity cannot be encoded (see |
| 59 | * exception message) |
| 60 | */ |
| 61 | public M encodeKey(H handle, X metadata, PiPipeconf pipeconf) |
| 62 | throws CodecException { |
| 63 | checkNotNull(handle); |
| 64 | try { |
| 65 | return encodeKey(handle, metadata, pipeconf, browserOrFail(pipeconf)); |
| 66 | } catch (P4InfoBrowser.NotFoundException e) { |
| 67 | throw new CodecException(e.getMessage()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns a P4Runtime protobuf message representing the P4Runtime.Entity |
| 73 | * "key" for the given PI entity, metadata and pipeconf. |
| 74 | * |
| 75 | * @param piEntity PI entity instance |
| 76 | * @param metadata metadata |
| 77 | * @param pipeconf pipeconf |
| 78 | * @return P4Runtime protobuf message |
| 79 | * @throws CodecException if the given PI entity cannot be encoded (see |
| 80 | * exception message) |
| 81 | */ |
| 82 | public M encodeKey(P piEntity, X metadata, PiPipeconf pipeconf) |
| 83 | throws CodecException { |
| 84 | checkNotNull(piEntity); |
| 85 | try { |
| 86 | return encodeKey(piEntity, metadata, pipeconf, browserOrFail(pipeconf)); |
| 87 | } catch (P4InfoBrowser.NotFoundException e) { |
| 88 | throw new CodecException(e.getMessage()); |
| 89 | } |
| 90 | } |
| 91 | } |