Chip Boling | f5af85d | 2019-02-12 15:36:17 -0600 | [diff] [blame] | 1 | # Copyright 2017-present Adtran, Inc. |
| 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 | |
| 15 | import structlog |
| 16 | import json |
| 17 | from twisted.internet.defer import inlineCallbacks, returnValue |
| 18 | from adapters.adtran_common.xpon.tcont import TCont |
| 19 | # from python.adapters.openolt.protos import openolt_pb2 |
| 20 | from olt_traffic_descriptor import OltTrafficDescriptor |
| 21 | from ..adtran_olt_handler import AdtranOltHandler |
| 22 | |
| 23 | log = structlog.get_logger() |
| 24 | |
| 25 | |
| 26 | class OltTCont(TCont): |
| 27 | """ |
| 28 | Adtran OLT specific implementation |
| 29 | """ |
| 30 | def __init__(self, alloc_id, tech_profile_id, traffic_descriptor, pon_id, onu_id, uni_id, is_mock=False): |
| 31 | super(OltTCont, self).__init__(alloc_id, tech_profile_id, traffic_descriptor, uni_id, is_mock=is_mock) |
| 32 | self.pon_id = pon_id |
| 33 | self.onu_id = onu_id |
| 34 | |
| 35 | def __str__(self): |
| 36 | return "TCont: {}/{}/{}, alloc-id: {}".format(self.pon_id, self.onu_id, |
| 37 | self.uni_id, self.alloc_id) |
| 38 | |
| 39 | @staticmethod |
| 40 | def create(tcont, pon_id, onu_id, tech_profile_id, uni_id, ofp_port_no): |
| 41 | # Only valid information in the upstream tcont of a tech profile |
| 42 | if tcont.direction != openolt_pb2.UPSTREAM: |
| 43 | return None |
| 44 | |
| 45 | td = OltTrafficDescriptor.create(tcont, pon_id, onu_id, uni_id, ofp_port_no) |
| 46 | return OltTCont(tcont.alloc_id, tech_profile_id, td, pon_id, onu_id, uni_id) |
| 47 | |
| 48 | @inlineCallbacks |
| 49 | def add_to_hardware(self, session): |
| 50 | if self._is_mock: |
| 51 | returnValue('mock') |
| 52 | |
| 53 | uri = AdtranOltHandler.GPON_TCONT_CONFIG_LIST_URI.format(self.pon_id, self.onu_id) |
| 54 | data = json.dumps({'alloc-id': self.alloc_id}) |
| 55 | name = 'tcont-create-{}-{}: {}'.format(self.pon_id, self.onu_id, self.alloc_id) |
| 56 | |
| 57 | # For TCONT, only leaf is the key. So only post needed |
| 58 | try: |
| 59 | results = yield session.request('POST', uri, data=data, name=name, |
| 60 | suppress_error=False) |
| 61 | except Exception as _e: |
| 62 | results = None |
| 63 | |
| 64 | if self.traffic_descriptor is not None: |
| 65 | try: |
| 66 | results = yield self.traffic_descriptor.add_to_hardware(session) |
| 67 | |
| 68 | except Exception as e: |
| 69 | log.exception('traffic-descriptor', tcont=self, |
| 70 | td=self.traffic_descriptor, e=e) |
| 71 | raise |
| 72 | |
| 73 | returnValue(results) |
| 74 | |
| 75 | def remove_from_hardware(self, session): |
| 76 | if self._is_mock: |
| 77 | returnValue('mock') |
| 78 | |
| 79 | uri = AdtranOltHandler.GPON_TCONT_CONFIG_URI.format(self.pon_id, self.onu_id, self.alloc_id) |
| 80 | name = 'tcont-delete-{}-{}: {}'.format(self.pon_id, self.onu_id, self.alloc_id) |
| 81 | return session.request('DELETE', uri, name=name) |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |