blob: e3ff7af6aa108ab40c73ae07e9d951eff5021513 [file] [log] [blame]
Matteo Scandoloa86e0a12017-08-08 13:05:27 -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 Baker171d35e2016-06-20 17:36:29 -070017import os
18import socket
19import sys
20import base64
21import time
Scott Baker5809ed42017-03-07 10:45:00 -080022from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
23from synchronizers.new_base.modelaccessor import *
Scott Baker171d35e2016-06-20 17:36:29 -070024from xos.logger import Logger, logging
25
26# hpclibrary will be in steps/..
27parentdir = os.path.join(os.path.dirname(__file__),"..")
28sys.path.insert(0,parentdir)
29
30logger = Logger(level=logging.INFO)
31
Scott Baker171d35e2016-06-20 17:36:29 -070032class SyncVTRTenant(SyncInstanceUsingAnsible):
33 provides=[VTRTenant]
34 observes=VTRTenant
35 requested_interval=0
36 template_name = "sync_vtrtenant.yaml"
Scott Baker171d35e2016-06-20 17:36:29 -070037
38 def __init__(self, *args, **kwargs):
39 super(SyncVTRTenant, self).__init__(*args, **kwargs)
40
Scott Baker171d35e2016-06-20 17:36:29 -070041 def get_vtr_service(self, o):
Scott Bakerc7958a92017-07-18 11:07:00 -070042 if not o.owner:
Scott Baker171d35e2016-06-20 17:36:29 -070043 return None
44
Scott Bakerc7958a92017-07-18 11:07:00 -070045 # cast from Service to VTRService
46 vtrs = VTRService.objects.filter(id=o.owner.id)
Scott Baker171d35e2016-06-20 17:36:29 -070047 if not vtrs:
48 return None
49
50 return vtrs[0]
51
Scott Baker5db44a92017-03-06 17:27:52 -080052 def get_target(self, o):
53 target = o.target
54 if target:
Scott Baker3bbf1e92017-03-07 12:06:06 -080055 model_name = getattr(target, "model_name", target.__class__.__name__)
Scott Bakerc7958a92017-07-18 11:07:00 -070056 if model_name in ["ServiceInstance", "CordSubscriberRoot"]:
57 # cast from ServiceInstance to CordSubscriberRoot
58 csrs = CordSubscriberRoot.objects.filter(id=target.id)
59 if csrs:
60 return csrs[0]
Scott Baker5db44a92017-03-06 17:27:52 -080061 return None
62
Scott Baker171d35e2016-06-20 17:36:29 -070063 def get_vcpe_service(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080064 target = self.get_target(o)
65 if target and target.volt and target.volt.vcpe:
Scott Bakerc7958a92017-07-18 11:07:00 -070066 return target.volt.vcpe
Scott Baker171d35e2016-06-20 17:36:29 -070067 return None
68
69 def get_instance(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080070 target = self.get_target(o)
71 if target and target.volt and target.volt.vcpe:
72 return target.volt.vcpe.instance
Scott Baker171d35e2016-06-20 17:36:29 -070073 else:
74 return None
75
76 def get_key_name(self, instance):
Scott Baker5809ed42017-03-07 10:45:00 -080077# if instance.slice.service and (instance.slice.service.kind==VCPE_KIND):
78# # We need to use the vsg service's private key. Onboarding won't
79# # by default give us another service's private key, so let's assume
80# # onboarding has been configured to add vsg_rsa to the vtr service.
81# return "/opt/xos/services/vtr/keys/vsg_rsa"
82
83 if instance.slice and instance.slice.service and instance.slice.service.private_key_fn:
84 # Assume the service has shared its key with VTR.
85 # Look for the instance's service key name in VTR's key directory.
86 service_keyfn = instance.slice.service.private_key_fn
87 return os.path.join("/opt/xos/services/vtr/keys", os.path.basename(service_keyfn))
Scott Baker171d35e2016-06-20 17:36:29 -070088 else:
89 raise Exception("VTR doesn't know how to get the private key for this instance")
90
91 def get_extra_attributes(self, o):
92 vtr_service = self.get_vtr_service(o)
93 vcpe_service = self.get_vcpe_service(o)
94
95 if not vcpe_service:
96 raise Exception("No vcpeservice")
97
98 instance = self.get_instance(o)
99
100 if not instance:
101 raise Exception("No instance")
102
Scott Baker5db44a92017-03-06 17:27:52 -0800103 target = self.get_target(o)
104
Scott Baker171d35e2016-06-20 17:36:29 -0700105 s_tags = []
106 c_tags = []
Scott Baker5db44a92017-03-06 17:27:52 -0800107 if target and target.volt:
108 s_tags.append(target.volt.s_tag)
109 c_tags.append(target.volt.c_tag)
Scott Baker171d35e2016-06-20 17:36:29 -0700110
111 fields = {"s_tags": s_tags,
112 "c_tags": c_tags,
113 "isolation": instance.isolation,
114 "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]),
Scott Baker3bbf1e92017-03-07 12:06:06 -0800115# "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")],
Scott Baker171d35e2016-06-20 17:36:29 -0700116 "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]),
117 "resultcode_fn": "code-%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) }
118
119 # add in the sync_attributes that come from the vSG object
120 # this will be wan_ip, wan_mac, wan_container_ip, wan_container_mac, ...
Scott Baker3a5b8e12017-03-31 14:22:20 -0700121 if target and target.volt and target.volt.vcpe:
122 for attribute_name in ["wan_vm_ip", "wan_container_ip"]:
123 if hasattr(target.volt.vcpe, attribute_name):
124 fields[attribute_name] = getattr(target.volt.vcpe, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700125
126 # add in the sync_attributes that come from the SubscriberRoot object
Scott Baker3bbf1e92017-03-07 12:06:06 -0800127# if target and hasattr(target, "sync_attributes"):
128# for attribute_name in target.sync_attributes:
129# fields[attribute_name] = getattr(target, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700130
Scott Baker3bbf1e92017-03-07 12:06:06 -0800131 for attribute_name in ["scope", "test", "argument"]: # o.sync_attributes:
Scott Baker171d35e2016-06-20 17:36:29 -0700132 fields[attribute_name] = getattr(o,attribute_name)
133
134 return fields
135
136 def sync_fields(self, o, fields):
137 # the super causes the playbook to be run
138
139 super(SyncVTRTenant, self).sync_fields(o, fields)
140
141 def run_playbook(self, o, fields):
142 o.result = ""
143
144 result_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["result_fn"])
145 if os.path.exists(result_fn):
146 os.remove(result_fn)
147
148 resultcode_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["resultcode_fn"])
149 if os.path.exists(resultcode_fn):
150 os.remove(resultcode_fn)
151
152 super(SyncVTRTenant, self).run_playbook(o, fields)
153
154 if os.path.exists(result_fn):
155 o.result = open(result_fn).read()
156
157 if os.path.exists(resultcode_fn):
158 o.result_code = open(resultcode_fn).read()
159
160
161 def delete_record(self, m):
162 pass