blob: c8084d13aa44245cee6f00f27c1ee8a91f5442c4 [file] [log] [blame]
Matteo Scandolo7781b5b2017-08-08 13:05:26 -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 Baker7a327592016-06-20 17:34:06 -070017import hashlib
18import os
19import socket
20import sys
21import base64
22import time
23import re
24import json
25from collections import OrderedDict
Matteo Scandolod3744122017-06-02 16:09:31 -070026from xosconfig import Config
Scott Baker91ee5e42017-03-14 10:33:52 -070027from synchronizers.new_base.ansible_helper import run_template
28from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
29from synchronizers.new_base.modelaccessor import *
Scott Baker7a327592016-06-20 17:34:06 -070030from xos.logger import Logger, logging
Scott Baker7a327592016-06-20 17:34:06 -070031
Scott Baker7a327592016-06-20 17:34:06 -070032logger = Logger(level=logging.INFO)
33
Matteo Scandolo0d41a862016-09-01 13:21:41 -070034
Scott Baker7a327592016-06-20 17:34:06 -070035class SyncONOSApp(SyncInstanceUsingAnsible):
36 provides=[ONOSApp]
37 observes=ONOSApp
38 requested_interval=0
39 template_name = "sync_onosapp.yaml"
Scott Baker7a327592016-06-20 17:34:06 -070040
41 def __init__(self, *args, **kwargs):
42 super(SyncONOSApp, self).__init__(*args, **kwargs)
43
Scott Baker7a327592016-06-20 17:34:06 -070044 def get_instance(self, o):
45 # We assume the ONOS service owns a slice, so pick one of the instances
46 # inside that slice to sync to.
47
48 serv = self.get_onos_service(o)
49
50 if serv.no_container:
51 raise Exception("get_instance() was called on a service that was marked no_container")
52
53 if serv.slices.exists():
54 slice = serv.slices.all()[0]
55 if slice.instances.exists():
56 return slice.instances.all()[0]
57
58 return None
59
60 def get_onos_service(self, o):
Scott Baker8d8f9922017-07-17 17:30:42 -070061 if not o.owner:
Scott Baker7a327592016-06-20 17:34:06 -070062 return None
63
Scott Baker8d8f9922017-07-17 17:30:42 -070064 onoses = ONOSService.objects.filter(id=o.owner.id)
Scott Baker7a327592016-06-20 17:34:06 -070065 if not onoses:
66 return None
67
68 return onoses[0]
69
70 def is_no_container(self, o):
71 return self.get_onos_service(o).no_container
72
73 def skip_ansible_fields(self, o):
74 return self.is_no_container(o)
75
76 def get_files_dir(self, o):
Matteo Scandolod3744122017-06-02 16:09:31 -070077 step_dir = Config.get("steps_dir")
Scott Baker7a327592016-06-20 17:34:06 -070078
79 return os.path.join(step_dir, "..", "files", str(self.get_onos_service(o).id), o.name)
80
81 def get_cluster_configuration(self, o):
82 instance = self.get_instance(o)
83 if not instance:
84 raise Exception("No instance for ONOS App")
85 node_ips = [socket.gethostbyname(instance.node.name)]
86
87 ipPrefix = ".".join(node_ips[0].split(".")[:3]) + ".*"
88 result = '{ "nodes": ['
89 result = result + ",".join(['{ "ip": "%s"}' % ip for ip in node_ips])
90 result = result + '], "ipPrefix": "%s"}' % ipPrefix
91 return result
92
93 def get_dynamic_parameter_value(self, o, param):
94 instance = self.get_instance(o)
95 if not instance:
96 raise Exception("No instance for ONOS App")
97 if param == 'rabbit_host':
98 return instance.controller.rabbit_host
99 if param == 'rabbit_user':
100 return instance.controller.rabbit_user
101 if param == 'rabbit_password':
102 return instance.controller.rabbit_password
103 if param == 'keystone_tenant_id':
104 cslice = ControllerSlice.objects.get(slice=instance.slice)
105 if not cslice:
106 raise Exception("Controller slice object for %s does not exist" % instance.slice.name)
107 return cslice.tenant_id
108 if param == 'keystone_user_id':
109 cuser = ControllerUser.objects.get(user=instance.creator)
110 if not cuser:
111 raise Exception("Controller user object for %s does not exist" % instance.creator)
112 return cuser.kuser_id
113
Scott Baker7a327592016-06-20 17:34:06 -0700114 def write_configs(self, o):
Scott Bakerf458f682017-03-14 17:10:36 -0700115 if hasattr(o, "create_attr"):
116 # new API doesn't let us setattr for things that don't already exist
117 o.create_attr("config_fns")
118 o.create_attr("rest_configs")
119 o.create_attr("component_configs")
120 o.create_attr("files_dir")
121 o.create_attr("node_key_fn")
122 o.create_attr("early_rest_configs")
123
Scott Baker7a327592016-06-20 17:34:06 -0700124 o.config_fns = []
125 o.rest_configs = []
126 o.component_configs = []
127 o.files_dir = self.get_files_dir(o)
128
129 if not os.path.exists(o.files_dir):
130 os.makedirs(o.files_dir)
131
132 # Combine the service attributes with the tenant attributes. Tenant
133 # attribute can override service attributes.
Scott Baker8d8f9922017-07-17 17:30:42 -0700134 attrs = o.owner.serviceattribute_dict
Scott Baker7a327592016-06-20 17:34:06 -0700135 attrs.update(o.tenantattribute_dict)
136
137 ordered_attrs = attrs.keys()
138
139 onos = self.get_onos_service(o)
140 if onos.node_key:
141 file(os.path.join(o.files_dir, "node_key"),"w").write(onos.node_key)
142 o.node_key_fn="node_key"
143 else:
144 o.node_key_fn=None
145
146 o.early_rest_configs=[]
147 if ("cordvtn" in o.dependencies) and (not self.is_no_container(o)):
148 # For VTN, since it's running in a docker host container, we need
149 # to make sure it configures the cluster using the right ip addresses.
150 # NOTE: rest_onos/v1/cluster/configuration/ will reboot the cluster and
151 # must go first.
152 name="rest_onos/v1/cluster/configuration/"
153 value= self.get_cluster_configuration(o)
154 fn = name[5:].replace("/","_")
155 endpoint = name[5:]
156 file(os.path.join(o.files_dir, fn),"w").write(" " +value)
157 o.early_rest_configs.append( {"endpoint": endpoint, "fn": fn} )
158
Scott Baker7a327592016-06-20 17:34:06 -0700159 for name in attrs.keys():
160 value = attrs[name]
161 if name.startswith("config_"):
162 fn = name[7:] # .replace("_json",".json")
163 o.config_fns.append(fn)
164 file(os.path.join(o.files_dir, fn),"w").write(value)
165 if name.startswith("rest_"):
166 fn = name[5:].replace("/","_")
167 endpoint = name[5:]
168 # Ansible goes out of it's way to make our life difficult. If
169 # 'lookup' sees a file that it thinks contains json, then it'll
170 # insist on parsing and return a json object. We just want
171 # a string, so prepend a space and then strip the space off
172 # later.
173 file(os.path.join(o.files_dir, fn),"w").write(" " +value)
174 o.rest_configs.append( {"endpoint": endpoint, "fn": fn} )
175 if name.startswith("component_config"):
176 components = json.loads(value,object_pairs_hook=OrderedDict)
177 for component in components.keys():
178 config = components[component]
179 for key in config.keys():
180 config_val = config[key]
181 found = re.findall('<(.+?)>',config_val)
182 for x in found:
183 #Get value corresponding to that string
184 val = self.get_dynamic_parameter_value(o, x)
185 if val:
186 config_val = re.sub('<'+x+'>', val, config_val)
187 #TODO: else raise an exception?
188 o.component_configs.append( {"component": component, "config_params": "'{\""+key+"\":\""+config_val+"\"}'"} )
189
190 def prepare_record(self, o):
191 self.write_configs(o)
192
193 def get_extra_attributes_common(self, o):
194 fields = {}
195
196 # These are attributes that are not dependent on Instance. For example,
197 # REST API stuff.
198
199 onos = self.get_onos_service(o)
200
201 fields["files_dir"] = o.files_dir
202 fields["appname"] = o.name
203 fields["rest_configs"] = o.rest_configs
204 fields["rest_hostname"] = onos.rest_hostname
205 fields["rest_port"] = onos.rest_port
206
207 if o.dependencies:
208 fields["dependencies"] = [x.strip() for x in o.dependencies.split(",")]
209 else:
210 fields["dependencies"] = []
211
Andy Bavier4a503b12016-06-21 11:41:29 -0400212 if o.install_dependencies:
213 fields["install_dependencies"] = [x.strip() for x in o.install_dependencies.split(",")]
214 else:
215 fields["install_dependencies"] = []
216
Scott Baker7a327592016-06-20 17:34:06 -0700217 return fields
218
219 def get_extra_attributes_full(self, o):
220 instance = self.get_instance(o)
221
222 fields = self.get_extra_attributes_common(o)
223
224 fields["config_fns"] = o.config_fns
225 fields["early_rest_configs"] = o.early_rest_configs
226 fields["component_configs"] = o.component_configs
227 fields["node_key_fn"] = o.node_key_fn
228
Scott Baker7a327592016-06-20 17:34:06 -0700229 if (instance.isolation=="container"):
230 fields["ONOS_container"] = "%s-%s" % (instance.slice.name, str(instance.id))
231 else:
232 fields["ONOS_container"] = "ONOS"
233 return fields
234
235 def get_extra_attributes(self, o):
236 if self.is_no_container(o):
237 return self.get_extra_attributes_common(o)
238 else:
239 return self.get_extra_attributes_full(o)
240
241 def sync_fields(self, o, fields):
242 # the super causes the playbook to be run
243 super(SyncONOSApp, self).sync_fields(o, fields)
244
245 def run_playbook(self, o, fields):
246 if self.is_no_container(o):
247 # There is no machine to SSH to, so use the synchronizer's
248 # run_template method directly.
Sapan Bhatia81405522017-02-04 09:26:31 -0800249 run_template("sync_onosapp_nocontainer.yaml", fields, object=o)
Scott Baker7a327592016-06-20 17:34:06 -0700250 else:
251 super(SyncONOSApp, self).run_playbook(o, fields)
252
253 def delete_record(self, m):
254 pass