Matteo Scandolo | eb0d11c | 2017-08-08 13:05:26 -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 | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 17 | import hashlib |
| 18 | import os |
| 19 | import socket |
| 20 | import socket |
| 21 | import sys |
| 22 | import base64 |
| 23 | import time |
| 24 | import re |
| 25 | import json |
| 26 | from django.db.models import F, Q |
| 27 | from xos.config import Config |
Murat Parlakisik | 638c65f | 2017-05-31 11:10:24 +0300 | [diff] [blame] | 28 | from synchronizers.new_base.syncstep import SyncStep |
| 29 | from synchronizers.new_base.ansible_helper import run_template_ssh |
| 30 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 31 | from modelaccessor import * |
| 32 | #from core.models import Service, Slice, ControllerSlice, ControllerUser |
| 33 | #from services.monitoring.models import SFlowService, SFlowTenant |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 34 | from xos.logger import Logger, logging |
| 35 | |
| 36 | # hpclibrary will be in steps/.. |
| 37 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 38 | sys.path.insert(0,parentdir) |
| 39 | |
| 40 | logger = Logger(level=logging.INFO) |
| 41 | |
| 42 | class SyncSFlowTenant(SyncInstanceUsingAnsible): |
| 43 | provides=[SFlowTenant] |
| 44 | observes=SFlowTenant |
| 45 | requested_interval=0 |
| 46 | template_name = "sync_sflowtenant.yaml" |
Srikanth Vavilapalli | d84b7b7 | 2016-06-28 00:19:07 +0000 | [diff] [blame] | 47 | service_key_name = "/opt/xos/synchronizers/monitoring/monitoring_channel_private_key" |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 48 | |
| 49 | def __init__(self, *args, **kwargs): |
| 50 | super(SyncSFlowTenant, self).__init__(*args, **kwargs) |
| 51 | |
| 52 | def fetch_pending(self, deleted): |
| 53 | if (not deleted): |
| 54 | objs = SFlowTenant.get_tenant_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 55 | else: |
| 56 | objs = SFlowTenant.get_deleted_tenant_objects() |
| 57 | |
| 58 | return objs |
| 59 | |
| 60 | def get_sflow_service(self, o): |
| 61 | sflows = SFlowService.get_service_objects().filter(id=o.provider_service.id) |
| 62 | if not sflows: |
| 63 | raise "No associated SFlow service" |
| 64 | |
| 65 | return sflows[0] |
| 66 | |
| 67 | def get_instance(self, o): |
| 68 | # We assume the SFlow service owns a slice, so pick one of the instances |
| 69 | # inside that slice to sync to. |
| 70 | |
| 71 | serv = self.get_sflow_service(o) |
| 72 | |
| 73 | if serv.slices.exists(): |
| 74 | slice = serv.slices.all()[0] |
| 75 | if slice.instances.exists(): |
| 76 | return slice.instances.all()[0] |
| 77 | |
| 78 | return None |
| 79 | |
| 80 | def get_extra_attributes(self, o): |
| 81 | instance = self.get_instance(o) |
| 82 | |
| 83 | fields={} |
| 84 | fields["sflow_api_base_url"] = self.get_sflow_service(o).sflow_api_url |
| 85 | fields["sflow_api_port"] = self.get_sflow_service(o).sflow_api_port |
| 86 | fields["listening_endpoint"] = o.listening_endpoint |
| 87 | fields["sflow_container"] = "sflowpubsub" |
| 88 | |
| 89 | return fields |
| 90 | |
| 91 | def sync_fields(self, o, fields): |
| 92 | # the super causes the playbook to be run |
| 93 | super(SyncSFlowTenant, self).sync_fields(o, fields) |
| 94 | |
| 95 | def run_playbook(self, o, fields): |
| 96 | super(SyncSFlowTenant, self).run_playbook(o, fields) |
| 97 | |
| 98 | def delete_record(self, m): |
| 99 | pass |