blob: 30dd43c07af33107314ff975c36b532b4ad59d51 [file] [log] [blame]
/*
* Copyright 2017-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.p4runtime.ctl;
import org.onosproject.net.pi.model.PiActionProfileId;
import org.onosproject.net.pi.model.PiPipeconf;
import org.onosproject.net.pi.runtime.PiActionProfileMember;
import org.onosproject.net.pi.runtime.PiActionProfileMemberId;
import p4.config.v1.P4InfoOuterClass;
import p4.v1.P4RuntimeOuterClass;
import p4.v1.P4RuntimeOuterClass.ActionProfileMember;
import static java.lang.String.format;
import static org.onosproject.p4runtime.ctl.TableEntryEncoder.decodeActionMsg;
import static org.onosproject.p4runtime.ctl.TableEntryEncoder.encodePiAction;
/**
* Encoder/Decoder of action profile member.
*/
final class ActionProfileMemberEncoder {
private ActionProfileMemberEncoder() {
// Hide default constructor
}
/**
* Encode a PiActionProfileMember to a ActionProfileMember.
*
* @param member the member to encode
* @param pipeconf the pipeconf, as encode spec
* @return encoded member
* @throws P4InfoBrowser.NotFoundException can't find action profile from
* P4Info browser
* @throws EncodeException can't find P4Info from pipeconf
*/
static ActionProfileMember encode(PiActionProfileMember member,
PiPipeconf pipeconf)
throws P4InfoBrowser.NotFoundException, EncodeException {
P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
if (browser == null) {
throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
}
ActionProfileMember.Builder actionProfileMemberBuilder =
ActionProfileMember.newBuilder();
// member id
actionProfileMemberBuilder.setMemberId(member.id().id());
// action profile id
P4InfoOuterClass.ActionProfile actionProfile =
browser.actionProfiles().getByName(member.actionProfile().id());
int actionProfileId = actionProfile.getPreamble().getId();
actionProfileMemberBuilder.setActionProfileId(actionProfileId);
// Action
P4RuntimeOuterClass.Action action = encodePiAction(member.action(), browser);
actionProfileMemberBuilder.setAction(action);
return actionProfileMemberBuilder.build();
}
/**
* Decode an action profile member to PI action profile member.
*
* @param member the action profile member
* @param weight the weight of the member
* @param pipeconf the pipeconf, as decode spec
* @return decoded PI action profile member
* @throws P4InfoBrowser.NotFoundException can't find definition of action
* from P4 info
* @throws EncodeException can't get P4 info browser from
* pipeconf
*/
static PiActionProfileMember decode(ActionProfileMember member,
int weight,
PiPipeconf pipeconf)
throws P4InfoBrowser.NotFoundException, EncodeException {
P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
if (browser == null) {
throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
}
final PiActionProfileId actionProfileId = PiActionProfileId.of(
browser.actionProfiles()
.getById(member.getActionProfileId())
.getPreamble()
.getName());
return PiActionProfileMember.builder()
.forActionProfile(actionProfileId)
.withId(PiActionProfileMemberId.of(member.getMemberId()))
.withWeight(weight)
.withAction(decodeActionMsg(member.getAction(), browser))
.build();
}
}