blob: 5f1cb282726b4dbb0456746916f036524d9ba279 [file] [log] [blame]
Yi Tseng43ee7e82018-04-12 16:37:34 +08001#!/usr/bin/env python2.7
2# -*- utf-8 -*-
3import argparse
Yi Tseng43ee7e82018-04-12 16:37:34 +08004import google.protobuf.text_format as tf
Carmelo Casconeb5324e72018-11-25 02:26:32 -08005import re
Yi Tseng13c27f12018-06-23 01:08:55 +08006from p4.config.v1 import p4info_pb2
Yi Tseng43ee7e82018-04-12 16:37:34 +08007
Yi Tseng43ee7e82018-04-12 16:37:34 +08008copyright = '''/*
9 * Copyright 2017-present Open Networking Foundation
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23'''
24
25imports = '''
26import org.onosproject.net.pi.model.PiActionId;
27import org.onosproject.net.pi.model.PiActionParamId;
28import org.onosproject.net.pi.model.PiActionProfileId;
Daniele Moroa354b672019-08-21 17:22:50 -070029import org.onosproject.net.pi.model.PiMeterId;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080030import org.onosproject.net.pi.model.PiPacketMetadataId;
Yi Tseng43ee7e82018-04-12 16:37:34 +080031import org.onosproject.net.pi.model.PiCounterId;
32import org.onosproject.net.pi.model.PiMatchFieldId;
33import org.onosproject.net.pi.model.PiTableId;'''
34
35PKG_FMT = 'package org.onosproject.pipelines.%s;'
36
37CLASS_OPEN = 'public final class %s {'
38CLASS_CLOSE = '}'
39
40DEFAULT_CONSTRUCTOR = '''
41 // hide default constructor
42 private %s() {
43 }
44'''
45
46CONST_FMT = ' public static final %s %s = %s;'
47SHORT_CONST_FMT =''' public static final %s %s =
48 %s;'''
49JAVA_STR = 'String'
50EMPTY_STR = ''
51JAVA_DOC_FMT = '''/**
52 * Constants for %s pipeline.
53 */'''
54
55
56PI_HF_FIELD_ID = 'PiMatchFieldId'
57PI_HF_FIELD_ID_CST = 'PiMatchFieldId.of("%s")'
58
59PI_TBL_ID = 'PiTableId'
60PI_TBL_ID_CST = 'PiTableId.of("%s")'
61
62PI_CTR_ID = 'PiCounterId'
63PI_CTR_ID_CST = 'PiCounterId.of("%s")'
64
65PI_ACT_ID = 'PiActionId'
66PI_ACT_ID_CST = 'PiActionId.of("%s")'
67
68PI_ACT_PRM_ID = 'PiActionParamId'
69PI_ACT_PRM_ID_CST = 'PiActionParamId.of("%s")'
70
71PI_ACT_PROF_ID = 'PiActionProfileId'
72PI_ACT_PROF_ID_CST = 'PiActionProfileId.of("%s")'
73
Carmelo Cascone4c289b72019-01-22 15:30:45 -080074PI_PKT_META_ID = 'PiPacketMetadataId'
75PI_PKT_META_ID_CST = 'PiPacketMetadataId.of("%s")'
Yi Tseng43ee7e82018-04-12 16:37:34 +080076
Daniele Moroa354b672019-08-21 17:22:50 -070077PI_METER_ID = 'PiMeterId'
78PI_METER_ID_CST = 'PiMeterId.of("%s")'
79
Carmelo Casconeb5324e72018-11-25 02:26:32 -080080HF_VAR_PREFIX = 'HDR_'
81
82
Yi Tseng43ee7e82018-04-12 16:37:34 +080083class ConstantClassGenerator(object):
84 headers = set()
85 header_fields = set()
86 tables = set()
87 counters = set()
88 direct_counters = set()
89 actions = set()
90 action_params = set()
91 action_profiles = set()
92 packet_metadata = set()
Daniele Moroa354b672019-08-21 17:22:50 -070093 meters = set()
Yi Tseng43ee7e82018-04-12 16:37:34 +080094
95 # https://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case
96 def convert_camel_to_all_caps(self, name):
97 s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
98 s1 = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper()
99 return s1.replace('.', '_')
100
101 def __init__(self, base_name):
102
103 self.class_name = base_name.title() + 'Constants'
104 self.package_name = PKG_FMT % (base_name, )
105 self.java_doc = JAVA_DOC_FMT % (base_name, )
106
107 def parse(self, p4info):
108 for tbl in p4info.tables:
109 for mf in tbl.match_fields:
110 self.header_fields.add(mf.name)
111
112 self.tables.add(tbl.preamble.name)
113
114 for ctr in p4info.counters:
115 self.counters.add(ctr.preamble.name)
116
117 for dir_ctr in p4info.direct_counters:
118 self.direct_counters.add(dir_ctr.preamble.name)
119
120 for act in p4info.actions:
121 self.actions.add(act.preamble.name)
122
123 for param in act.params:
124 self.action_params.add(param.name)
125
126 for act_prof in p4info.action_profiles:
127 self.action_profiles.add(act_prof.preamble.name)
128
129 for cpm in p4info.controller_packet_metadata:
130 for mta in cpm.metadata:
131 self.packet_metadata.add(mta.name)
Daniele Moroa354b672019-08-21 17:22:50 -0700132 for mtr in p4info.meters:
133 self.meters.add(mtr.preamble.name)
Yi Tseng43ee7e82018-04-12 16:37:34 +0800134
135 def const_line(self, name, type, constructor):
136 var_name = self.convert_camel_to_all_caps(name)
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800137 if type == PI_HF_FIELD_ID:
138 var_name = HF_VAR_PREFIX + var_name
Yi Tseng43ee7e82018-04-12 16:37:34 +0800139 val = constructor % (name, )
140
141 line = CONST_FMT % (type, var_name, val)
142 if len(line) > 80:
143 line = SHORT_CONST_FMT % (type, var_name, val)
144 return line
145
146 def generate_java(self):
147 lines = list()
148 lines.append(copyright)
149 lines.append(self.package_name)
150 lines.append(imports)
151 lines.append(self.java_doc)
152 # generate the class
153 lines.append(CLASS_OPEN % (self.class_name, ))
154 lines.append(DEFAULT_CONSTRUCTOR % (self.class_name, ))
155
156 if len(self.header_fields) is not 0:
157 lines.append(' // Header field IDs')
158 for hf in self.header_fields:
159 lines.append(self.const_line(hf, PI_HF_FIELD_ID, PI_HF_FIELD_ID_CST))
160
161 if len(self.tables) is not 0:
162 lines.append(' // Table IDs')
163 for tbl in self.tables:
164 lines.append(self.const_line(tbl, PI_TBL_ID, PI_TBL_ID_CST))
165
166 if len(self.counters) is not 0:
167 lines.append(' // Indirect Counter IDs')
168 for ctr in self.counters:
169 lines.append(self.const_line(ctr, PI_CTR_ID, PI_CTR_ID_CST))
170
171 if len(self.direct_counters) is not 0:
172 lines.append(' // Direct Counter IDs')
173 for dctr in self.direct_counters:
174 lines.append(self.const_line(dctr, PI_CTR_ID, PI_CTR_ID_CST))
175
176 if len(self.actions) is not 0:
177 lines.append(' // Action IDs')
178 for act in self.actions:
179 lines.append(self.const_line(act, PI_ACT_ID, PI_ACT_ID_CST))
180
181 if len(self.action_params) is not 0:
182 lines.append(' // Action Param IDs')
183 for act_prm in self.action_params:
184 lines.append(self.const_line(act_prm, PI_ACT_PRM_ID, PI_ACT_PRM_ID_CST))
185
186 if len(self.action_profiles) is not 0:
187 lines.append(' // Action Profile IDs')
188 for act_prof in self.action_profiles:
189 lines.append(self.const_line(act_prof, PI_ACT_PROF_ID, PI_ACT_PROF_ID_CST))
190
191 if len(self.packet_metadata) is not 0:
192 lines.append(' // Packet Metadata IDs')
193 for pmeta in self.packet_metadata:
Daniele Moroa354b672019-08-21 17:22:50 -0700194 if not pmeta.startswith("_"):
195 lines.append(self.const_line(pmeta, PI_PKT_META_ID, PI_PKT_META_ID_CST))
196
197 if len(self.meters) is not 0:
198 lines.append(' // Meter IDs')
199 for mtr in self.meters:
200 lines.append(self.const_line(mtr, PI_METER_ID, PI_METER_ID_CST))
Yi Tseng43ee7e82018-04-12 16:37:34 +0800201 lines.append(CLASS_CLOSE)
202 # end of class
203
204 return '\n'.join(lines)
205
206
207def main():
208 parser = argparse.ArgumentParser(prog='onos-gen-p4-constants',
209 description='ONOS P4Info to Java constant generator.')
210 parser.add_argument('name', help='Name of the constant, will be used as class name')
211 parser.add_argument('p4info', help='P4Info file')
212 parser.add_argument('-o', '--output', help='output path', default='-')
213 args = parser.parse_args()
214
215 base_name = args.name
216 file_name = args.p4info
217 p4info = p4info_pb2.P4Info()
218 with open(file_name, 'r') as intput_file:
219 s = intput_file.read()
220 tf.Merge(s, p4info)
221
222 gen = ConstantClassGenerator(base_name)
223 gen.parse(p4info)
224
225 java_code = gen.generate_java()
226
227 if args.output == '-':
228 # std output
229 print java_code
230 else:
231 with open(args.output, 'w') as output_file:
232 output_file.write(java_code)
233
234
235if __name__ == '__main__':
236 main()