Commit b2ae2f7f authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

NBI Component - WellKnown-HostMeta Connector:

- Added "links" to well-known/host-meta
parent 59b47597
Loading
Loading
Loading
Loading
+12 −9
Original line number Diff line number Diff line
@@ -18,6 +18,9 @@ HTTP_ACCEPTED = 202
HTTP_NOCONTENT       = 204
HTTP_BADREQUEST      = 400
HTTP_NOTFOUND        = 404
HTTP_NOT_ACCEPTABLE  = 406
HTTP_CONFLICT        = 409
HTTP_UNSUPMEDIATYPE  = 415
HTTP_SERVERERROR     = 500
HTTP_NOT_IMPLEMENTED = 501
HTTP_GATEWAYTIMEOUT  = 504
+46 −14
Original line number Diff line number Diff line
@@ -12,14 +12,39 @@
# See the License for the specific language governing permissions and
# limitations under the License.


# RESTCONF .well-known endpoint (RFC 8040)

from flask import jsonify
from flask import abort, jsonify, make_response, request
from flask_restful import Resource
from xml.sax.saxutils import escape
import xml.etree.ElementTree as ET
from .._tools.HttpStatusCodes import HTTP_NOT_ACCEPTABLE

XRD_NS = 'http://docs.oasis-open.org/ns/xri/xrd-1.0'
ET.register_namespace('', XRD_NS)

RESTCONF_PREFIX = '/restconf'

class WellKnownHostMeta(Resource):
    def get(self):
        response = {
        best = request.accept_mimetypes.best_match([
            'application/xrd+xml',
            'application/json',
        ], default='application/xrd+xml')

        if best == 'application/xrd+xml':
            xrd = ET.Element('{{{:s}}}XRD'.format(str(XRD_NS)))
            ET.SubElement(xrd, '{{{:s}}}Link'.format(str(XRD_NS)), attrib={
                'rel': 'restconf', 'href': RESTCONF_PREFIX
            })
            xml_string = ET.tostring(xrd, encoding='utf-8', xml_declaration=True).decode()
            response = make_response(str(xml_string))
            response.status_code = 200
            response.content_type = best
            return response
        elif best == 'application/json':
            response = jsonify({
                'restconf': {
                    'capabilities': [
                        'urn:ietf:params:restconf:capability:defaults:1.0',
@@ -30,6 +55,13 @@ class WellKnownHostMeta(Resource):
                        'application/yang-data+json',
                        'application/yang-data+xml'
                    ]
            }
        }
        return jsonify(response)
                },
                'links': [
                    {'rel': 'restconf', 'href': RESTCONF_PREFIX}
                ]
            })
            response.status_code = 200
            response.content_type = best
            return response
        else:
            abort(HTTP_NOT_ACCEPTABLE)