Matteo Scandolo | ede125b | 2017-08-08 13:05:25 -0700 | [diff] [blame] | 1 | |
| 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 Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 17 | import os |
| 18 | import sys |
| 19 | import base64 |
| 20 | from django.db.models import F, Q |
| 21 | from xos.config import Config |
| 22 | from synchronizers.base.syncstep import SyncStep |
| 23 | from core.models import Service |
| 24 | from services.hpc.models import ServiceProvider, ContentProvider, CDNPrefix |
| 25 | from xos.logger import Logger, logging |
| 26 | |
| 27 | # hpclibrary will be in steps/.. |
| 28 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 29 | sys.path.insert(0,parentdir) |
| 30 | |
| 31 | from hpclib import HpcLibrary |
| 32 | |
| 33 | logger = Logger(level=logging.INFO) |
| 34 | |
| 35 | class SyncCDNPrefix(SyncStep, HpcLibrary): |
| 36 | provides=[CDNPrefix] |
| 37 | observes=CDNPrefix |
| 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 | return [x for x in objs if x.contentProvider.serviceProvider.hpcService == hpcService] |
| 48 | |
| 49 | def fetch_pending(self, deleted): |
| 50 | #self.consistency_check() |
| 51 | |
| 52 | return self.filter_hpc_service(SyncStep.fetch_pending(self, deleted)) |
| 53 | |
| 54 | def consistency_check(self): |
| 55 | # set to true if something changed |
| 56 | result=False |
| 57 | |
| 58 | # sanity check to make sure our PS objects have CMI objects behind them |
| 59 | all_p_ids = [x["cdn_prefix_id"] for x in self.client.onev.ListAll("CDNPrefix")] |
| 60 | |
| 61 | all_p_ids = [] |
| 62 | all_origins = {} |
| 63 | for x in self.client.onev.ListAll("CDNPrefix"): |
| 64 | id = x["cdn_prefix_id"] |
| 65 | all_p_ids.append(id) |
| 66 | all_origins[id] = x.get("default_origin_server", None) |
| 67 | |
| 68 | for p in CDNPrefix.objects.all(): |
| 69 | if (p.cdn_prefix_id is None): |
| 70 | continue |
| 71 | |
| 72 | if (p.cdn_prefix_id not in all_p_ids): |
| 73 | logger.info("CDN Prefix %s was not found on CMI" % p.cdn_prefix_id) |
| 74 | p.cdn_prefix_id=None |
| 75 | p.save() |
| 76 | result = True |
| 77 | |
| 78 | if (p.defaultOriginServer!=None) and (all_origins.get(p.cdn_prefix_id,None) != p.defaultOriginServer.url): |
| 79 | logger.info("CDN Prefix %s does not have default origin server on CMI" % str(p)) |
| 80 | p.save() # this will set updated>enacted and force observer to re-sync |
| 81 | result = True |
| 82 | |
| 83 | return result |
| 84 | |
| 85 | def sync_record(self, cp): |
| 86 | logger.info("sync'ing cdn prefix %s" % str(cp),extra=cp.tologdict()) |
| 87 | |
| 88 | if (not cp.contentProvider) or (not cp.contentProvider.content_provider_id): |
| 89 | raise Exception("CDN Prefix %s is linked to a contentProvider without an id" % str(cp)) |
| 90 | |
| 91 | cpid = cp.contentProvider.content_provider_id |
| 92 | |
| 93 | cp_dict = {"service": "HyperCache", "enabled": cp.enabled, "content_provider_id": cpid, "cdn_prefix": cp.prefix} |
| 94 | |
| 95 | if cp.defaultOriginServer and cp.defaultOriginServer.url: |
| 96 | if (not cp.defaultOriginServer.origin_server_id): |
| 97 | # It's probably a bad idea to try to set defaultOriginServer before |
| 98 | # we've crated defaultOriginServer. |
| 99 | raise Exception("cdn prefix %s is waiting for it's default origin server to get an id" % str(cp)) |
| 100 | |
| 101 | cp_dict["default_origin_server"] = cp.defaultOriginServer.url |
| 102 | |
| 103 | #print cp_dict |
| 104 | |
| 105 | if not cp.cdn_prefix_id: |
| 106 | id = self.client.onev.Create("CDNPrefix", cp_dict) |
| 107 | cp.cdn_prefix_id = id |
| 108 | else: |
| 109 | del cp_dict["content_provider_id"] # this can't be updated |
| 110 | del cp_dict["cdn_prefix"] # this can't be updated either |
| 111 | self.client.onev.Update("CDNPrefix", cp.cdn_prefix_id, cp_dict) |
| 112 | |
| 113 | cp.save() |
| 114 | |
| 115 | def delete_record(self, m): |
| 116 | if m.cdn_prefix_id is not None: |
| 117 | self.client.onev.Delete("CDNPrefix", m.cdn_prefix_id) |