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.PiActionProfileId; |
| 20 | import org.onosproject.net.pi.model.PiPipeconf; |
| 21 | import org.onosproject.net.pi.runtime.PiActionProfileGroup; |
| 22 | import org.onosproject.net.pi.runtime.PiActionProfileGroupHandle; |
| 23 | import org.onosproject.net.pi.runtime.PiActionProfileGroupId; |
| 24 | import org.onosproject.net.pi.runtime.PiActionProfileMemberId; |
| 25 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 26 | import p4.v1.P4RuntimeOuterClass.ActionProfileGroup; |
| 27 | |
| 28 | /** |
| 29 | * Codec for P4Runtime ActionProfileGroup. |
| 30 | */ |
| 31 | public final class ActionProfileGroupCodec |
| 32 | extends AbstractEntityCodec<PiActionProfileGroup, PiActionProfileGroupHandle, ActionProfileGroup, Object> { |
| 33 | |
| 34 | @Override |
| 35 | public ActionProfileGroup encode( |
| 36 | PiActionProfileGroup piGroup, Object ignored, PiPipeconf pipeconf, |
| 37 | P4InfoBrowser browser) |
| 38 | throws P4InfoBrowser.NotFoundException { |
| 39 | final ActionProfileGroup.Builder msgBuilder = keyMsgBuilder( |
| 40 | piGroup.actionProfile(), piGroup.id(), browser) |
| 41 | .setMaxSize(piGroup.maxSize()); |
| 42 | piGroup.members().forEach(m -> { |
| 43 | // TODO: currently we don't set "watch" field |
| 44 | ActionProfileGroup.Member member = ActionProfileGroup.Member.newBuilder() |
| 45 | .setMemberId(m.id().id()) |
| 46 | .setWeight(m.weight()) |
| 47 | .build(); |
| 48 | msgBuilder.addMembers(member); |
| 49 | }); |
| 50 | return msgBuilder.build(); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected ActionProfileGroup encodeKey( |
| 55 | PiActionProfileGroupHandle handle, Object ignored, |
| 56 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 57 | throws P4InfoBrowser.NotFoundException { |
| 58 | return keyMsgBuilder(handle.actionProfile(), handle.groupId(), browser) |
| 59 | .build(); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | protected ActionProfileGroup encodeKey( |
| 64 | PiActionProfileGroup piEntity, Object metadata, |
| 65 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 66 | throws P4InfoBrowser.NotFoundException { |
| 67 | return keyMsgBuilder(piEntity.actionProfile(), piEntity.id(), browser) |
| 68 | .build(); |
| 69 | } |
| 70 | |
| 71 | private ActionProfileGroup.Builder keyMsgBuilder( |
| 72 | PiActionProfileId actProfId, PiActionProfileGroupId groupId, |
| 73 | P4InfoBrowser browser) |
| 74 | throws P4InfoBrowser.NotFoundException { |
| 75 | return ActionProfileGroup.newBuilder() |
| 76 | .setGroupId(groupId.id()) |
| 77 | .setActionProfileId(browser.actionProfiles() |
| 78 | .getByName(actProfId.id()) |
| 79 | .getPreamble().getId()); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public PiActionProfileGroup decode( |
| 84 | ActionProfileGroup msg, Object ignored, PiPipeconf pipeconf, |
| 85 | P4InfoBrowser browser) |
| 86 | throws P4InfoBrowser.NotFoundException { |
| 87 | final PiActionProfileGroup.Builder piGroupBuilder = PiActionProfileGroup.builder() |
| 88 | .withActionProfileId(PiActionProfileId.of( |
| 89 | browser.actionProfiles() |
| 90 | .getById(msg.getActionProfileId()) |
| 91 | .getPreamble().getName())) |
| 92 | .withId(PiActionProfileGroupId.of(msg.getGroupId())) |
| 93 | .withMaxSize(msg.getMaxSize()); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 94 | msg.getMembersList().forEach(m -> { |
| 95 | final int weight; |
| 96 | if (m.getWeight() < 1) { |
| 97 | log.warn("Decoding group with invalid weight '{}', will set to 1", |
| 98 | m.getWeight()); |
| 99 | weight = 1; |
| 100 | } else { |
| 101 | weight = m.getWeight(); |
| 102 | } |
| 103 | piGroupBuilder.addMember( |
| 104 | PiActionProfileMemberId.of(m.getMemberId()), weight); |
| 105 | }); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 106 | return piGroupBuilder.build(); |
| 107 | } |
| 108 | } |