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