Matteo Scandolo | 7781b5b | 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 | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 17 | import hashlib |
| 18 | import os |
| 19 | import socket |
| 20 | import sys |
| 21 | import base64 |
| 22 | import time |
Scott Baker | 91ee5e4 | 2017-03-14 10:33:52 -0700 | [diff] [blame] | 23 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 24 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 25 | from xos.logger import Logger, logging |
| 26 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 27 | logger = Logger(level=logging.INFO) |
| 28 | |
| 29 | class SyncONOSService(SyncInstanceUsingAnsible): |
| 30 | provides=[ONOSService] |
| 31 | observes=ONOSService |
| 32 | requested_interval=0 |
| 33 | template_name = "sync_onosservice.yaml" |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 34 | |
| 35 | def __init__(self, *args, **kwargs): |
| 36 | super(SyncONOSService, self).__init__(*args, **kwargs) |
| 37 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 38 | def get_instance(self, o): |
| 39 | # We assume the ONOS service owns a slice, so pick one of the instances |
| 40 | # inside that slice to sync to. |
| 41 | |
| 42 | serv = o |
| 43 | |
| 44 | if serv.slices.exists(): |
| 45 | slice = serv.slices.all()[0] |
| 46 | if slice.instances.exists(): |
| 47 | return slice.instances.all()[0] |
| 48 | |
| 49 | return None |
| 50 | |
| 51 | def get_extra_attributes(self, o): |
| 52 | fields={} |
| 53 | fields["instance_hostname"] = self.get_instance(o).instance_name.replace("_","-") |
| 54 | fields["appname"] = o.name |
| 55 | fields["ONOS_container"] = "ONOS" |
| 56 | return fields |
| 57 | |
| 58 | def sync_record(self, o): |
| 59 | if o.no_container: |
| 60 | logger.info("no work to do for onos service, because o.no_container is set",extra=o.tologdict()) |
| 61 | o.save() |
| 62 | else: |
| 63 | super(SyncONOSService, self).sync_record(o) |
| 64 | |
| 65 | def sync_fields(self, o, fields): |
| 66 | # the super causes the playbook to be run |
| 67 | super(SyncONOSService, self).sync_fields(o, fields) |
| 68 | |
| 69 | def run_playbook(self, o, fields): |
| 70 | instance = self.get_instance(o) |
| 71 | if (instance.isolation=="container"): |
| 72 | # If the instance is already a container, then we don't need to |
| 73 | # install ONOS. |
| 74 | return |
| 75 | super(SyncONOSService, self).run_playbook(o, fields) |
| 76 | |
| 77 | def delete_record(self, m): |
| 78 | pass |