blob: 183d827c4841265235067c6449b59086cc57722d [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.PiActionProfileMember;
22import org.onosproject.net.pi.runtime.PiActionProfileMemberHandle;
23import org.onosproject.net.pi.runtime.PiActionProfileMemberId;
24import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
25import p4.v1.P4RuntimeOuterClass;
26import p4.v1.P4RuntimeOuterClass.ActionProfileMember;
27
28import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS;
29
30/**
31 * Codec for ActionProfileMember.
32 */
33public final class ActionProfileMemberCodec
34 extends AbstractEntityCodec<PiActionProfileMember, PiActionProfileMemberHandle, ActionProfileMember, Object> {
35
36 @Override
37 public ActionProfileMember encode(
38 PiActionProfileMember piEntity, Object ignored,
39 PiPipeconf pipeconf, P4InfoBrowser browser)
40 throws CodecException, P4InfoBrowser.NotFoundException {
41 return keyMsgBuilder(
42 piEntity.actionProfile(), piEntity.id(), browser)
43 .setAction(CODECS.action().encode(
44 piEntity.action(), null, pipeconf))
45 .build();
46 }
47
48 @Override
49 protected ActionProfileMember encodeKey(
50 PiActionProfileMemberHandle handle, Object ignored,
51 PiPipeconf pipeconf, P4InfoBrowser browser)
52 throws P4InfoBrowser.NotFoundException {
53 return keyMsgBuilder(handle.actionProfileId(), handle.memberId(), browser)
54 .build();
55 }
56
57 @Override
58 protected ActionProfileMember encodeKey(
59 PiActionProfileMember piEntity, Object metadata,
60 PiPipeconf pipeconf, P4InfoBrowser browser)
61 throws P4InfoBrowser.NotFoundException {
62 return keyMsgBuilder(
63 piEntity.actionProfile(), piEntity.id(), browser)
64 .build();
65 }
66
67 private P4RuntimeOuterClass.ActionProfileMember.Builder keyMsgBuilder(
68 PiActionProfileId actProfId, PiActionProfileMemberId memberId,
69 P4InfoBrowser browser)
70 throws P4InfoBrowser.NotFoundException {
71 return P4RuntimeOuterClass.ActionProfileMember.newBuilder()
72 .setActionProfileId(browser.actionProfiles()
73 .getByName(actProfId.id())
74 .getPreamble().getId())
75 .setMemberId(memberId.id());
76 }
77
78 @Override
79 public PiActionProfileMember decode(
80 ActionProfileMember message, Object ignored,
81 PiPipeconf pipeconf, P4InfoBrowser browser)
82 throws P4InfoBrowser.NotFoundException, CodecException {
83 final PiActionProfileId actionProfileId = PiActionProfileId.of(
84 browser.actionProfiles()
85 .getById(message.getActionProfileId())
86 .getPreamble()
87 .getName());
88 return PiActionProfileMember.builder()
89 .forActionProfile(actionProfileId)
90 .withId(PiActionProfileMemberId.of(message.getMemberId()))
91 .withAction(CODECS.action().decode(
92 message.getAction(), null, pipeconf))
93 .build();
94 }
95}