Open Ethernet Networking (OpEN) API Guide and Reference Manual  3.6.0.3
pbvlan_example.py
1 /*! \file pbvlan_example.py
2  */
3 
4 import OpEN_py as OpEN
5 from OpENUtil import *
6 
7 open_ = OpENUtil()
8 
9 class pbvlanExample:
10 
11  def __init__(self, client):
12  self.client = client
13 
14  ''' Creates new Protocol-Based Group with specified group ID '''
15  def pbvlanGroupCreate(self, groupId):
16  result = OpEN.openapiPbVlanGroupCreate(self.client, groupId)
17  if result == OpEN.OPEN_E_NONE:
18  print "The new Protocol-Based VLAN group with specified ID (%d) has been successfully created. (result = %d)" % (groupId, result)
19  elif result == OpEN.OPEN_E_EXISTS:
20  print "Protocol-Based VLAN group with specified ID (%d) already exists. (result = %d)" % (groupId, result)
21  elif result == OpEN.OPEN_E_PARAM:
22  print "Invalid parameter was passed. (result = %d)" % (result)
23  else:
24  print "Failed to created new Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (groupId, result)
25 
26  return result
27 
28  ''' Deletes Protocol-Based Group with specified group ID '''
29  def pbvlanGroupDelete(self, groupId):
30  result = OpEN.openapiPbVlanGroupDelete(self.client, groupId)
31  if result == OpEN.OPEN_E_NONE:
32  print "Protocol-Based VLAN group with specified ID (%d) has been successfully deleted. (result = %d)" % (groupId, result)
33  elif result == OpEN.OPEN_E_NOT_FOUND:
34  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
35  elif result == OpEN.OPEN_E_PARAM:
36  print "Invalid parameter was passed. (result = %d)" % (result)
37  else:
38  print "Failed to delete Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (groupId, result)
39 
40  return
41 
42  ''' Demonstrates how to set/get a name of the Protocol-Based Group with specified group ID '''
43  def pbvlanNameExample(self, groupId, groupName):
44  result = self.pbvlanGroupCreate(groupId)
45  if (result != OpEN.OPEN_E_NONE) and (result != OpEN.OPEN_E_EXISTS):
46  return
47 
48  buffdesc = OpEN.open_buffdesc()
49  try:
50  buf_set = open_.getStringBuffer(len(groupName) + 1, groupName)
51  except OpENBufferSizeError:
52  print("pbvlanNameExample: getStringBuffer raised OpENBufferSizeError")
53  return
54  except TypeError:
55  print("pbvlanNameExample: getStringBuffer raised TypeError")
56  return
57  buffdesc.pstart = buf_set
58  buffdesc.size = len(groupName) + 1
59 
60  result = OpEN.openapiPbVlanGroupNameSet(self.client, groupId, buffdesc)
61  if result == OpEN.OPEN_E_NONE:
62  print "The name (%s) of the Protocol-Based VLAN group with specified ID (%d) has been successfully set. (result = %d)" % (buf_set.cast(), groupId, result)
63  elif result == OpEN.OPEN_E_NOT_FOUND:
64  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
65  elif result == OpEN.OPEN_E_PARAM:
66  print "Invalid parameter was passed. (result = %d)" % (result)
67  else:
68  print "Failed to set the name (%s) of the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (buf_set.cast(), groupId, result)
69 
70  if result == OpEN.OPEN_E_NONE:
71  group_name = OpEN.open_buffdesc()
72  group_name.size = OpEN.OPENAPI_PBVLAN_MAX_GROUP_NAME + 1
73  try:
74  buf_get = open_.getStringBuffer(group_name.size)
75  except OpENBufferSizeError:
76  print("pbvlanNameExample: getStringBuffer raised OpENBufferSizeError")
77  return
78  except TypeError:
79  print("pbvlanNameExample: getStringBuffer raised TypeError")
80  return
81  group_name.pstart = buf_get
82 
83  result = OpEN.openapiPbVlanGroupNameGet(self.client, groupId, group_name)
84  if result == OpEN.OPEN_E_NONE:
85  print "The name of the Protocol-Based VLAN group with specified ID (%d) is %s. (result = %d)" % (groupId, buf_get.cast(), result)
86  elif result == OpEN.OPEN_E_NOT_FOUND:
87  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
88  elif result == OpEN.OPEN_E_PARAM:
89  print "Invalid parameter was passed. (result = %d)" % (result)
90  else:
91  print "Failed to get the name of the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (groupId, result)
92 
93  self.pbvlanGroupDelete(groupId)
94  return
95 
96 
97  ''' Demonstrates how to add/get/delete VLAN ID of the Protocol-Based Group with specified group ID '''
98  def pbvlanVlanExample(self, groupId, vlanId):
99  result = self.pbvlanGroupCreate(groupId)
100  if (result != OpEN.OPEN_E_NONE) and (result != OpEN.OPEN_E_EXISTS):
101  return
102 
103  result = OpEN.openapiPbVlanGroupVlanIDAdd(self.client, groupId, vlanId)
104  if result == OpEN.OPEN_E_NONE:
105  print "The VLAN (%d) has been successfully added to the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (vlanId, groupId, result)
106  elif result == OpEN.OPEN_E_NOT_FOUND:
107  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
108  elif result == OpEN.OPEN_E_PARAM:
109  print "Invalid parameter was passed. (result = %d)" % (str.cast(), result)
110  else:
111  print "Failed to add the VLAN (%d) to the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (vlanId, groupId, result)
112 
113  if result == OpEN.OPEN_E_NONE:
114  newVlanId_p = OpEN.new_uint32_tp()
115 
116  result = OpEN.openapiPbVlanGroupVlanIDGet(self.client, groupId, newVlanId_p)
117  if result == OpEN.OPEN_E_NONE:
118  print "The VLAN ID of the Protocol-Based VLAN group with specified ID (%d) is %d. (result = %d)" % (groupId, OpEN.uint32_tp_value(newVlanId_p), result)
119  elif result == OpEN.OPEN_E_NOT_FOUND:
120  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
121  elif result == OpEN.OPEN_E_PARAM:
122  print "Invalid parameter was passed. (result = %d)" % (result)
123  else:
124  print "Failed to get the VLAN ID of the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (groupId, result)
125 
126  result = OpEN.openapiPbVlanGroupVlanIDDelete(self.client, groupId, vlanId)
127  if result == OpEN.OPEN_E_NONE:
128  print "The VLAN (%d) has been successfully deleted from the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (vlanId, groupId, result)
129  elif result == OpEN.OPEN_E_NOT_FOUND:
130  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
131  elif result == OpEN.OPEN_E_ERROR:
132  print "VLAN (%d) was not found in the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (vlanId, groupId, result)
133  elif result == OpEN.OPEN_E_PARAM:
134  print "Invalid parameter was passed. (result = %d)" % (result)
135  else:
136  print "Failed to delete the VLAN (%d) from the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (vlanId, groupId, result)
137 
138  OpEN.delete_uint32_tp(newVlanId_p)
139 
140  self.pbvlanGroupDelete(groupId)
141  return
142 
143  ''' Demonstrates how to add/get/delete a protocol of the Protocol-Based Group with specified group ID '''
144  def pbvlanProtocolExample(self, groupId, prot, protType):
145  result = self.pbvlanGroupCreate(groupId)
146  if (result != OpEN.OPEN_E_NONE) and (result != OpEN.OPEN_E_EXISTS):
147  return
148 
149  result = OpEN.openapiPbVlanGroupProtocolAdd(self.client, groupId, prot, protType)
150  if result == OpEN.OPEN_E_NONE:
151  print "The protocol (%d) has been successfully added to the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (prot, groupId, result)
152  elif result == OpEN.OPEN_E_NOT_FOUND:
153  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
154  elif result == OpEN.OPEN_E_EXISTS:
155  print "Protocol (%d) is already added to the group with specified ID (%d). (result = %d)" % (prot, groupId, result)
156  elif result == OpEN.OPEN_E_PARAM:
157  print "Invalid parameter was passed. (result = %d)" % (result)
158  else:
159  print "Failed to add the protocol (%d) to the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (prot, groupId, result)
160 
161  if (result == OpEN.OPEN_E_NONE) or (result == OpEN.OPEN_E_EXISTS):
162  newProt_p = OpEN.new_uint32_tp()
163 
164  result = OpEN.openapiPbVlanGroupProtocolGetNext(self.client, groupId, 0, newProt_p)
165  if result == OpEN.OPEN_E_NONE:
166  print "The first protocol of the Protocol-Based VLAN group with specified ID (%d) is %d. (result = %d)" % (groupId, OpEN.uint32_tp_value(newProt_p), result)
167  elif result == OpEN.OPEN_E_NOT_FOUND:
168  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
169  elif result == OpEN.OPEN_E_PARAM:
170  print "Invalid parameter was passed. (result = %d)" % (result)
171  else:
172  print "Failed to get the first protocol of the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (groupId, result)
173 
174  result = OpEN.openapiPbVlanGroupProtocolDelete(self.client, groupId, prot)
175  if result == OpEN.OPEN_E_NONE:
176  print "The protocol (%d) has been successfully deleted from the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (prot, groupId, result)
177  elif result == OpEN.OPEN_E_NOT_FOUND:
178  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
179  elif result == OpEN.OPEN_E_ERROR:
180  print "Protocol (%d) was not found in the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (prot, groupId, result)
181  elif result == OpEN.OPEN_E_PARAM:
182  print "Invalid parameter was passed. (result = %d)" % (result)
183  else:
184  print "Failed to delete the protocol (%d) from the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (prot, groupId, result)
185 
186  OpEN.delete_uint32_tp(newProt_p)
187 
188  self.pbvlanGroupDelete(groupId)
189  return
190 
191  ''' Demonstrates how to add/get/delete an interface of the Protocol-Based Group with specified group ID '''
192  def pbvlanPortExample(self, groupId, ifNum):
193  result = self.pbvlanGroupCreate(groupId)
194  if (result != OpEN.OPEN_E_NONE) and (result != OpEN.OPEN_E_EXISTS):
195  return
196 
197  result = OpEN.openapiPbVlanIntfValidate(self.client, ifNum)
198  if result == OpEN.OPEN_E_NONE:
199  print "The interface (%d) could be used for the Protocol-Based VLAN configuration. (result = %d)" % (ifNum, result)
200  elif result == OpEN.OPEN_E_PARAM:
201  print "Invalid parameter was passed. (result = %d)" % (result)
202  else:
203  print "The interface (%d) could not be used for the Protocol-Based VLAN configuration. (result = %d)" % (ifNum, result)
204 
205  result = OpEN.openapiPbVlanGroupPortAdd(self.client, groupId, ifNum)
206  if result == OpEN.OPEN_E_NONE:
207  print "The interface (%d) has been successfully added to the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (ifNum, groupId, result)
208  elif result == OpEN.OPEN_E_NOT_FOUND:
209  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
210  elif result == OpEN.OPEN_E_PARAM:
211  print "Invalid parameter was passed. (result = %d)" % (result)
212  else:
213  print "Failed to add the protocol (%d) to the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (ifNum, groupId, result)
214 
215  if result == OpEN.OPEN_E_NONE:
216  newIfNum_p = OpEN.new_uint32_tp()
217 
218  result = OpEN.openapiPbVlanGroupPortGetNext(self.client, groupId, 0, newIfNum_p)
219  if result == OpEN.OPEN_E_NONE:
220  print "The first interface of the Protocol-Based VLAN group with specified ID (%d) is %d. (result = %d)" % (groupId, OpEN.uint32_tp_value(newIfNum_p), result)
221  elif result == OpEN.OPEN_E_NOT_FOUND:
222  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
223  elif result == OpEN.OPEN_E_PARAM:
224  print "Invalid parameter was passed. (result = %d)" % (result)
225  else:
226  print "Failed to get the first interface of the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (groupId, result)
227 
228  result = OpEN.openapiPbVlanGroupPortDelete(self.client, groupId, ifNum)
229  if result == OpEN.OPEN_E_NONE:
230  print "The interface (%d) has been successfully deleted from the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (ifNum, groupId, result)
231  elif result == OpEN.OPEN_E_NOT_FOUND:
232  print "Protocol-Based VLAN group with specified ID (%d) was not found. (result = %d)" % (groupId, result)
233  elif result == OpEN.OPEN_E_ERROR:
234  print "Interface (%d) was not found in the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (ifNum, groupId, result)
235  elif result == OpEN.OPEN_E_PARAM:
236  print "Invalid parameter was passed. (result = %d)" % (result)
237  else:
238  print "Failed to delete the protocol (%d) from the Protocol-Based VLAN group with specified ID (%d). (result = %d)" % (ifNum, groupId, result)
239 
240  OpEN.delete_uint32_tp(newIfNum_p)
241 
242  self.pbvlanGroupDelete(groupId)
243  return
244 
245 def main():
246  ret = open_.connect("pbvlan_example")
247  if ret == OpEN.OPEN_E_NONE :
248  open_.getNetworkOSVersion()
249  client = open_.get_client()
250 
251  example = pbvlanExample(client)
252  example.pbvlanNameExample(10, "test_name")
253  example.pbvlanVlanExample(10, 10)
254  example.pbvlanProtocolExample(10, 0x0800, OpEN.OPENAPI_PBVLAN_NUM_PROT_TYPE)
255  example.pbvlanPortExample(10, 1)
256 
257  open_.terminate()
258  else :
259  print "Unable to connect"
260 
261 if __name__ == '__main__': main()