5883f29becd5ce02e196ca1664532336119d573d
[dcaegen2/platform/plugins.git] / k8s / msb / msb.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 #
21
22 import uuid
23 import json
24
25 MSB_ANNOTATION_KEY = 'msb.onap.org/service-info'
26
27 def _sanitize_service_info(service_info):
28     '''
29     Sanitize a dict containing the MSB annotation parameters for registering
30     a service with MSB.  (Not bullet proof, but useful.)
31     MSB registration happens asynchronously from the installation flow:  an MSB process
32     watches the k8s event stream for new Service creations and looks for the MSB annotation.
33     A bad annotation will fail silently.  This sanitization process should make sure that something
34     gets put into    the MSB's list of services, so that the problem can be seen.
35
36     service_info is a dict containing the MSB annotation parameters.
37     -- serviceName: the name under which the service is to be registered (default: random--pretty useless!)
38     -- port: the container port on which the service can be contacted (default: "80"--nearly as useless)
39     -- version: the API version (default: "v1")
40     -- url: the path to the application's API endpoint (default: "/")
41     -- protocol: see the MSB documentation--the default is usually OK (default: "REST")
42     -- enable_ssl: a flag indicating if the service uses SSL (True) or not (False) (default: True)
43     -- visualRange: "1" means the service is exposed only in ONAP, "0" means externally (default: "1")
44        (Note this is a string value)
45     '''
46     return {
47         'serviceName': service_info.get('serviceName', str(uuid.uuid4())),
48         'port': str(service_info.get('port', '80')),
49         'version': service_info.get('version','v1'),
50         'url': service_info.get('url','/'),
51         'protocol': service_info.get('protocol','REST'),
52         'enable_ssl': bool(service_info.get('enable_ssl', False)),
53         'visualRange': str(service_info.get('visualRange', '1'))
54     }
55
56 def create_msb_annotation(msb_service_list):
57     '''
58     Creates an annotation that can be added to a k8s Service to trigger
59     registration with MSB.
60     msb_list is a list of dicts each containing MSB registration information for a
61     service.  (One k8s Service can have multiple ports, each one of which can be 
62     registered as an MSB service.)
63     '''
64     return {MSB_ANNOTATION_KEY : json.dumps([_sanitize_service_info(service_info) for service_info in msb_service_list])}