2020-07-23 09:20:04 -07:00
|
|
|
"""Base Connection Class."""
|
|
|
|
|
|
|
|
|
|
# Standard Library
|
2021-09-16 15:57:33 -07:00
|
|
|
import typing as t
|
2021-09-12 15:06:34 -07:00
|
|
|
from abc import ABC, abstractmethod
|
2020-07-23 09:20:04 -07:00
|
|
|
|
|
|
|
|
# Project
|
|
|
|
|
from hyperglass.log import log
|
2021-09-16 15:57:33 -07:00
|
|
|
from hyperglass.types import Series
|
2021-09-11 17:55:27 -07:00
|
|
|
from hyperglass.plugins import OutputPluginManager
|
2020-07-23 09:20:04 -07:00
|
|
|
|
2020-10-11 13:14:57 -07:00
|
|
|
# Local
|
2020-10-05 12:11:39 -07:00
|
|
|
from ._construct import Construct
|
|
|
|
|
|
2021-09-16 15:57:33 -07:00
|
|
|
if t.TYPE_CHECKING:
|
2021-09-12 15:06:34 -07:00
|
|
|
# Project
|
2021-09-12 18:27:33 -07:00
|
|
|
from hyperglass.models.api import Query
|
2021-09-13 02:37:32 -07:00
|
|
|
from hyperglass.models.data import OutputDataModel
|
2021-09-12 15:06:34 -07:00
|
|
|
from hyperglass.compat._sshtunnel import SSHTunnelForwarder
|
2021-09-12 18:27:33 -07:00
|
|
|
from hyperglass.models.config.devices import Device
|
2020-07-23 09:20:04 -07:00
|
|
|
|
2021-09-12 15:06:34 -07:00
|
|
|
|
|
|
|
|
class Connection(ABC):
|
2020-07-23 09:20:04 -07:00
|
|
|
"""Base transport driver class."""
|
|
|
|
|
|
2021-09-12 18:27:33 -07:00
|
|
|
def __init__(self, device: "Device", query_data: "Query") -> None:
|
2020-07-23 09:20:04 -07:00
|
|
|
"""Initialize connection to device."""
|
|
|
|
|
self.device = device
|
|
|
|
|
self.query_data = query_data
|
|
|
|
|
self.query_type = self.query_data.query_type
|
|
|
|
|
self.query_target = self.query_data.query_target
|
2021-09-07 22:58:39 -07:00
|
|
|
self._query = Construct(device=self.device, query=self.query_data)
|
2020-07-23 09:20:04 -07:00
|
|
|
self.query = self._query.queries()
|
2021-09-11 17:55:27 -07:00
|
|
|
self.plugin_manager = OutputPluginManager()
|
2020-07-23 09:20:04 -07:00
|
|
|
|
2021-09-12 15:06:34 -07:00
|
|
|
@abstractmethod
|
|
|
|
|
def setup_proxy(self: "Connection") -> "SSHTunnelForwarder":
|
|
|
|
|
"""Return a preconfigured sshtunnel.SSHTunnelForwarder instance."""
|
|
|
|
|
pass
|
|
|
|
|
|
2021-09-16 15:57:33 -07:00
|
|
|
async def response(self, output: Series[str]) -> t.Union["OutputDataModel", str]:
|
2020-07-23 09:20:04 -07:00
|
|
|
"""Send output through common parsers."""
|
|
|
|
|
|
2020-09-28 12:37:44 -07:00
|
|
|
log.debug("Pre-parsed responses:\n{}", output)
|
2020-07-23 09:20:04 -07:00
|
|
|
|
2021-09-18 12:47:56 -07:00
|
|
|
response = self.plugin_manager.execute(output=output, query=self.query_data)
|
2020-07-23 09:20:04 -07:00
|
|
|
|
|
|
|
|
if response is None:
|
2021-09-16 15:57:33 -07:00
|
|
|
response = ()
|
2020-07-23 09:20:04 -07:00
|
|
|
|
2020-09-28 12:37:44 -07:00
|
|
|
log.debug("Post-parsed responses:\n{}", response)
|
2020-07-23 09:20:04 -07:00
|
|
|
return response
|