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 | from adapters.adtran_common.net.adtran_netconf import adtran_module_url |
| 16 | from twisted.internet.defer import inlineCallbacks, returnValue |
| 17 | import xmltodict |
| 18 | import structlog |
| 19 | |
| 20 | log = structlog.get_logger() |
| 21 | |
| 22 | _phys_entities_rpc = """ |
| 23 | <filter xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 24 | <physical-entities-state xmlns="{}"> |
| 25 | <physical-entity/> |
| 26 | </physical-entities-state> |
| 27 | </filter> |
| 28 | """.format(adtran_module_url('adtran-physical-entities')) |
| 29 | |
| 30 | |
| 31 | class PhysicalEntitiesState(object): |
| 32 | def __init__(self, session): |
| 33 | self._session = session |
| 34 | self._rpc_reply = None |
| 35 | |
| 36 | @inlineCallbacks |
| 37 | def get_state(self): |
| 38 | self._rpc_reply = None |
| 39 | request = self._session.get(_phys_entities_rpc) |
| 40 | self._rpc_reply = yield request |
| 41 | returnValue(self._rpc_reply) |
| 42 | |
| 43 | @property |
| 44 | def physical_entities(self): |
| 45 | """ |
| 46 | :return: (list) of OrderDict physical entities |
| 47 | """ |
| 48 | if self._rpc_reply is None: |
| 49 | # TODO: Support auto-get? |
| 50 | return None |
| 51 | |
| 52 | result_dict = xmltodict.parse(self._rpc_reply.data_xml) |
| 53 | return result_dict['data']['physical-entities-state']['physical-entity'] |
| 54 | |
| 55 | def get_physical_entities(self, classification=None): |
| 56 | """ |
| 57 | Get the physical entities of a particular type |
| 58 | :param classification: (String or List) The classification or general hardware type of the |
| 59 | component identified by this physical entity |
| 60 | (case-insensitive) |
| 61 | :return: (list) of OrderDict physical entities |
| 62 | """ |
| 63 | entries = self.physical_entities |
| 64 | |
| 65 | if classification is None: |
| 66 | return entries |
| 67 | |
| 68 | # for entry in entries: |
| 69 | # import pprint |
| 70 | # log.info(pprint.PrettyPrinter(indent=2).pformat(entry)) |
| 71 | |
| 72 | def _matches(entry, value): |
| 73 | if 'classification' in entry and '#text' in entry['classification']: |
| 74 | text_val = entry['classification']['#text'].lower() |
| 75 | if isinstance(value, list): |
| 76 | return any(v.lower() in text_val for v in value) |
| 77 | return value.lower() in text_val |
| 78 | return False |
| 79 | |
| 80 | return [entry for entry in entries if _matches(entry, classification)] |