Debian Codegraph @

ansible_collections/cisco/iosxr/plugins/sub_plugins/grpc/iosxr.py

raw
1 # (c) 2019 Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3 #
4 from __future__ import absolute_import, division, print_function
5
6
7 __metaclass__ = type
8
9 DOCUMENTATION = """
10 ---
11 author: Ansible Networking Team
12 name: grpc
13 short_description: gRPC plugin for IOS-XR devices
14 description:
15 - This gRPC plugin provides methods to connect and talk to Cisco IOS XR
16 devices over gRPC protocol.
17 version_added: "3.3.0"
18 """
19
20 import json
21 import os
22 import sys
23
24 from ansible_collections.ansible.netcommon.plugins.sub_plugins.grpc.base import (
25 GrpcBase,
26 ensure_connect,
27 )
28
29
30 class Grpc(GrpcBase):
31 def __init__(self, connection):
32 super(Grpc, self).__init__(connection)
33 module_name = "ems_grpc_pb2"
34 module_path = os.path.join(
35 os.path.dirname(os.path.realpath(__file__)),
36 "pb/ems_grpc_pb2.py",
37 )
38 if sys.version_info[0] == 3 and sys.version_info[1] >= 5:
39 import importlib.util
40
41 spec = importlib.util.spec_from_file_location(module_name, module_path)
42 self._ems_grpc_pb2 = importlib.util.module_from_spec(spec)
43 spec.loader.exec_module(self._ems_grpc_pb2)
44 elif sys.version_info[0] == 3 and sys.version_info[1] < 5:
45 import importlib.machinery
46
47 loader = importlib.machinery.SourceFileLoader(module_name, module_path)
48 self._ems_grpc_pb2 = loader.load_module()
49 elif sys.version_info[0] == 2:
50 import imp
51
52 self._ems_grpc_pb2 = imp.load_source(module_name, module_path)
53
54 def get_config(self, section=None):
55 stub = self._ems_grpc_pb2.beta_create_gRPCConfigOper_stub(
56 self._connection._channel,
57 )
58 message = self._ems_grpc_pb2.ConfigGetArgs(yangpathjson=section)
59 responses = stub.GetConfig(
60 message,
61 self._connection._timeout,
62 metadata=self._connection._login_credentials,
63 )
64 output = {"response": "", "error": ""}
65 for response in responses:
66 output["response"] += response.yangjson
67 output["error"] += response.errors
68 return output
69
70 def get(self, section=None):
71 stub = self._ems_grpc_pb2.beta_create_gRPCConfigOper_stub(
72 self._connection._channel,
73 )
74 message = self._ems_grpc_pb2.GetOperArgs(yangpathjson=section)
75 responses = stub.GetOper(
76 message,
77 self._connection._timeout,
78 metadata=self._connection._login_credentials,
79 )
80 output = {"response": "", "error": ""}
81 for response in responses:
82 output["response"] += response.yangjson
83 output["error"] += response.errors
84 return output
85
86 @ensure_connect
87 def merge_config(self, path):
88 """Merge grpc call equivalent of PATCH RESTconf call
89 :param data: JSON
90 :type data: str
91 :return: Return the response object
92 :rtype: Response object
93 """
94 path = json.dumps(path)
95 stub = self._ems_grpc_pb2.beta_create_gRPCConfigOper_stub(
96 self._connection._channel,
97 )
98 message = self._ems_grpc_pb2.ConfigArgs(yangjson=path)
99 response = stub.MergeConfig(
100 message,
101 self._connection._timeout,
102 metadata=self._connection._login_credentials,
103 )
104 if response:
105 return response.errors
106 else:
107 return None
108
109 @ensure_connect
110 def replace_config(self, path):
111 """Replace grpc call equivalent of PATCH RESTconf call
112 :param data: JSON
113 :type data: str
114 :return: Return the response object
115 :rtype: Response object
116 """
117 path = json.dumps(path)
118 stub = self._ems_grpc_pb2.beta_create_gRPCConfigOper_stub(
119 self._connection._channel,
120 )
121 message = self._ems_grpc_pb2.ConfigArgs(yangjson=path)
122 response = stub.ReplaceConfig(
123 message,
124 self._connection._timeout,
125 metadata=self._connection._login_credentials,
126 )
127 if response:
128 return response.errors
129 else:
130 return None
131
132 @ensure_connect
133 def delete_config(self, path):
134 """Delete grpc call equivalent of PATCH RESTconf call
135 :param data: JSON
136 :type data: str
137 :return: Return the response object
138 :rtype: Response object
139 """
140 path = json.dumps(path)
141 stub = self._ems_grpc_pb2.beta_create_gRPCConfigOper_stub(
142 self._connection._channel,
143 )
144 message = self._ems_grpc_pb2.ConfigArgs(yangjson=path)
145 response = stub.DeleteConfig(
146 message,
147 self._connection._timeout,
148 metadata=self._connection._login_credentials,
149 )
150 if response:
151 return response.errors
152 else:
153 return None
154
155 @ensure_connect
156 def run_cli(self, command=None, display=None):
157 if command is None:
158 raise ValueError("command value must be provided")
159
160 output = {"response": "", "error": ""}
161 stub = self._ems_grpc_pb2.beta_create_gRPCExec_stub(
162 self._connection._channel,
163 )
164
165 message = self._ems_grpc_pb2.ShowCmdArgs(cli=command)
166 if display == "text":
167 responses = stub.ShowCmdTextOutput(
168 message,
169 self._connection._timeout,
170 metadata=self._connection._login_credentials,
171 )
172 for response in responses:
173 output["response"] += response.output
174 output["error"] += response.errors
175 else:
176 responses = stub.ShowCmdJSONOutput(
177 message,
178 self._connection._timeout,
179 metadata=self._connection._login_credentials,
180 )
181 for response in responses:
182 output["response"] += response.jsonoutput
183 output["error"] += response.errors
184 return output
185
186 @property
187 def server_capabilities(self):
188 capability = dict()
189 capability["display"] = ["json", "text"]
190 capability["data_type"] = ["config", "oper"]
191 capability["supports_commit"] = True
192 capability["supports_cli_command"] = True
193 return capability
194
195 @ensure_connect
196 def get_capabilities(self):
197 result = dict()
198 result["rpc"] = self.__rpc__ + ["commit", "discard_changes"]
199 result["network_api"] = "ansible.netcommon.grpc"
200 result["server_capabilities"] = self.server_capabilities
201 return result