blob: 684ef04196d4ae07bba0c078d40807b57c591989 [file] [log] [blame]
Carmelo Cascone99c59db2019-01-17 15:39:35 -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;
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.PiActionProfileGroupId;
23import org.onosproject.net.pi.runtime.PiActionProfileMemberId;
24import p4.v1.P4RuntimeOuterClass.ActionProfileGroup;
25
26/**
27 * Codec for P4Runtime ActionProfileGroup.
28 */
29final class ActionProfileGroupCodec
30 extends AbstractP4RuntimeCodec<PiActionProfileGroup, ActionProfileGroup> {
31
32 @Override
33 public ActionProfileGroup encode(
34 PiActionProfileGroup piGroup, PiPipeconf pipeconf, P4InfoBrowser browser)
35 throws P4InfoBrowser.NotFoundException {
36
37 final int p4ActionProfileId = browser.actionProfiles()
38 .getByName(piGroup.actionProfile().id())
39 .getPreamble().getId();
40 final ActionProfileGroup.Builder msgBuilder = ActionProfileGroup.newBuilder()
41 .setGroupId(piGroup.id().id())
42 .setActionProfileId(p4ActionProfileId)
43 .setMaxSize(piGroup.maxSize());
44 piGroup.members().forEach(m -> {
45 // TODO: currently we don't set "watch" field
46 ActionProfileGroup.Member member = ActionProfileGroup.Member.newBuilder()
47 .setMemberId(m.id().id())
48 .setWeight(m.weight())
49 .build();
50 msgBuilder.addMembers(member);
51 });
52
53 return msgBuilder.build();
54 }
55
56 @Override
57 public PiActionProfileGroup decode(
58 ActionProfileGroup msg, PiPipeconf pipeconf, P4InfoBrowser browser)
59 throws CodecException, P4InfoBrowser.NotFoundException {
60
61 final PiActionProfileGroup.Builder piGroupBuilder = PiActionProfileGroup.builder()
62 .withActionProfileId(PiActionProfileId.of(
63 browser.actionProfiles()
64 .getById(msg.getActionProfileId())
65 .getPreamble().getName()))
66 .withId(PiActionProfileGroupId.of(msg.getGroupId()))
67 .withMaxSize(msg.getMaxSize());
68
69 msg.getMembersList().forEach(m -> {
70 int weight = m.getWeight();
71 if (weight < 1) {
72 // FIXME: currently PI has a bug which will always return weight 0
73 // ONOS won't accept group buckets with weight 0
74 log.warn("Decoding ActionProfileGroup with 'weight' " +
75 "field {}, will set to 1", weight);
76 weight = 1;
77 }
78 piGroupBuilder.addMember(PiActionProfileMemberId.of(
79 m.getMemberId()), weight);
80 });
81 return piGroupBuilder.build();
82 }
83}