Open Ethernet Networking (OpEN) API Guide and Reference Manual  3.11.1.2
vlan_example.py
1 /*! \file vlan_example.py
2  */
3 
4 import OpEN_py as OpEN
5 from OpENUtil import *
6 from time import time
7 from select import select
8 import fcntl
9 import os
10 import errno
11 from sys import version_info
12 
13 #
14 # Copyright 2016 Broadcom.
15 #
16 # Licensed under the Apache License, Version 2.0 (the "License");
17 # you may not use this file except in compliance with the License.
18 # You may obtain a copy of the License at
19 #
20 # http://www.apache.org/licenses/LICENSE-2.0
21 #
22 # Unless required by applicable law or agreed to in writing, software
23 # distributed under the License is distributed on an "AS IS" BASIS,
24 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 # See the License for the specific language governing permissions and
26 # limitations under the License.
27 #
28 
29 
30 #
31 # Python 2.6.6
32 #
33 
34 class Vlan :
35  def __init__(self):
36  self.m_vlan_exists = False
37  self.m_staticVlan = False
38 
39 class VlanExample :
40  def __init__(self, client) :
41  self.m_client = client
42  self.m_vlan_table = []
43 
44  def vlan_status_show(self):
45  print("\n\nThe following VLAN IDs are in the switch's VLAN database:\n")
46  size = 4096
47  if version_info >= (3,0,0):
48  for i in range(0, size - 1):
49  if self.m_vlan_table[i].vlan_exists == True :
50  res = "VLAN: " + str(i) + " is "
51  if self.m_vlan_table[i].staticVlan == True :
52  res += "Static"
53  else :
54  res += "Dynamic"
55  print(res)
56  else:
57  for i in xrange(0, size - 1):
58  if self.m_vlan_table[i].vlan_exists == True :
59  res = "VLAN: " + str(i) + " is "
60  if self.m_vlan_table[i].staticVlan == True :
61  res += "Static"
62  else :
63  res += "Dynamic"
64  print(res)
65 
66  def vlan_table_validate(self):
67  size = 4096
68  if version_info >= (3,0,0):
69  for vlan_num in range(0, size - 1) :
70  if self.m_vlan_table[vlan_num].exists == True :
71  ret = OpEN.openapiVlanCreatedCheck(self.m_client, vlan_num)
72  if ret == OPEN_E_NONE :
73  self.m_vlan_table[vlan_num].staticVlan = OpEN.openapiVlanIsStatic(self.m_client, vlan_num)
74  else :
75  self.m_vlan_table[vlan_num].vlan_exists = False;
76  self.m_vlan_table[vlan_num].staticVlan = False;
77  else:
78  for vlan_num in xrange(0, size - 1) :
79  if self.m_vlan_table[vlan_num].exists == True :
80  ret = OpEN.openapiVlanCreatedCheck(self.m_client, vlan_num)
81  if ret == OPEN_E_NONE :
82  self.m_vlan_table[vlan_num].staticVlan = OpEN.openapiVlanIsStatic(self.m_client, vlan_num)
83  else :
84  self.m_vlan_table[vlan_num].vlan_exists = False;
85  self.m_vlan_table[vlan_num].staticVlan = False;
86 
87  def vlan_table_create(self):
88  size = 4096
89  self.m_vlan_table = []
90 
91  open_event = OpEN.openapiEventList_t()
92  OpEN.openapiEventListClear(open_event)
93  OpEN.openapiEventSet(open_event, OpEN.OPEN_EVENT_VLAN)
94 
95  ret = OpEN.openapiEventRegister(self.m_client, open_event)
96  if ret != OpEN.OPEN_E_NONE :
97  print("Failed to register for OPEN_EVENT_VLAN events.\n")
98  else :
99  print("Successfully registered for OPEN_EVENT_VLAN events.\n")
100 
101  size -= 1
102  if version_info >= (3,0,0):
103  for vlan_num in range(0, size):
104  vlan = Vlan()
105  ret = OpEN.openapiVlanCreatedCheck(self.m_client, vlan_num)
106  if ret == OpEN.OPEN_E_NONE :
107  vlan.vlan_exists = True
108  vlan.staticVlan = OpEN.openapiVlanIsStatic(self.m_client, vlan_num)
109  else :
110  vlan.vlan_exists = False
111  vlan.staticVlan = False
112  self.m_vlan_table.append(vlan)
113  else:
114  for vlan_num in xrange(0, size):
115  vlan = Vlan()
116  ret = OpEN.openapiVlanCreatedCheck(self.m_client, vlan_num)
117  if ret == OpEN.OPEN_E_NONE :
118  vlan.vlan_exists = True
119  vlan.staticVlan = OpEN.openapiVlanIsStatic(self.m_client, vlan_num)
120  else :
121  vlan.vlan_exists = False
122  vlan.staticVlan = False
123  self.m_vlan_table.append(vlan)
124 
125  vlan_num = 1
126  while True :
127  self.m_vlan_table[vlan_num].vlan_exists = True
128  self.m_vlan_table[vlan_num].staticVlan = OpEN.openapiVlanIsStatic(self.m_client, vlan_num)
129  vlan_num_p = OpEN.new_uint32_tp()
130  ret = OpEN.openapiVlanNextGet(self.m_client, vlan_num, vlan_num_p)
131  if ret == OpEN.OPEN_E_NONE:
132  vlan_num = OpEN.uint32_tp_value(vlan_num_p)
133  else:
134  break
135  OpEN.delete_uint32_tp(vlan_num_p)
136 
137  def vlan_table_monitor(self, duration):
138  change_pending_event = OpEN.openapiEventList_t()
139  OpEN.openapiEventListClear(change_pending_event)
140  purge_event = OpEN.openapiEventList_t()
141  OpEN.openapiEventListClear(purge_event)
142 
143  end_time = time() + duration
144  notify_fd = OpEN.openapiClientNotifySocketFDGet(self.m_client)
145  if notify_fd == 0 :
146  print("Invalid notify_fd retrieved")
147  else :
148  fcntl.fcntl(notify_fd, fcntl.F_SETFL, os.O_NONBLOCK)
149  print("notify_fd is %d\n" % notify_fd)
150  rd_fds = [notify_fd]
151  wr_fds = []
152  ex_fds = []
153  while time() < end_time :
154  rr, wr, er = select(rd_fds, wr_fds, ex_fds, 5)
155  if len(rr) :
156  while True:
157  s = ""
158  try:
159  s = os.read(notify_fd, 1024)
160  except os.error as e:
161  if e.errno == errno.EWOULDBLOCK:
162  break
163  if len(s) > 0 :
164  continue
165  else :
166  break
167  ret = OpEN.openapiPendingEventsGet(self.m_client, change_pending_event.m_open_event, purge_event.m_open_event)
168  if ret == OpEN.OPEN_E_NONE :
169  if purge_event.is_set(OpEN.OPEN_EVENT_VLAN) == True :
170  print("Got Purge event for OPEN_EVENT_VLAN")
171  vlan_table_validate()
172  if change_pending_event.is_set(OpEN.OPEN_EVENT_VLAN) == True :
173  print("Got Change Pending event for OPEN_EVENT_VLAN")
174 
175  vlan_num = 0
176  vlan_num_p = OpEN.new_uint32_tp()
177  delete_pending_p = OpEN.new_uint32_tp()
178  while True :
179  ret = OpEN.openapiVlanNextChangedGet(self.m_client, vlan_num, vlan_num_p, delete_pending_p)
180  if ret == OpEN.OPEN_E_NONE :
181  vlan_num = OpEN.uint32_tp_value(vlan_num_p)
182  delete_pending = OpEN.uint32_tp_value(delete_pending_p)
183  if delete_pending != 0 :
184  print("Deleting VLAN ID: %d" % vlan_num)
185  self.m_vlan_table[vlan_num].vlan_exists = False
186  self.m_vlan_table[vlan_num].staticVlan = False
187  else :
188  print("Change event received for VLAN ID: %d" % vlan_num)
189  ret2 = OpEN.openapiVlanCreatedCheck(self.m_client, vlan_num)
190  if ret2 == OpEN.OPEN_E_NONE :
191  self.m_vlan_table[vlan_num].vlan_exists = True
192  self.m_vlan_table[vlan_num].staticVlan = OpEN.openapiVlanIsStatic(self.m_client, vlan_num)
193  else :
194  self.m_vlan_table[vlan_num].vlan_exists = False
195  self.m_vlan_table[vlan_num].staticVlan = False
196  self.vlan_status_show()
197  else :
198  break
199  OpEN.delete_uint32_tp(vlan_num_p)
200  OpEN.delete_uint32_tp(delete_pending_p)
201 
202 open = OpENUtil()
203 ret = open.connect("vlan_example")
204 if ret == OpEN.OPEN_E_NONE :
205  open.getNetworkOSVersion()
206  open.getAPIVersion()
207  client = open.get_client()
208  vlanExample = VlanExample(client)
209  vlanExample.vlan_table_create()
210  vlanExample.vlan_status_show()
211  vlanExample.vlan_table_monitor(60)
212  vlanExample.vlan_status_show()
213  open.terminate()
214 else :
215  print("Unable to connect")
216