-
Notifications
You must be signed in to change notification settings - Fork 0
/
yang_alto.py
93 lines (77 loc) · 5.68 KB
/
yang_alto.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
#!/usr/bin/env python3
# © 2024 Telefónica Innovación Digital
# 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.
import json
from datetime import datetime
COSTE = "routingcost"
class RespuestasAlto:
def __init__(self):
self.algo=0
self.tipos={"directory": "application/alto-directory+json", "networkmap": "application/alto-networkmap+json",
"networkmapfilter": "application/alto-networkmapfilter+json","costmap": "application/alto-costmap+json",
"costmapfilter": "application/alto-costmapfilter+json","endpointprop": "application/alto-endpointprop+json",
"endpointpropparams": "application/alto-endpointpropparams+json","endpointcost": "application/alto-endpointcost+json",
"endpointcostparams": "application/alto-endpointcostparams+json","error": "application/alto-error+json"}
def crear_respuesta(self, tipo, ctipo, rid, vtag, contenido):
if tipo == "cost-map":
return self.respuesta_costes(ctipo, rid, vtag, contenido)
elif tipo == "pid-map":
return self.respuesta_pid(ctipo, rid, vtag, contenido)
elif tipo == "filtro":
return self.respuesta_filtro(ctipo, rid, vtag, contenido)
elif tipo == "prop":
return self.respuesta_prop(ctipo, rid, vtag, contenido)
elif tipo == "endpoint-costs":
return self.respuestar_endpoint_costs(ctipo, rid, vtag, contenido)
else:
return ""
def respuesta_costes(self, tipo, rid, vtag, costmap):
'''
Return a json-YANG structure from a raw costmap.
Parameters:
type: type of map required.
rid: resource ID of the network map related
vtag: timestamp of the last network map
costmap: dict of PIDs and costs
'''
cabecera = {'Content-Type' : self.tipos[tipo]}
meta = {'dependent-vtags': [{'resource-id': str(rid), 'tag': str(vtag)}], 'cost-type':{'cost-mode':'numerical','cost-metric':COSTE}}
cuerpo = {'cost-map' : costmap}
#resp = "{'meta':{'Content-Type':'" + self.tipos[tipo] + "','dependent-vtag':[{'resource-id':'" + str(rid) + "','tag': '" + str(vtag) +"'}],'cost-type': {'cost-mode' : 'numerical','cost-metric' : 'routingcost'}},'cost-map':" + str(costmap) + "}"
#return "{'header':" + str(cabecera) + ", 'meta':"+ str(meta) + ", 'cost-map': " + str(costmap) + '}'
return "{'meta':"+ str(meta) + ", 'cost-map': " + str(costmap) + '}'
def respuesta_pid(self, tipo, rid, vtag, netmap):
'''
Return a json-YANG structure from a raw networkmap.
Parameters:
type: type of map required.
rid: resource ID of the network map
vtag: timestamp of the network map
netmap: dict of PIDs and network reachables
'''
cabecera = {'Content-Type' : self.tipos[tipo]}
meta = {'vtag': {'resource-id': str(rid), 'tag': str(vtag)}}
cuerpo = {"network-map" : netmap}
#resp = "{'meta' : {'type':'alto-networkmap+json','vtag' : [{'resource-id':'" + str(rid) + "','tag':'" + str(vtag) +"'}]},'network-map':" + str(netmap) + "}"
#return "{'header':" + str(cabecera) + ", 'meta':"+ str(meta) + ", 'network-map': " + str(netmap) + '}
return "{'meta':"+ str(meta) + ", 'network-map': " + str(netmap) + '}'
def respuesta_prop(self, tipo, rid, vtag, contenido):
cabecera = {'Content-Type' : self.tipos[tipo]}
meta = {'dependent-vtags': [{'resource-id': str(rid), 'tag': str(vtag)}]}
cuerpo = {'endpoint-properties' : contenido}
#resp = "{'meta':{'Content-Type':'" + self.tipos[tipo] + "','dependent-vtag':[{'resource-id':'" + str(rid) + "','tag': '" + str(vtag) +"'}],'cost-type': {'cost-mode' : 'numerical','cost-metric' : 'routingcost'}},'cost-map':" + str(costmap) + "}"
return "{'header':" + str(cabecera) + ", 'meta':"+ str(meta) + ", 'cost-map': " + str(contenido) + '}'
def respuestar_endpoint_costs(self, rid, vtag, costmap):
return ""
def indice(self):
return '''{"meta" : {"cost-types": {"num-routing": {"cost-mode" : "numerical","cost-metric": "routingcost","description": "My default"},"num-hop": {"cost-mode" : "numerical","cost-metric": "hopcount"},"ord-routing": {"cost-mode" : "ordinal","cost-metric": "routingcost"},"ord-hop": {"cost-mode" : "ordinal","cost-metric": "hopcount"}}},"resources" : {"network-map" : {"uri" : "http://localhost:5000/networkmap","media-type" : "application/alto-networkmap+json","uses": [ "networkmap-default" ]},"cost-map" : {"uri" : "http://localhost:5000/costmap","media-type" : "application/alto-costmap+json","capabilities" : {"cost-constraints" : true,"cost-type-names" : [ "num-routing", "num-hop","ord-routing", "ord-hop" ]},"uses": [ "networkmap-default" ]},"filtered-costs-map" : {"uri" : "http://localhost:5000//costmap/filter/<string:pid>","media-type" : "application/alto-networkmap+json","accepts" : "application/alto-networkmapfilter+json","uses": [ "networkmap-default" ]},"both-map" : {"uri" : "http://localhost:5000//maps","media-types" : ["application/alto-networkmap+json","application/alto-costmap+json"] ,"uses": [ "networkmap-default" ]}}}'''