blob: 1674725ea625a34bccf96643ba9b98fa0c9360ad [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
20
21from django.db.models import F, Q
22from xos.config import Config
23from synchronizers.base.syncstep import SyncStep
24from core.models import Service
25from services.hpc.models import ServiceProvider, ContentProvider, CDNPrefix, OriginServer
26from xos.logger import Logger, logging
27
28# hpclibrary will be in steps/..
29parentdir = os.path.join(os.path.dirname(__file__),"..")
30sys.path.insert(0,parentdir)
31
32from hpclib import HpcLibrary
33
34logger = Logger(level=logging.INFO)
35
36class SyncOriginServer(SyncStep, HpcLibrary):
37 provides=[OriginServer]
38 observes=OriginServer
39 requested_interval=0
40
41 def __init__(self, **args):
42 SyncStep.__init__(self, **args)
43 HpcLibrary.__init__(self)
44
45 def filter_hpc_service(self, objs):
46 hpcService = self.get_hpc_service()
47
48 return [x for x in objs if x.contentProvider.serviceProvider.hpcService == hpcService]
49
50 def fetch_pending(self, deleted):
51 #self.consistency_check()
52
53 return self.filter_hpc_service(SyncStep.fetch_pending(self, deleted))
54
55 def consistency_check(self):
56 # set to true if something changed
57 result=False
58
59 # sanity check to make sure our PS objects have CMI objects behind them
60 all_ors_ids = [x["origin_server_id"] for x in self.client.onev.ListAll("OriginServer")]
61 for ors in OriginServer.objects.all():
62 if (ors.origin_server_id is not None) and (ors.origin_server_id not in all_ors_ids):
63 # we have an origin server ID, but it doesn't exist in the CMI
64 # something went wrong
65 # start over
66 logger.info("origin server %s was not found on CMI" % ors.origin_server_id)
67 ors.origin_server_id=None
68 ors.save()
69 result = True
70
71 return result
72
73 def sync_record(self, ors):
74 logger.info("sync'ing origin server %s" % str(ors),extra=ors.tologdict())
75
76 if (not ors.contentProvider) or (not ors.contentProvider.content_provider_id):
77 raise Exception("Origin Server %s is linked to a contentProvider with no id" % str(ors))
78
79 cpid = ors.contentProvider.content_provider_id
80
81 # validation requires URL start with http://
82 url = ors.url
83 if not url.startswith("http://"):
84 url = "http://" + url
85
86 ors_dict = {"authenticated_content": ors.authenticated, "zone_redirects": ors.redirects, "content_provider_id": cpid, "url": url, "service_type": "HyperCache", "caching_type": "Optimistic", "description": ors.description}
87 if not ors_dict["description"]:
88 ors_dict["description"] = "blank"
89
90 #print os_dict
91
92 if not ors.origin_server_id:
93 id = self.client.onev.Create("OriginServer", ors_dict)
94 ors.origin_server_id = id
95 else:
96 self.client.onev.Update("OriginServer", ors.origin_server_id, ors_dict)
97
98 # ... something breaks (analytics) if the URL starts with http://, so we
99 # change it in cob after we added it via onev.
100 url = url[7:]
101 self.client.cob.UpdateContent(ors.origin_server_id, {"url": url})
102
103 ors.silent = True
104 ors.save()
105
106 def delete_record(self, m):
107 if m.origin_server_id is not None:
108 self.client.onev.Delete("OriginServer", m.origin_server_id)