blob: dba7857fdb35546f0e78dd5e31ae736589ecf8a3 [file] [log] [blame]
alshabib22302372016-12-20 13:46:14 -08001#
2# Copyright 2016 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16from uuid import uuid4
17from ofagent.utils import mac_str_to_tuple
18import structlog
19from voltha.protos.common_pb2 import ConnectStatus, OperStatus
20from voltha.protos.logical_device_pb2 import LogicalDevice, LogicalPort
21from voltha.protos.openflow_13_pb2 import ofp_desc, ofp_switch_features, OFPC_FLOW_STATS, OFPC_TABLE_STATS, \
22 OFPC_PORT_STATS, OFPC_GROUP_STATS, ofp_port, OFPPS_LIVE, OFPPF_10GB_FD, OFPPF_FIBER
23
24log = structlog.get_logger()
25
26class DeviceManager(object):
27
28 def __init__(self, device, adapter_agent):
29 self.device = device
30 self.adapter_agent = adapter_agent
31 self.logical_device = None
32
33 def update_device(self, pkt):
34
35 self.device.root = True
36 self.device.vendor = 'Celestica Inc.'
37 self.device.model = 'Ruby'
38 self.device.hardware_version = \
39 '{}.{}'.format(hex(pkt.major_hardware_version),
40 pkt.minor_hardware_version)
41 self.device.firmware_version = '{}.{}.{}'.format(pkt.major_firmware_version,
42 pkt.minor_firmware_version,
43 pkt.build_firmware_version)
44 self.device.software_version = '0.0.1'
45 self.device.serial_number = self.device.mac_address
46 self.device.connect_status = ConnectStatus.REACHABLE
47 self.adapter_agent.update_device(self.device)
48
49 def create_logical_device(self):
50 log.info('create-logical-device')
51 # then shortly after we create the logical device with one port
52 # that will correspond to the NNI port
53 logical_device_id = uuid4().hex[:12]
54 ld = LogicalDevice(
55 id=logical_device_id,
56 datapath_id=int('0x' + logical_device_id[:8], 16), # from id
57 desc=ofp_desc(
58 mfr_desc=self.device.vendor,
59 hw_desc=self.device.hardware_version,
60 sw_desc=self.device.firmware_version,
61 serial_num=uuid4().hex,
62 dp_desc='n/a'
63 ),
64 switch_features=ofp_switch_features(
65 n_buffers=256, # TODO fake for now
66 n_tables=2, # TODO ditto
67 capabilities=( # TODO and ditto
68 OFPC_FLOW_STATS
69 | OFPC_TABLE_STATS
70 | OFPC_PORT_STATS
71 | OFPC_GROUP_STATS
72 )
73 ),
74 root_device_id=self.device.id
75 )
76 self.adapter_agent.create_logical_device(ld)
77 self.logical_device = ld
78
79 def add_port(self, port):
80 self.adapter_agent.add_port(self.device.id, port)
81
82 cap = OFPPF_10GB_FD | OFPPF_FIBER
83 logical_port = LogicalPort(
84 id='uni',
85 ofp_port=ofp_port(
86 port_no=port.port_no,
87 hw_addr=mac_str_to_tuple(self.device.mac_address),
88 name='{}-{}'.format(port.label, port.port_no),
89 config=0,
90 state=OFPPS_LIVE,
91 curr=cap,
92 advertised=cap,
93 peer=cap,
94 curr_speed=OFPPF_10GB_FD,
95 max_speed=OFPPF_10GB_FD
96 )
97 )
alshabib22302372016-12-20 13:46:14 -080098 self.adapter_agent.add_logical_port(self.logical_device.id,
99 logical_port)
100
101 def activate(self):
102 self.device.parent_id = self.logical_device.id
103 self.device.oper_status = OperStatus.ACTIVE
104 self.adapter_agent.update_device(self.device)