blob: e063faaea4e4f8e94f475a176583caebdcee7bd1 [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.PiPipeconf;
20import org.onosproject.net.pi.runtime.PiActionProfileGroup;
21import org.onosproject.net.pi.runtime.PiActionProfileMember;
Carmelo Cascone9db4d5c2019-04-16 17:36:33 -070022import org.onosproject.net.pi.runtime.PiCloneSessionEntry;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080023import org.onosproject.net.pi.runtime.PiCounterCell;
24import org.onosproject.net.pi.runtime.PiEntity;
25import org.onosproject.net.pi.runtime.PiMeterCellConfig;
26import org.onosproject.net.pi.runtime.PiMulticastGroupEntry;
Carmelo Cascone9db4d5c2019-04-16 17:36:33 -070027import org.onosproject.net.pi.runtime.PiPreEntry;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080028import org.onosproject.net.pi.runtime.PiTableEntry;
29import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
30import p4.v1.P4RuntimeOuterClass;
31
32import static java.lang.String.format;
33import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS;
34
35/**
36 * Codec for P4Runtime Entity.
37 */
38public final class EntityCodec extends AbstractCodec<PiEntity, P4RuntimeOuterClass.Entity, Object> {
39
40 @Override
41 protected P4RuntimeOuterClass.Entity encode(
42 PiEntity piEntity, Object ignored, PiPipeconf pipeconf, P4InfoBrowser browser)
43 throws CodecException {
44 final P4RuntimeOuterClass.Entity.Builder p4Entity = P4RuntimeOuterClass.Entity.newBuilder();
45 switch (piEntity.piEntityType()) {
46 case TABLE_ENTRY:
47 return p4Entity.setTableEntry(
48 CODECS.tableEntry().encode(
49 (PiTableEntry) piEntity, null, pipeconf))
50 .build();
51 case ACTION_PROFILE_GROUP:
52 return p4Entity.setActionProfileGroup(
53 CODECS.actionProfileGroup().encode(
54 (PiActionProfileGroup) piEntity, null, pipeconf))
55 .build();
56 case ACTION_PROFILE_MEMBER:
57 return p4Entity.setActionProfileMember(
58 CODECS.actionProfileMember().encode(
59 (PiActionProfileMember) piEntity, null, pipeconf))
60 .build();
Carmelo Cascone9db4d5c2019-04-16 17:36:33 -070061 case PRE_ENTRY:
62 final PiPreEntry preEntry = (PiPreEntry) piEntity;
63 switch (preEntry.preEntryType()) {
64 case MULTICAST_GROUP:
65 return p4Entity.setPacketReplicationEngineEntry(
66 P4RuntimeOuterClass.PacketReplicationEngineEntry.newBuilder()
67 .setMulticastGroupEntry(CODECS.multicastGroupEntry().encode(
68 (PiMulticastGroupEntry) piEntity, null, pipeconf))
69 .build())
70 .build();
71 case CLONE_SESSION:
72 return p4Entity.setPacketReplicationEngineEntry(
73 P4RuntimeOuterClass.PacketReplicationEngineEntry.newBuilder()
74 .setCloneSessionEntry(CODECS.cloneSessionEntry().encode(
75 (PiCloneSessionEntry) piEntity, null, pipeconf))
76 .build())
77 .build();
78 default:
79 throw new CodecException(format(
80 "Encoding of %s of type %s is not supported",
81 piEntity.piEntityType(),
82 preEntry.preEntryType()));
83 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -080084 case METER_CELL_CONFIG:
85 final PiMeterCellConfig meterCellConfig = (PiMeterCellConfig) piEntity;
86 switch (meterCellConfig.cellId().meterType()) {
87 case DIRECT:
88 return p4Entity.setDirectMeterEntry(
89 CODECS.directMeterEntry().encode(
90 meterCellConfig, null, pipeconf))
91 .build();
92 case INDIRECT:
93 return p4Entity.setMeterEntry(
94 CODECS.meterEntry().encode(
95 meterCellConfig, null, pipeconf))
96 .build();
97 default:
98 throw new CodecException(format(
99 "Encoding of %s of type %s is not supported",
100 piEntity.piEntityType(),
101 meterCellConfig.cellId().meterType()));
102 }
103 case COUNTER_CELL:
104 final PiCounterCell counterCell = (PiCounterCell) piEntity;
105 switch (counterCell.cellId().counterType()) {
106 case DIRECT:
107 return p4Entity.setDirectCounterEntry(
108 CODECS.directCounterEntry().encode(
109 counterCell, null, pipeconf))
110 .build();
111 case INDIRECT:
112 return p4Entity.setCounterEntry(
113 CODECS.counterEntry().encode(
114 counterCell, null, pipeconf))
115 .build();
116 default:
117 throw new CodecException(format(
118 "Encoding of %s of type %s is not supported",
119 piEntity.piEntityType(),
120 counterCell.cellId().counterType()));
121 }
122 case REGISTER_CELL:
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800123 default:
124 throw new CodecException(format(
125 "Encoding of %s not supported",
126 piEntity.piEntityType()));
127 }
128 }
129
130 @Override
131 protected PiEntity decode(
132 P4RuntimeOuterClass.Entity message, Object ignored, PiPipeconf pipeconf, P4InfoBrowser browser)
Carmelo Cascone9db4d5c2019-04-16 17:36:33 -0700133 throws CodecException {
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800134 switch (message.getEntityCase()) {
135 case TABLE_ENTRY:
136 return CODECS.tableEntry().decode(
137 message.getTableEntry(), null, pipeconf);
138 case ACTION_PROFILE_MEMBER:
139 return CODECS.actionProfileMember().decode(
140 message.getActionProfileMember(), null, pipeconf);
141 case ACTION_PROFILE_GROUP:
142 return CODECS.actionProfileGroup().decode(
143 message.getActionProfileGroup(), null, pipeconf);
144 case METER_ENTRY:
145 return CODECS.meterEntry().decode(
146 message.getMeterEntry(), null, pipeconf);
147 case DIRECT_METER_ENTRY:
148 return CODECS.directMeterEntry().decode(
149 message.getDirectMeterEntry(), null, pipeconf);
150 case COUNTER_ENTRY:
151 return CODECS.counterEntry().decode(
152 message.getCounterEntry(), null, pipeconf);
153 case DIRECT_COUNTER_ENTRY:
154 return CODECS.directCounterEntry().decode(
155 message.getDirectCounterEntry(), null, pipeconf);
156 case PACKET_REPLICATION_ENGINE_ENTRY:
157 switch (message.getPacketReplicationEngineEntry().getTypeCase()) {
158 case MULTICAST_GROUP_ENTRY:
159 return CODECS.multicastGroupEntry().decode(
160 message.getPacketReplicationEngineEntry()
161 .getMulticastGroupEntry(), null, pipeconf);
162 case CLONE_SESSION_ENTRY:
Carmelo Cascone9db4d5c2019-04-16 17:36:33 -0700163 return CODECS.cloneSessionEntry().decode(
164 message.getPacketReplicationEngineEntry()
165 .getCloneSessionEntry(), null, pipeconf);
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800166 case TYPE_NOT_SET:
167 default:
168 throw new CodecException(format(
169 "Decoding of %s of type %s not supported",
170 message.getEntityCase(),
171 message.getPacketReplicationEngineEntry().getTypeCase()));
172 }
173 case VALUE_SET_ENTRY:
174 case REGISTER_ENTRY:
175 case DIGEST_ENTRY:
176 case EXTERN_ENTRY:
177 case ENTITY_NOT_SET:
178 default:
179 throw new CodecException(format(
180 "Decoding of %s not supported",
181 message.getEntityCase()));
182
183 }
184 }
185}