blob: b2b581b9dc5234e7a11afd063a9a1f18be6ff6b6 [file] [log] [blame]
Matteo Scandoloede125b2017-08-08 13:05:25 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Baker25467ff2016-08-04 09:50:22 -070017import os
18import sys
19import base64
20from django.db.models import F, Q
21from xos.config import Config
22from synchronizers.base.syncstep import SyncStep
23from core.models import Service
24from services.hpc.models import ServiceProvider, ContentProvider, CDNPrefix, SiteMap
25from xos.logger import Logger, logging
26
27# hpclibrary will be in steps/..
28parentdir = os.path.join(os.path.dirname(__file__),"..")
29sys.path.insert(0,parentdir)
30
31from hpclib import HpcLibrary
32
33logger = Logger(level=logging.INFO)
34
35class SyncSiteMap(SyncStep, HpcLibrary):
36 provides=[SiteMap]
37 observes=SiteMap
38 requested_interval=0
39
40 def __init__(self, **args):
41 SyncStep.__init__(self, **args)
42 HpcLibrary.__init__(self)
43
44 def filter_hpc_service(self, objs):
45 hpcService = self.get_hpc_service()
46
47 filtered_objs = []
48 for x in objs:
49 if ((x.hpcService == hpcService) or
50 ((x.serviceProvider != None) and (x.serviceProvider.hpcService == hpcService)) or
51 ((x.contentProvider != None) and (x.contentProvider.serviceProvider.hpcService == hpcService)) or
52 ((x.cdnPrefix!= None) and (x.cdnPrefix.contentProvider.serviceProvider.hpcService == hpcService))):
53 filtered_objs.append(x)
54
55 return filtered_objs
56
57 def fetch_pending(self, deleted):
58 return self.filter_hpc_service(SyncStep.fetch_pending(self, deleted))
59
60 def consistency_check(self):
61 # set to true if something changed
62 result=False
63
64 # sanity check to make sure our PS objects have CMI objects behind them
65 all_map_ids = [x["map_id"] for x in self.client.onev.ListAll("Map")]
66 for map in SiteMap.objects.all():
67 if (map.map_id is not None) and (map.map_id not in all_map_ids):
68 logger.info("Map %s was not found on CMI" % map.map_id,extra=map.tologdict())
69 map.map_id=None
70 map.save()
71 result = True
72
73 return result
74
75 def update_bind(self, map, map_dict, field_name, to_name, ids):
76 for id in ids:
77 if (not id in map_dict.get(field_name, [])):
78 print "Bind Map", map.map_id, "to", to_name, id
79 self.client.onev.Bind("Map", map.map_id, to_name, id)
80
81 for id in map_dict.get(field_name, []):
82 if (not id in ids):
83 print "Unbind Map", map.map_id, "from", to_name, id
84 self.client.onev.UnBind("map", map.map_id, to_name, id)
85
86 def sync_record(self, map):
87 logger.info("sync'ing SiteMap %s" % str(map),extra=map.tologdict())
88
89 if not map.map:
90 # no contents
91 return
92
93 content = map.map.read()
94
95 map_dict = {"name": map.name, "type": "site", "content": content}
96
97 cdn_prefix_ids=[]
98 service_provider_ids=[]
99 content_provider_ids=[]
100
101 if (map.contentProvider):
102 if not map.contentProvider.content_provider_id:
103 raise Exception("Map %s links to a contentProvider with no id" % map.name)
104 conent_provider_ids = [map.contentProvider.content_provider_id]
105
106 if (map.serviceProvider):
107 if not map.serviceProvider.service_provider_id:
108 raise Exception("Map %s links to a serviceProvider with no id" % map.name)
109 service_provider_ids = [map.serviceProvider.service_provider_id]
110
111 if (map.cdnPrefix):
112 if not map.cdnPrefix.cdn_prefix_id:
113 raise Exception("Map %s links to a cdnPrefix with no id" % map.name)
114 cdn_prefix_ids = [map.cdnPrefix.cdn_prefix_id]
115
116 if not map.map_id:
117 print "Create Map", map_dict
118 id = self.client.onev.Create("Map", map_dict)
119 map.map_id = id
120 else:
121 print "Update Map", map_dict
122 # these things we probably cannot update
123 del map_dict["name"]
124 self.client.onev.Update("Map", map.map_id, map_dict)
125
126 cmi_map_dict = self.client.onev.Read("Map", map.map_id)
127
128 self.update_bind(map, cmi_map_dict, "cdn_prefix_ids", "CDNPrefix", cdn_prefix_ids)
129
130 map.save()
131
132 def delete_record(self, m):
133 if m.map_id is not None:
134 self.client.onev.Delete("Map", m.map_id)