This repository was archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnetconf-rest.py
executable file
·267 lines (199 loc) · 8.52 KB
/
netconf-rest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python
# coding=utf-8
"""
#************************************************
# Copyright 2018 Fortinet, Inc.
#
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
#************************************************
# Netconf to Rest translator for FortiGate
#
# +----------+-----------------------------------------+
# | RESTCONF | NETCONF |
# +----------+-----------------------------------------+
# | OPTIONS | none |
# | HEAD | none |
# | GET | < get - config >, < get > |
# | POST | < edit - config > (operation="create") |
# | PUT | < edit - config > (operation="replace") |
# | PATCH | < edit - config > (operation="merge") |
# | DELETE | < edit - config > (operation="delete") |
# +----------+-----------------------------------------+
# Use: sudo ./netconf-rest.py
#
# Author: "Miguel Angel Muñoz González" (********* at fortinet.com)
#
#************************************************
"""
from __future__ import absolute_import, division, unicode_literals, print_function, nested_scopes
import logging
import argparse
from time import sleep
try:
from lxml import etree
except ImportError:
from xml.etree import ElementTree as etree
from netconf import server
from yang2rest.yang2restconverter import Yang2RestConverter
from yang2rest.restcaller import RestCaller
from yang2rest.json2yang import Json2Yang
from fortiosapi import FortiOSAPI
import sys, traceback
__author__ = "Miguel Angel Muñoz González (********** at fortinet.com)"
__copyright__ = "Copyright 2018, Fortinet, Inc."
__credits__ = "Miguel Angel Muñoz"
__license__ = "Apache 2.0"
__version__ = "0.6"
__maintainer__ = "Miguel Ángel Muñoz"
__email__ = "******* at fortinet.com"
__status__ = "Development"
# **********************************
# Global definitions
# **********************************
netconf_server = None # pylint: disable=C0103
logger = logging.getLogger(__name__) # pylint: disable=C0103
NC_PORT = 830
NC_USER = ''
NC_PASSWORD = ''
FGT_HOST='192.168.122.40'
FGT_USER=''
FGT_PASSWORD=''
SERVER_DEBUG = False
# **********************************
# General Netconf functions
# **********************************
class NetconfMethods(server.NetconfMethods):
""" Class containing the methods that will be called upon reception of Netconf external calls"""
def nc_append_capabilities(self, capabilities_answered):
capability_list = ["urn:ietf:params:netconf:capability:writable - running:1.0",
"urn:ietf:params:netconf:capability:interleave:1.0",
"urn:ietf:params:netconf:capability:notification:1.0",
"urn:ietf:params:netconf:capability:validate:1.0"]
for cap in capability_list:
elem = etree.Element("capability")
elem.text = cap
capabilities_answered.append(elem)
return
def rpc_get(self, unused_session, rpc, *unused_params):
logger.info("rpc_get")
logger.debug("RPC received:%s", format(etree.tostring(rpc, pretty_print=True)))
# Locate object
ns = {"nc": "urn:ietf:params:xml:ns:netconf:base:1.0"}
netconf_data = rpc.find("nc:get/nc:filter/", ns)
if netconf_data is None:
raise Exception("Not able to find filter tag")
y2rc = Yang2RestConverter()
(url, content, operation) = y2rc.extract_url_content_operation(netconf_data)
logger.info("URL: %s", format(url))
logger.info("Content: %s", format(content))
logger.info("Operation: %s", format(operation))
fosapi = FortiOSAPI()
fosapi.https('off')
fosapi.login(FGT_HOST, FGT_USER, FGT_PASSWORD)
rc = RestCaller()
rc.set_fos(fosapi)
http_result, http_content = rc.execute_rest_call(operation, url, content)
fosapi.logout()
if http_result == 200 or 'success':
if not http_content or http_content is None:
return etree.Element('ok')
else:
j2y = Json2Yang()
return j2y.convert_json(str(http_content).replace("'", '"'))
else:
raise Exception('http-result:' + str(http_result) + ', ' + http_content)
def rpc_get_config(self, unused_session, rpc, *unused_params):
logger.info("rpc_get_config")
logger.debug("RPC received:%s", format(etree.tostring(rpc, pretty_print=True)))
# Locate object
ns = {"nc": "urn:ietf:params:xml:ns:netconf:base:1.0"}
netconf_data = rpc.find("nc:get-config/nc:filter/", ns)
if netconf_data is None:
raise Exception("Not able to find filter tag")
y2rc = Yang2RestConverter()
(url, content, operation) = y2rc.extract_url_content_operation(netconf_data)
logger.info("URL: %s", format(url))
logger.info("Content: %s", format(content))
logger.info("Operation: %s", format(operation))
fosapi = FortiOSAPI()
fosapi.https('off')
fosapi.login(FGT_HOST, FGT_USER, FGT_PASSWORD)
rc = RestCaller()
rc.set_fos(fosapi)
http_result, http_content = rc.execute_rest_call(operation, url, content)
fosapi.logout()
if http_result == 200:
j2y = Json2Yang()
return j2y.convert_json(str(http_content).replace("'", '"'))
else:
raise Exception('http-result:' + str(http_result) + ', ' + http_content)
def rpc_edit_config(self, unused_session, rpc, *unused_params):
logger.info("rpc_edit_config")
logger.debug("RPC received:%s", format(etree.tostring(rpc, pretty_print=True)))
# #Locate object
ns = {"nc": "urn:ietf:params:xml:ns:netconf:base:1.0"}
netconf_data = rpc.find("nc:edit-config/nc:config/", ns)
yrc = Yang2RestConverter()
(url, content, operation) = yrc.extract_url_content_operation(netconf_data)
logger.info("URL: %s", format(url))
logger.info("Content: %s", format(content))
logger.info("Operation: %s", format(operation))
fosapi = FortiOSAPI()
fosapi.https('off')
fosapi.login(FGT_HOST, FGT_USER, FGT_PASSWORD)
rc = RestCaller()
rc.set_fos(fosapi)
http_result, status = rc.execute_rest_call(operation, url, content)
fosapi.logout()
if http_result == 200:
return etree.Element("ok")
else:
raise Exception('http-result:' + str(http_result) + ', ' + status)
def rpc_create_subscription(self, unused_session, rpc, *unused_params):
return etree.Element("ok")
# **********************************
# Setup Netconf
# **********************************
def setup_netconf():
"Configure Netconf server listener"
global netconf_server # pylint: disable=C0103
if netconf_server is not None:
logger.error("Netconf Server is already up and running")
else:
server_ctl = server.SSHUserPassController(username=NC_USER,
password=NC_PASSWORD)
netconf_server = server.NetconfSSHServer(server_ctl=server_ctl,
server_methods=NetconfMethods(),
port=NC_PORT,
host_key="keys/host_key",
debug=SERVER_DEBUG)
# **********************************
# Main
# **********************************
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Netconf Server")
parser.add_argument("-d", "--debug", action="store_true", help="Activate debug logs")
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.DEBUG)
SERVER_DEBUG = logger.getEffectiveLevel() == logging.DEBUG
logger.info("SERVER_DEBUG:" + str(SERVER_DEBUG))
setup_netconf()
# Start the loop for Netconf
logger.info("Listening Netconf")
while True:
sleep(5)