Chip Boling | f5af85d | 2019-02-12 15:36:17 -0600 | [diff] [blame^] | 1 | # |
| 2 | # Copyright 2017-present Adtran, Inc. |
| 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 | |
| 16 | import structlog |
| 17 | import json |
| 18 | |
| 19 | from adapters.adtran_common.xpon.gem_port import GemPort |
| 20 | from twisted.internet.defer import inlineCallbacks, returnValue |
| 21 | from ..adtran_olt_handler import AdtranOltHandler |
| 22 | |
| 23 | log = structlog.get_logger() |
| 24 | |
| 25 | |
| 26 | class OltGemPort(GemPort): |
| 27 | """ |
| 28 | Adtran OLT specific implementation |
| 29 | """ |
| 30 | def __init__(self, gem_id, alloc_id, tech_profile_id, pon_id, onu_id, uni_id, |
| 31 | encryption=False, |
| 32 | multicast=False, |
| 33 | traffic_class=None, |
| 34 | handler=None, |
| 35 | is_mock=False): |
| 36 | super(OltGemPort, self).__init__(gem_id, alloc_id, uni_id, tech_profile_id, |
| 37 | encryption=encryption, |
| 38 | multicast=multicast, |
| 39 | traffic_class=traffic_class, |
| 40 | handler=handler, |
| 41 | is_mock=is_mock) |
| 42 | self._timestamp = None |
| 43 | self._pon_id = pon_id |
| 44 | self._onu_id = onu_id # None if this is a multicast GEM Port |
| 45 | |
| 46 | def __str__(self): |
| 47 | return "GemPort: {}/{}/{}, alloc-id: {}, gem-id: {}".format(self.pon_id, self.onu_id, |
| 48 | self.uni_id, self.alloc_id, |
| 49 | self.gem_id) |
| 50 | |
| 51 | @staticmethod |
| 52 | def create(handler, gem, alloc_id, tech_profile_id, pon_id, onu_id, uni_id, _ofp_port_no): |
| 53 | return OltGemPort(gem.gemport_id, |
| 54 | alloc_id, |
| 55 | tech_profile_id, |
| 56 | pon_id, onu_id, uni_id, |
| 57 | encryption=gem.aes_encryption.lower() == 'true', |
| 58 | handler=handler, |
| 59 | multicast=False) |
| 60 | |
| 61 | @property |
| 62 | def pon_id(self): |
| 63 | return self._pon_id |
| 64 | |
| 65 | @property |
| 66 | def onu_id(self): |
| 67 | return self._onu_id |
| 68 | |
| 69 | @property |
| 70 | def timestamp(self): |
| 71 | return self._timestamp |
| 72 | |
| 73 | @timestamp.setter |
| 74 | def timestamp(self, value): |
| 75 | self._timestamp = value |
| 76 | |
| 77 | @property |
| 78 | def encryption(self): |
| 79 | return self._encryption |
| 80 | |
| 81 | @encryption.setter |
| 82 | def encryption(self, value): |
| 83 | assert isinstance(value, bool), 'encryption is a boolean' |
| 84 | |
| 85 | if self._encryption != value: |
| 86 | self._encryption = value |
| 87 | self.set_config(self._handler.rest_client, 'encryption', value) |
| 88 | |
| 89 | @inlineCallbacks |
| 90 | def add_to_hardware(self, session, operation='POST'): |
| 91 | if self._is_mock: |
| 92 | returnValue('mock') |
| 93 | |
| 94 | uri = AdtranOltHandler.GPON_GEM_CONFIG_LIST_URI.format(self.pon_id, self.onu_id) |
| 95 | data = json.dumps(self.to_dict()) |
| 96 | name = 'gem-port-create-{}-{}: {}/{}'.format(self.pon_id, self.onu_id, |
| 97 | self.gem_id, |
| 98 | self.alloc_id) |
| 99 | try: |
| 100 | results = yield session.request(operation, uri, data=data, name=name) |
| 101 | returnValue(results) |
| 102 | |
| 103 | except Exception as e: |
| 104 | if operation == 'POST': |
| 105 | returnValue(self.add_to_hardware(session, operation='PATCH')) |
| 106 | else: |
| 107 | log.exception('add-2-hw', gem=self, e=e) |
| 108 | raise |
| 109 | |
| 110 | def remove_from_hardware(self, session): |
| 111 | if self._is_mock: |
| 112 | returnValue('mock') |
| 113 | |
| 114 | uri = AdtranOltHandler.GPON_GEM_CONFIG_URI.format(self.pon_id, self.onu_id, self.gem_id) |
| 115 | name = 'gem-port-delete-{}-{}: {}'.format(self.pon_id, self.onu_id, self.gem_id) |
| 116 | return session.request('DELETE', uri, name=name) |
| 117 | |
| 118 | def set_config(self, session, leaf, value): |
| 119 | from ..adtran_olt_handler import AdtranOltHandler |
| 120 | |
| 121 | data = json.dumps({leaf: value}) |
| 122 | uri = AdtranOltHandler.GPON_GEM_CONFIG_URI.format(self.pon_id, |
| 123 | self.onu_id, |
| 124 | self.gem_id) |
| 125 | name = 'onu-set-config-{}-{}-{}'.format(self._pon_id, leaf, str(value)) |
| 126 | return session.request('PATCH', uri, data=data, name=name) |