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 sys |
| 21 | import base64 |
| 22 | import time |
| 23 | from django.db.models import F, Q |
| 24 | from xos.config import Config |
Murat Parlakisik | 638c65f | 2017-05-31 11:10:24 +0300 | [diff] [blame] | 25 | from synchronizers.new_base.syncstep import SyncStep |
| 26 | from synchronizers.new_base.ansible_helper import run_template_ssh |
| 27 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 28 | from modelaccessor import * |
| 29 | #from core.models import Service, Slice |
| 30 | #from services.monitoring.models import SFlowService |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 31 | from xos.logger import Logger, logging |
| 32 | |
| 33 | # hpclibrary will be in steps/.. |
| 34 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 35 | sys.path.insert(0,parentdir) |
| 36 | |
| 37 | logger = Logger(level=logging.INFO) |
| 38 | |
| 39 | class SyncSFlowService(SyncInstanceUsingAnsible): |
| 40 | provides=[SFlowService] |
| 41 | observes=SFlowService |
| 42 | requested_interval=0 |
| 43 | template_name = "sync_sflowservice.yaml" |
Srikanth Vavilapalli | d84b7b7 | 2016-06-28 00:19:07 +0000 | [diff] [blame] | 44 | service_key_name = "/opt/xos/synchronizers/monitoring/monitoring_channel_private_key" |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 45 | |
| 46 | def __init__(self, *args, **kwargs): |
| 47 | super(SyncSFlowService, self).__init__(*args, **kwargs) |
| 48 | |
| 49 | def fetch_pending(self, deleted): |
| 50 | if (not deleted): |
| 51 | objs = SFlowService.get_service_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 52 | else: |
| 53 | objs = SFlowService.get_deleted_service_objects() |
| 54 | |
| 55 | return objs |
| 56 | |
| 57 | def get_instance(self, o): |
| 58 | # We assume the ONOS service owns a slice, so pick one of the instances |
| 59 | # inside that slice to sync to. |
| 60 | |
| 61 | serv = o |
| 62 | |
| 63 | if serv.slices.exists(): |
| 64 | slice = serv.slices.all()[0] |
| 65 | if slice.instances.exists(): |
| 66 | return slice.instances.all()[0] |
| 67 | |
| 68 | return None |
| 69 | |
| 70 | def get_extra_attributes(self, o): |
| 71 | fields={} |
| 72 | fields["instance_hostname"] = self.get_instance(o).instance_name.replace("_","-") |
| 73 | fields["sflow_port"] = o.sflow_port |
| 74 | fields["sflow_api_port"] = o.sflow_api_port |
| 75 | fields["sflow_container"] = "sflowpubsub" |
| 76 | return fields |
| 77 | |
| 78 | def sync_fields(self, o, fields): |
| 79 | # the super causes the playbook to be run |
| 80 | super(SyncSFlowService, self).sync_fields(o, fields) |
| 81 | |
| 82 | def run_playbook(self, o, fields): |
| 83 | instance = self.get_instance(o) |
| 84 | if (instance.isolation=="container"): |
| 85 | # If the instance is already a container, then we don't need to |
| 86 | # install ONOS. |
| 87 | return |
| 88 | super(SyncSFlowService, self).run_playbook(o, fields) |
| 89 | |
| 90 | def delete_record(self, m): |
| 91 | pass |