blob: 51102740cab0f263ae46681f2c79e19cb9bc724a [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.PiActionProfileId;
20import org.onosproject.net.pi.model.PiPipeconf;
21import org.onosproject.net.pi.runtime.PiActionProfileGroup;
22import org.onosproject.net.pi.runtime.PiActionProfileGroupHandle;
23import org.onosproject.net.pi.runtime.PiActionProfileGroupId;
24import org.onosproject.net.pi.runtime.PiActionProfileMemberId;
25import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
26import p4.v1.P4RuntimeOuterClass.ActionProfileGroup;
27
28/**
29 * Codec for P4Runtime ActionProfileGroup.
30 */
31public 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 Cascone61469462019-03-05 23:59:11 -080094 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 Cascone4c289b72019-01-22 15:30:45 -0800106 return piGroupBuilder.build();
107 }
108}