blob: 759dc670d619ce264312668f43de777d127c30f6 [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 Baker3bce78b2017-09-21 12:45:45 -070027from synchronizers.new_base.syncstep import DeferredException
Scott Baker91ee5e42017-03-14 10:33:52 -070028from synchronizers.new_base.ansible_helper import run_template
29from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
30from synchronizers.new_base.modelaccessor import *
Scott Baker7a327592016-06-20 17:34:06 -070031from xos.logger import Logger, logging
Scott Baker7a327592016-06-20 17:34:06 -070032
Scott Baker7a327592016-06-20 17:34:06 -070033logger = Logger(level=logging.INFO)
34
Matteo Scandolo0d41a862016-09-01 13:21:41 -070035
Scott Baker7a327592016-06-20 17:34:06 -070036class SyncONOSApp(SyncInstanceUsingAnsible):
37 provides=[ONOSApp]
38 observes=ONOSApp
39 requested_interval=0
40 template_name = "sync_onosapp.yaml"
Scott Baker7a327592016-06-20 17:34:06 -070041
42 def __init__(self, *args, **kwargs):
43 super(SyncONOSApp, self).__init__(*args, **kwargs)
44
Scott Baker7a327592016-06-20 17:34:06 -070045 def get_instance(self, o):
46 # We assume the ONOS service owns a slice, so pick one of the instances
47 # inside that slice to sync to.
48
49 serv = self.get_onos_service(o)
50
51 if serv.no_container:
52 raise Exception("get_instance() was called on a service that was marked no_container")
53
54 if serv.slices.exists():
55 slice = serv.slices.all()[0]
56 if slice.instances.exists():
57 return slice.instances.all()[0]
58
59 return None
60
61 def get_onos_service(self, o):
Scott Baker0c62dfa2017-11-21 09:15:43 -080062 return o.owner.leaf_model
Scott Baker7a327592016-06-20 17:34:06 -070063
64 def is_no_container(self, o):
65 return self.get_onos_service(o).no_container
66
67 def skip_ansible_fields(self, o):
68 return self.is_no_container(o)
69
70 def get_files_dir(self, o):
Matteo Scandolod3744122017-06-02 16:09:31 -070071 step_dir = Config.get("steps_dir")
Scott Baker7a327592016-06-20 17:34:06 -070072
73 return os.path.join(step_dir, "..", "files", str(self.get_onos_service(o).id), o.name)
74
75 def get_cluster_configuration(self, o):
76 instance = self.get_instance(o)
77 if not instance:
78 raise Exception("No instance for ONOS App")
79 node_ips = [socket.gethostbyname(instance.node.name)]
80
81 ipPrefix = ".".join(node_ips[0].split(".")[:3]) + ".*"
82 result = '{ "nodes": ['
83 result = result + ",".join(['{ "ip": "%s"}' % ip for ip in node_ips])
84 result = result + '], "ipPrefix": "%s"}' % ipPrefix
85 return result
86
87 def get_dynamic_parameter_value(self, o, param):
88 instance = self.get_instance(o)
89 if not instance:
90 raise Exception("No instance for ONOS App")
91 if param == 'rabbit_host':
92 return instance.controller.rabbit_host
93 if param == 'rabbit_user':
94 return instance.controller.rabbit_user
95 if param == 'rabbit_password':
96 return instance.controller.rabbit_password
97 if param == 'keystone_tenant_id':
98 cslice = ControllerSlice.objects.get(slice=instance.slice)
99 if not cslice:
100 raise Exception("Controller slice object for %s does not exist" % instance.slice.name)
101 return cslice.tenant_id
102 if param == 'keystone_user_id':
103 cuser = ControllerUser.objects.get(user=instance.creator)
104 if not cuser:
105 raise Exception("Controller user object for %s does not exist" % instance.creator)
106 return cuser.kuser_id
107
Scott Baker7a327592016-06-20 17:34:06 -0700108 def write_configs(self, o):
Scott Bakerf458f682017-03-14 17:10:36 -0700109 if hasattr(o, "create_attr"):
110 # new API doesn't let us setattr for things that don't already exist
111 o.create_attr("config_fns")
112 o.create_attr("rest_configs")
113 o.create_attr("component_configs")
114 o.create_attr("files_dir")
115 o.create_attr("node_key_fn")
116 o.create_attr("early_rest_configs")
117
Scott Baker7a327592016-06-20 17:34:06 -0700118 o.config_fns = []
119 o.rest_configs = []
120 o.component_configs = []
121 o.files_dir = self.get_files_dir(o)
122
123 if not os.path.exists(o.files_dir):
124 os.makedirs(o.files_dir)
125
126 # Combine the service attributes with the tenant attributes. Tenant
127 # attribute can override service attributes.
Scott Baker8d8f9922017-07-17 17:30:42 -0700128 attrs = o.owner.serviceattribute_dict
Scott Baker7a327592016-06-20 17:34:06 -0700129 attrs.update(o.tenantattribute_dict)
130
Scott Baker3bce78b2017-09-21 12:45:45 -0700131 # Check to see if we're waiting on autoconfig
132 if (attrs.get("autogenerate") in ["vtn-network-cfg",]) and \
133 (not attrs.get("rest_onos/v1/network/configuration/")):
134 raise DeferredException("Network configuration is not populated yet")
135
Scott Baker7a327592016-06-20 17:34:06 -0700136 ordered_attrs = attrs.keys()
137
138 onos = self.get_onos_service(o)
139 if onos.node_key:
140 file(os.path.join(o.files_dir, "node_key"),"w").write(onos.node_key)
141 o.node_key_fn="node_key"
142 else:
143 o.node_key_fn=None
144
145 o.early_rest_configs=[]
146 if ("cordvtn" in o.dependencies) and (not self.is_no_container(o)):
147 # For VTN, since it's running in a docker host container, we need
148 # to make sure it configures the cluster using the right ip addresses.
149 # NOTE: rest_onos/v1/cluster/configuration/ will reboot the cluster and
150 # must go first.
151 name="rest_onos/v1/cluster/configuration/"
152 value= self.get_cluster_configuration(o)
153 fn = name[5:].replace("/","_")
154 endpoint = name[5:]
155 file(os.path.join(o.files_dir, fn),"w").write(" " +value)
156 o.early_rest_configs.append( {"endpoint": endpoint, "fn": fn} )
157
Scott Baker7a327592016-06-20 17:34:06 -0700158 for name in attrs.keys():
159 value = attrs[name]
160 if name.startswith("config_"):
161 fn = name[7:] # .replace("_json",".json")
162 o.config_fns.append(fn)
163 file(os.path.join(o.files_dir, fn),"w").write(value)
164 if name.startswith("rest_"):
165 fn = name[5:].replace("/","_")
166 endpoint = name[5:]
167 # Ansible goes out of it's way to make our life difficult. If
168 # 'lookup' sees a file that it thinks contains json, then it'll
169 # insist on parsing and return a json object. We just want
170 # a string, so prepend a space and then strip the space off
171 # later.
172 file(os.path.join(o.files_dir, fn),"w").write(" " +value)
173 o.rest_configs.append( {"endpoint": endpoint, "fn": fn} )
174 if name.startswith("component_config"):
175 components = json.loads(value,object_pairs_hook=OrderedDict)
176 for component in components.keys():
177 config = components[component]
178 for key in config.keys():
179 config_val = config[key]
180 found = re.findall('<(.+?)>',config_val)
181 for x in found:
182 #Get value corresponding to that string
183 val = self.get_dynamic_parameter_value(o, x)
184 if val:
185 config_val = re.sub('<'+x+'>', val, config_val)
186 #TODO: else raise an exception?
187 o.component_configs.append( {"component": component, "config_params": "'{\""+key+"\":\""+config_val+"\"}'"} )
188
189 def prepare_record(self, o):
190 self.write_configs(o)
191
192 def get_extra_attributes_common(self, o):
193 fields = {}
194
195 # These are attributes that are not dependent on Instance. For example,
196 # REST API stuff.
197
198 onos = self.get_onos_service(o)
199
200 fields["files_dir"] = o.files_dir
201 fields["appname"] = o.name
202 fields["rest_configs"] = o.rest_configs
Scott Bakerd84abe82017-10-19 09:51:10 -0700203 fields["component_configs"] = o.component_configs
Scott Baker7a327592016-06-20 17:34:06 -0700204 fields["rest_hostname"] = onos.rest_hostname
Scott Baker0c62dfa2017-11-21 09:15:43 -0800205 fields["rest_username"] = onos.rest_username
206 fields["rest_password"] = onos.rest_password
Scott Baker7a327592016-06-20 17:34:06 -0700207 fields["rest_port"] = onos.rest_port
208
209 if o.dependencies:
210 fields["dependencies"] = [x.strip() for x in o.dependencies.split(",")]
211 else:
212 fields["dependencies"] = []
213
Andy Bavier4a503b12016-06-21 11:41:29 -0400214 if o.install_dependencies:
215 fields["install_dependencies"] = [x.strip() for x in o.install_dependencies.split(",")]
216 else:
217 fields["install_dependencies"] = []
218
Scott Baker7a327592016-06-20 17:34:06 -0700219 return fields
220
221 def get_extra_attributes_full(self, o):
222 instance = self.get_instance(o)
223
224 fields = self.get_extra_attributes_common(o)
225
226 fields["config_fns"] = o.config_fns
227 fields["early_rest_configs"] = o.early_rest_configs
Scott Baker7a327592016-06-20 17:34:06 -0700228 fields["node_key_fn"] = o.node_key_fn
229
Scott Baker7a327592016-06-20 17:34:06 -0700230 if (instance.isolation=="container"):
231 fields["ONOS_container"] = "%s-%s" % (instance.slice.name, str(instance.id))
232 else:
233 fields["ONOS_container"] = "ONOS"
234 return fields
235
236 def get_extra_attributes(self, o):
237 if self.is_no_container(o):
238 return self.get_extra_attributes_common(o)
239 else:
240 return self.get_extra_attributes_full(o)
241
242 def sync_fields(self, o, fields):
243 # the super causes the playbook to be run
244 super(SyncONOSApp, self).sync_fields(o, fields)
245
246 def run_playbook(self, o, fields):
247 if self.is_no_container(o):
248 # There is no machine to SSH to, so use the synchronizer's
249 # run_template method directly.
Sapan Bhatia81405522017-02-04 09:26:31 -0800250 run_template("sync_onosapp_nocontainer.yaml", fields, object=o)
Scott Baker7a327592016-06-20 17:34:06 -0700251 else:
252 super(SyncONOSApp, self).run_playbook(o, fields)
253
254 def delete_record(self, m):
255 pass