Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 16 | |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 17 | import ply.lex as lex |
| 18 | import ply.yacc as yacc |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 19 | import plyxproto.model as m |
| 20 | from plyxproto.helpers import Visitor |
| 21 | from plyxproto.logicparser import FOLLexer, FOLParser |
| 22 | from six.moves import map |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 23 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 24 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 25 | class Stack(list): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 26 | def push(self, x): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 27 | self.append(x) |
| 28 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 29 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 30 | def str_to_dict(s): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 31 | lst = s.rsplit(".", 1) |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 32 | name = lst[-1] |
| 33 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 34 | if len(lst) == 2: |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 35 | package = lst[0] |
| 36 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 37 | package = "" |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 38 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 39 | return {"name": name, "package": package, "fqn": s} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 40 | |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 41 | |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 42 | def replace_link(obj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 43 | try: |
| 44 | link = obj.link |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 45 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 46 | through = link["through"] |
| 47 | except KeyError: |
| 48 | through = None |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 49 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 50 | try: |
| 51 | through_str = through[1:-1] |
| 52 | except TypeError: |
| 53 | through_str = None |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 54 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 55 | if through_str: |
| 56 | through_dict = str_to_dict(through_str) |
| 57 | else: |
| 58 | through_dict = {} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 59 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 60 | model_dict = str_to_dict(link["model"][1:-1]) |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 61 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 62 | ls = m.LinkSpec( |
| 63 | obj, |
| 64 | m.LinkDefinition( |
| 65 | link["link"][1:-1], |
| 66 | obj.name, |
| 67 | model_dict, |
| 68 | link["port"][1:-1], |
| 69 | through_dict, |
| 70 | ), |
| 71 | ) |
| 72 | return ls |
| 73 | except BaseException: |
| 74 | return obj |
| 75 | |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 76 | |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 77 | class Proto2XProto(Visitor): |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 78 | fol_lexer = lex.lex(module=FOLLexer()) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 79 | fol_parser = yacc.yacc(module=FOLParser(), start="goal", debug=0, outputdir="/tmp") |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 80 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 81 | def __init__(self): |
| 82 | super(Proto2XProto, self).__init__() |
| 83 | |
| 84 | self.stack = Stack() |
| 85 | self.count_stack = Stack() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 86 | self.content = "" |
| 87 | self.offset = 0 |
| 88 | self.statementsChanged = 0 |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 89 | self.message_options = {} |
| 90 | self.options = {} |
| 91 | self.current_message_name = None |
| 92 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 93 | self.xproto_message_options = ["bases"] |
| 94 | self.xproto_field_options = ["model"] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 95 | self.verbose = 0 |
| 96 | self.first_field = True |
| 97 | self.first_method = True |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 98 | |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 99 | def replace_policy(self, obj): |
| 100 | if isinstance(obj, m.OptionStatement): |
| 101 | rhs = obj.value.value.pval |
| 102 | if rhs.startswith('"') and rhs.endswith('"'): |
| 103 | rhs = rhs[1:-1] |
| 104 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 105 | if rhs.startswith("policy:"): |
| 106 | str = rhs.split(":", 1)[1] |
| 107 | val = self.fol_parser.parse(str, lexer=self.fol_lexer) |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 108 | |
| 109 | return m.PolicyDefinition(obj.name, val) |
| 110 | |
| 111 | return obj |
| 112 | |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 113 | def proto_to_xproto_field(self, obj): |
| 114 | try: |
| 115 | opts = {} |
| 116 | for fd in obj.fieldDirective: |
| 117 | k = fd.pval.name.value.pval |
| 118 | v = fd.pval.value.value.pval |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 119 | opts[k] = v |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 120 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 121 | if "model" in opts and "link" in opts and "port" in opts: |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 122 | obj.link = opts |
| 123 | pass |
| 124 | except KeyError: |
| 125 | raise |
| 126 | |
| 127 | def proto_to_xproto_message(self, obj): |
| 128 | try: |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 129 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 130 | bases = self.message_options["bases"].split(",") |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 131 | except KeyError: |
| 132 | bases = [] |
| 133 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 134 | bases = [str_to_dict(x[1:-1]) for x in bases] |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 135 | obj.bases = bases |
| 136 | except KeyError: |
| 137 | raise |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 138 | |
| 139 | def map_field(self, obj, s): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 140 | if "model" in s: |
| 141 | link = m.LinkDefinition( |
| 142 | "onetoone", "src", "name", "dst", obj.linespan, obj.lexspan, obj.p |
| 143 | ) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 144 | lspec = m.LinkSpec(link, obj) |
| 145 | else: |
| 146 | lspec = obj |
| 147 | return lspec |
| 148 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 149 | def get_stack(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 150 | return self.stack |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 151 | |
| 152 | def visit_PackageStatement(self, obj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 153 | """Ignore""" |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 154 | return True |
| 155 | |
| 156 | def visit_ImportStatement(self, obj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 157 | """Ignore""" |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 158 | return True |
| 159 | |
| 160 | def visit_OptionStatement(self, obj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 161 | if self.current_message_name: |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 162 | k = obj.name.value.pval |
| 163 | self.message_options[k] = obj.value.value.pval |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 164 | if k in self.xproto_message_options: |
| 165 | obj.mark_for_deletion = True |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 166 | else: |
| 167 | self.options[obj.name.value.pval] = obj.value.value.pval |
| 168 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 169 | return True |
| 170 | |
| 171 | def visit_LU(self, obj): |
| 172 | return True |
| 173 | |
| 174 | def visit_default(self, obj): |
| 175 | return True |
| 176 | |
| 177 | def visit_FieldDirective(self, obj): |
| 178 | return True |
| 179 | |
| 180 | def visit_FieldDirective_post(self, obj): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 181 | return True |
| 182 | |
| 183 | def visit_FieldType(self, obj): |
| 184 | return True |
| 185 | |
| 186 | def visit_LinkDefinition(self, obj): |
| 187 | return True |
| 188 | |
| 189 | def visit_FieldDefinition(self, obj): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 190 | return True |
| 191 | |
| 192 | def visit_FieldDefinition_post(self, obj): |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 193 | self.proto_to_xproto_field(obj) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 194 | return True |
| 195 | |
| 196 | def visit_EnumFieldDefinition(self, obj): |
| 197 | return True |
| 198 | |
| 199 | def visit_EnumDefinition(self, obj): |
| 200 | return True |
| 201 | |
| 202 | def visit_MessageDefinition(self, obj): |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 203 | self.current_message_name = obj.name.value.pval |
| 204 | self.message_options = {} |
| 205 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 206 | return True |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 207 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 208 | def visit_MessageDefinition_post(self, obj): |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 209 | self.proto_to_xproto_message(obj) |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 210 | obj.body = [x for x in obj.body if not hasattr(x, "mark_for_deletion")] |
| 211 | obj.body = list(map(replace_link, obj.body)) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 212 | |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 213 | self.current_message_name = None |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 214 | return True |
| 215 | |
| 216 | def visit_MessageExtension(self, obj): |
| 217 | return True |
| 218 | |
| 219 | def visit_MethodDefinition(self, obj): |
| 220 | return True |
| 221 | |
| 222 | def visit_ServiceDefinition(self, obj): |
| 223 | return True |
| 224 | |
| 225 | def visit_ExtensionsDirective(self, obj): |
| 226 | return True |
| 227 | |
| 228 | def visit_Literal(self, obj): |
| 229 | return True |
| 230 | |
| 231 | def visit_Name(self, obj): |
| 232 | return True |
| 233 | |
| 234 | def visit_DotName(self, obj): |
| 235 | return True |
| 236 | |
| 237 | def visit_Proto(self, obj): |
| 238 | self.count_stack.push(len(obj.body)) |
| 239 | return True |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 240 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 241 | def visit_Proto_post(self, obj): |
Sapan Bhatia | ea6ff75 | 2017-07-14 01:52:18 -0400 | [diff] [blame] | 242 | |
| 243 | obj.body = [self.replace_policy(o) for o in obj.body] |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 244 | return True |
| 245 | |
| 246 | def visit_LinkSpec(self, obj): |
| 247 | return False |