blob: 80f8f63d400fc84565205b26c639e9930f56f557 [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;
29import org.onosproject.net.pi.model.PiControlMetadataId;
30import org.onosproject.net.pi.model.PiCounterId;
31import org.onosproject.net.pi.model.PiMatchFieldId;
32import org.onosproject.net.pi.model.PiTableId;'''
33
34PKG_FMT = 'package org.onosproject.pipelines.%s;'
35
36CLASS_OPEN = 'public final class %s {'
37CLASS_CLOSE = '}'
38
39DEFAULT_CONSTRUCTOR = '''
40 // hide default constructor
41 private %s() {
42 }
43'''
44
45CONST_FMT = ' public static final %s %s = %s;'
46SHORT_CONST_FMT =''' public static final %s %s =
47 %s;'''
48JAVA_STR = 'String'
49EMPTY_STR = ''
50JAVA_DOC_FMT = '''/**
51 * Constants for %s pipeline.
52 */'''
53
54
55PI_HF_FIELD_ID = 'PiMatchFieldId'
56PI_HF_FIELD_ID_CST = 'PiMatchFieldId.of("%s")'
57
58PI_TBL_ID = 'PiTableId'
59PI_TBL_ID_CST = 'PiTableId.of("%s")'
60
61PI_CTR_ID = 'PiCounterId'
62PI_CTR_ID_CST = 'PiCounterId.of("%s")'
63
64PI_ACT_ID = 'PiActionId'
65PI_ACT_ID_CST = 'PiActionId.of("%s")'
66
67PI_ACT_PRM_ID = 'PiActionParamId'
68PI_ACT_PRM_ID_CST = 'PiActionParamId.of("%s")'
69
70PI_ACT_PROF_ID = 'PiActionProfileId'
71PI_ACT_PROF_ID_CST = 'PiActionProfileId.of("%s")'
72
73PI_PKT_META_ID = 'PiControlMetadataId'
74PI_PKT_META_ID_CST = 'PiControlMetadataId.of("%s")'
75
Carmelo Casconeb5324e72018-11-25 02:26:32 -080076HF_VAR_PREFIX = 'HDR_'
77
78
Yi Tseng43ee7e82018-04-12 16:37:34 +080079class ConstantClassGenerator(object):
80 headers = set()
81 header_fields = set()
82 tables = set()
83 counters = set()
84 direct_counters = set()
85 actions = set()
86 action_params = set()
87 action_profiles = set()
88 packet_metadata = set()
89
90 # https://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case
91 def convert_camel_to_all_caps(self, name):
92 s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
93 s1 = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper()
94 return s1.replace('.', '_')
95
96 def __init__(self, base_name):
97
98 self.class_name = base_name.title() + 'Constants'
99 self.package_name = PKG_FMT % (base_name, )
100 self.java_doc = JAVA_DOC_FMT % (base_name, )
101
102 def parse(self, p4info):
103 for tbl in p4info.tables:
104 for mf in tbl.match_fields:
105 self.header_fields.add(mf.name)
106
107 self.tables.add(tbl.preamble.name)
108
109 for ctr in p4info.counters:
110 self.counters.add(ctr.preamble.name)
111
112 for dir_ctr in p4info.direct_counters:
113 self.direct_counters.add(dir_ctr.preamble.name)
114
115 for act in p4info.actions:
116 self.actions.add(act.preamble.name)
117
118 for param in act.params:
119 self.action_params.add(param.name)
120
121 for act_prof in p4info.action_profiles:
122 self.action_profiles.add(act_prof.preamble.name)
123
124 for cpm in p4info.controller_packet_metadata:
125 for mta in cpm.metadata:
126 self.packet_metadata.add(mta.name)
127
128 def const_line(self, name, type, constructor):
129 var_name = self.convert_camel_to_all_caps(name)
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800130 if type == PI_HF_FIELD_ID:
131 var_name = HF_VAR_PREFIX + var_name
Yi Tseng43ee7e82018-04-12 16:37:34 +0800132 val = constructor % (name, )
133
134 line = CONST_FMT % (type, var_name, val)
135 if len(line) > 80:
136 line = SHORT_CONST_FMT % (type, var_name, val)
137 return line
138
139 def generate_java(self):
140 lines = list()
141 lines.append(copyright)
142 lines.append(self.package_name)
143 lines.append(imports)
144 lines.append(self.java_doc)
145 # generate the class
146 lines.append(CLASS_OPEN % (self.class_name, ))
147 lines.append(DEFAULT_CONSTRUCTOR % (self.class_name, ))
148
149 if len(self.header_fields) is not 0:
150 lines.append(' // Header field IDs')
151 for hf in self.header_fields:
152 lines.append(self.const_line(hf, PI_HF_FIELD_ID, PI_HF_FIELD_ID_CST))
153
154 if len(self.tables) is not 0:
155 lines.append(' // Table IDs')
156 for tbl in self.tables:
157 lines.append(self.const_line(tbl, PI_TBL_ID, PI_TBL_ID_CST))
158
159 if len(self.counters) is not 0:
160 lines.append(' // Indirect Counter IDs')
161 for ctr in self.counters:
162 lines.append(self.const_line(ctr, PI_CTR_ID, PI_CTR_ID_CST))
163
164 if len(self.direct_counters) is not 0:
165 lines.append(' // Direct Counter IDs')
166 for dctr in self.direct_counters:
167 lines.append(self.const_line(dctr, PI_CTR_ID, PI_CTR_ID_CST))
168
169 if len(self.actions) is not 0:
170 lines.append(' // Action IDs')
171 for act in self.actions:
172 lines.append(self.const_line(act, PI_ACT_ID, PI_ACT_ID_CST))
173
174 if len(self.action_params) is not 0:
175 lines.append(' // Action Param IDs')
176 for act_prm in self.action_params:
177 lines.append(self.const_line(act_prm, PI_ACT_PRM_ID, PI_ACT_PRM_ID_CST))
178
179 if len(self.action_profiles) is not 0:
180 lines.append(' // Action Profile IDs')
181 for act_prof in self.action_profiles:
182 lines.append(self.const_line(act_prof, PI_ACT_PROF_ID, PI_ACT_PROF_ID_CST))
183
184 if len(self.packet_metadata) is not 0:
185 lines.append(' // Packet Metadata IDs')
186 for pmeta in self.packet_metadata:
187 lines.append(self.const_line(pmeta, PI_PKT_META_ID, PI_PKT_META_ID_CST))
188 lines.append(CLASS_CLOSE)
189 # end of class
190
191 return '\n'.join(lines)
192
193
194def main():
195 parser = argparse.ArgumentParser(prog='onos-gen-p4-constants',
196 description='ONOS P4Info to Java constant generator.')
197 parser.add_argument('name', help='Name of the constant, will be used as class name')
198 parser.add_argument('p4info', help='P4Info file')
199 parser.add_argument('-o', '--output', help='output path', default='-')
200 args = parser.parse_args()
201
202 base_name = args.name
203 file_name = args.p4info
204 p4info = p4info_pb2.P4Info()
205 with open(file_name, 'r') as intput_file:
206 s = intput_file.read()
207 tf.Merge(s, p4info)
208
209 gen = ConstantClassGenerator(base_name)
210 gen.parse(p4info)
211
212 java_code = gen.generate_java()
213
214 if args.output == '-':
215 # std output
216 print java_code
217 else:
218 with open(args.output, 'w') as output_file:
219 output_file.write(java_code)
220
221
222if __name__ == '__main__':
223 main()