blob: bcf6d53a199689eefcedb2e5248901f5d77e281a [file] [log] [blame]
Gopinath Taget22383d92018-08-22 12:28:52 -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
16import base64
17import jinja2
18import json
19from synchronizers.new_base.modelaccessor import *
20from synchronizers.new_base.policy import Policy
21
22from xosconfig import Config
23from multistructlog import create_logger
24
25log = create_logger(Config().get('logging'))
26
27class VEpcServiceInstancePolicy(Policy):
28 model_name = "VEpcServiceInstance"
29
30 def handle_create(self, service_instance):
31 return self.handle_update(service_instance)
32
33 # def render_index(self, service_instance):
34 # service = service_instance.owner.leaf_model
35
36 # fields = {}
37 # fields['tenant_message'] = service_instance.tenant_message
38 # fields['service_message'] = service.service_message
39
40 # if service_instance.foreground_color:
41 # fields["foreground_color"] = service_instance.foreground_color.html_code
42
43 # if service_instance.background_color:
44 # fields["background_color"] = service_instance.background_color.html_code
45
46 # images=[]
47 # for image in service_instance.embedded_images.all():
48 # images.append({"name": image.name,
49 # "url": image.url})
50 # fields["images"] = images
51
52 # template_fn = os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "index.html.j2")
53 # template = jinja2.Template(open(template_fn).read())
54
55 # return template.render(fields)
56
57 def handle_update(self, service_instance):
58 # if not service_instance.compute_instance:
59
60 # # TODO: Break dependency
61 # compute_service = KubernetesService.objects.first()
62 # compute_service_instance_class = Service.objects.get(id=compute_service.id).get_service_instance_class()
63
64 # vepcservice = service_instance.owner.leaf_model
65
66 # # TODO: What if there is the wrong number of slices?
67 # slice = vepcservice.slices.first()
68
69
70 # # TODO: What if there is no default image?
71 # # image = slice.default_image
72
73 # images = service_instance.images.all()
74
75 # # TODO: Gopi: Create the XOS Image object for each container
76 # # TODO: Gopi: Might have to extend the K8S synch to support multiple containers in a K8S pod
77 # # TODO: Gopi: Will have to extend the K8S sync to support StatefulSets
78
79 # docker_images = []
80 # for image in images:
81 # # TODO: Gopi: WORK ON THIS LOOP. THE Names need to be explicit now
82 # # TODO: slice = vepcservice.slices.get(name="mysite_vepcservice_mme")
83 # docker_image = "%s:%s" % (image.image_name, image.image_tag)
84 # compute_service_instance = compute_service_instance_class(slice=slice, owner=compute_service, image=docker_image, name=image.component_name) # Might need this at some point if I use configmap/secret: no_sync=True
85 # compute_service_instance.save()
86
87 # # # Create a configmap and attach it to the compute instance
88 # # data = {"index.html": self.render_index(service_instance)}
89 # # cfmap = KubernetesConfigMap(name="vepcserviceinstance-map-%s" % service_instance.id,
90 # # trust_domain=slice.trust_domain,
91 # # data=json.dumps(data))
92 # # cfmap.save()
93 # # cfmap_mnt = KubernetesConfigVolumeMount(config=cfmap,
94 # # service_instance=compute_service_instance,
95 # # mount_path="/usr/local/apache2/htdocs")
96 # # cfmap_mnt.save()
97
98 # # # Create a secret and attach it to the compute instance
99 # # data = {"service_secret.txt": base64.b64encode(str(vepcservice.service_secret)),
100 # # "tenant_secret.txt": base64.b64encode(str(service_instance.tenant_secret))}
101 # # secret = KubernetesSecret(name="vepcserviceinstance-secret-%s" % service_instance.id,
102 # # trust_domain=slice.trust_domain,
103 # # data=json.dumps(data))
104 # # secret.save()
105 # # secret_mnt = KubernetesSecretVolumeMount(secret=secret, service_instance=compute_service_instance, mount_path="/usr/local/apache2/secrets")
106 # # secret_mnt.save()
107
108 # # compute_service_instance.no_sync = False
109 # # compute_service_instance.save(update_fields=["no_sync"])
110
111 # # TODO: Gopi/Scott revisit this to make it more robust to declare
112 # # the K8S service instances dynamically
113 # service_instance.compute_instance_mme = compute_service_instance_mme
114 # service_instance.save(update_fields=["compute_instance_mme"])
115 # service_instance.compute_instance_hss = compute_service_instance_hss
116 # service_instance.save(update_fields=["compute_instance_hss"])
117 # service_instance.compute_instance_hssdb = compute_service_instance_hssdb
118 # service_instance.save(update_fields=["compute_instance_hssdb"])
119 # service_instance.compute_instance_spgw = compute_service_instance_spgw
120 # service_instance.save(update_fields=["compute_instance_spgw"])
121 # else:
122 # # TODO: Gopi: Need to handle the scenario of multiple compute instances representing the K8S Pods
123 # compute_instance = service_instance.compute_instance
124 # mnt = compute_instance.leaf_model.kubernetes_config_volume_mounts.first()
125 # config = mnt.config
126 # new_data = json.dumps({"index.html": self.render_index(service_instance)})
127 # if (new_data != config.data):
128 # config.data = new_data
129 # config.save(always_update_timestamp=True)
130 # # Force the Kubernetes syncstep
131 # compute_instance.save(always_update_timestamp=True)
132 pass
133
134 def handle_delete(self, service_instance):
135 # TODO: Gopi: Need to handle the scenario of multiple compute instances representing the K8S Pods
136 # log.info("handle_delete")
137 # if service_instance.compute_instance:
138 # log.info("has a compute_instance")
139 # service_instance.compute_instance.delete()
140 # service_instance.compute_instance = None
141 # # TODO: I'm not sure we can save things that are being deleted...
142 # service_instance.save(update_fields=["compute_instance"])
143 pass