packetforge: add packetforge for generic flow to extras
[vpp.git] / extras / packetforge / NodeField.py
1 # Copyright (c) 2022 Intel and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 import ExpressionConverter
15 from InputFormat import *
16
17
18 class NodeField:
19     def __init__(self):
20         self.DefaultValue = None
21         self.IsReserved = None
22         self.IsReadonly = None
23         self.IsAutoIncrease = None
24         self.IsIncreaseLength = None
25         self.Optional = None
26         self.VariableSize = None
27
28     def Create(token):
29         if token == None:
30             return None
31
32         field = NodeField()
33
34         if token["name"] == None:
35             return None
36         if token["size"] == None:
37             return None
38
39         # name
40         field.Name = token["name"]
41
42         if field.Name == "reserved":
43             field.IsReserved = True
44
45         inputFormat = InputFormat.bytearray
46         res, u16 = ExpressionConverter.ToNum(token["size"])
47
48         # size
49         if res:
50             field.Size = u16
51             if u16 <= 8:
52                 inputFormat = InputFormat.u8
53             elif u16 <= 16:
54                 inputFormat = InputFormat.u16
55             elif u16 <= 32:
56                 inputFormat = InputFormat.u32
57             elif u16 <= 64:
58                 inputFormat = InputFormat.u64
59             else:
60                 inputFormat = InputFormat.bytearray
61         else:
62             field.Size = 0
63             field.VariableSize = token["size"]
64
65         if "format" in token and token["format"] != None:
66             inputFormat = InputFormat[token["format"]]
67
68         field.Format = inputFormat
69
70         if "default" in token and token["default"] != None:
71             field.DefaultValue = token["default"]
72             ret, _ = ExpressionConverter.Verify(field.Format, field.DefaultValue)
73             if not ret:
74                 return None
75
76         if "readonly" in token and token["readonly"] == "true" or field.IsReserved:
77             field.IsReadonly = True
78         if "autoincrease" in token and token["autoincrease"] == "true":
79             field.IsAutoIncrease = True
80             field.IsReadonly = True
81         if "increaselength" in token and token["increaselength"] == "true":
82             field.IsIncreaseLength = True
83         if "optional" in token:
84             field.Optional = token["optional"]
85
86         return field