aboutsummaryrefslogtreecommitdiffstats
path: root/src/onapsdk/utils/headers_creator.py
blob: adb06098e273e26c2805e263d21cb6e00a14cf85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""Header creator package."""
#   Copyright 2022 Orange, Deutsche Telekom AG
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
from typing import Dict
from uuid import uuid4
import base64
import hashlib

from onapsdk.configuration import settings


def headers_sdc_creator(base_header: Dict[str, str],
                        user: str = "cs0008",
                        authorization: str = None):
    """
    Create the right headers for SDC creator type.

    Args:
        base_header (Dict[str, str]): the base header to use
        user (str, optional): the user to use. Default to cs0008
        authorization (str, optional): the basic auth to use.
                                       Default to "classic" one

    Returns:
        Dict[str, str]: the needed headers

    """
    return headers_sdc_generic(base_header, user, authorization=authorization)


def headers_sdc_tester(base_header: Dict[str, str],
                       user: str = "jm0007",
                       authorization: str = None):
    """
    Create the right headers for SDC tester type.

    Args:
        base_header (Dict[str, str]): the base header to use
        user (str, optional): the user to use. Default to jm0007
        authorization (str, optional): the basic auth to use.
                                       Default to "classic" one

    Returns:
        Dict[str, str]: the needed headers

    """
    return headers_sdc_generic(base_header, user, authorization=authorization)


def headers_sdc_governor(base_header: Dict[str, str],
                         user: str = "gv0001",
                         authorization: str = None):
    """
    Create the right headers for SDC governor type.

    Args:
        base_header (Dict[str, str]): the base header to use
        user (str, optional): the user to use. Default to gv0001
        authorization (str, optional): the basic auth to use.
                                       Default to "classic" one

    Returns:
        Dict[str, str]: the needed headers

    """
    return headers_sdc_generic(base_header, user, authorization=authorization)


def headers_sdc_operator(base_header: Dict[str, str],
                         user: str = "op0001",
                         authorization: str = None):
    """
    Create the right headers for SDC operator type.

    Args:
        base_header (Dict[str, str]): the base header to use
        user (str, optional): the user to use. Default to op0001
        authorization (str, optional): the basic auth to use.
                                       Default to "classic" one

    Returns:
        Dict[str, str]: the needed headers

    """
    return headers_sdc_generic(base_header, user, authorization=authorization)


def headers_sdc_generic(base_header: Dict[str, str],
                        user: str,
                        authorization: str = None):
    """
    Create the right headers for SDC generic type.

    Args:
        base_header (Dict[str, str]): the base header to use
        user (str): the user to use.
        authorization (str, optional): the basic auth to use.
                                       Default to "classic" one

    Returns:
        Dict[str, str]: the needed headers

    """
    headers = base_header.copy()
    headers["USER_ID"] = user
    headers["Authorization"] = authorization or settings.SDC_AUTH
    headers["X-ECOMP-InstanceID"] = "onapsdk"
    return headers


def headers_aai_creator(base_header: Dict[str, str]):
    """
    Create the right headers for AAI creator type.

    Args:
        base_header (Dict[str, str]): the base header to use

    Returns:
        Dict[str, str]: the needed headers

    """
    headers = base_header.copy()
    headers["x-fromappid"] = "AAI"
    headers["x-transactionid"] = "0a3f6713-ba96-4971-a6f8-c2da85a3176e"
    headers["authorization"] = settings.AAI_AUTH
    return headers


def headers_so_creator(base_header: Dict[str, str]):
    """
    Create the right headers for SO creator type.

    Args:
        base_header (Dict[str, str]): the base header to use

    Returns:
        Dict[str, str]: the needed headers

    """
    headers = base_header.copy()
    headers["x-fromappid"] = "AAI"
    headers["x-transactionid"] = str(uuid4())
    headers["authorization"] = settings.SO_AUTH
    headers["cache-control"] = "no-cache"
    return headers

def headers_so_catelog_db_creator(base_header: Dict[str, str]):
    """
    Create the right headers for SO creator type.

    Args:
        base_header (Dict[str, str]): the base header to use

    Returns:
        Dict[str, str]: the needed headers

    """
    headers = base_header.copy()
    headers["x-fromappid"] = "AAI"
    headers["x-transactionid"] = str(uuid4())
    headers["authorization"] = settings.SO_CAT_DB_AUTH
    headers["cache-control"] = "no-cache"
    return headers

def headers_msb_creator(base_header: Dict[str, str]):
    """
    Create the right headers for MSB.

    Args:
        base_header (Dict[str, str]): the base header to use

    Returns:
        Dict[str, str]: the needed headers

    """
    headers = base_header.copy()
    headers["cache-control"] = "no-cache"
    return headers


def headers_sdnc_creator(base_header: Dict[str, str]):
    """
    Create the right headers for SDNC.

    Args:
        base_header (Dict[str, str]): the base header to use

    Returns:
        Dict[str, str]: the needed headers

    """
    headers = base_header.copy()
    headers["authorization"] = settings.SDNC_AUTH
    headers["x-transactionid"] = str(uuid4())
    headers["x-fromappid"] = "API client"
    return headers


def headers_sdc_artifact_upload(base_header: Dict[str, str], data: str):
    """
    Create the right headers for sdc artifact upload.

    Args:
        base_header (Dict[str, str]): the base header to use
        data (str): payload data used to create an md5 content header

    Returns:
        Dict[str, str]: the needed headers

    """
    headers = base_header.copy()
    headers["Accept"] = "application/json, text/plain, */*"
    headers["Accept-Encoding"] = "gzip, deflate, br"
    headers["Content-Type"] = "application/json; charset=UTF-8"
    md5_content = hashlib.md5(data.encode('UTF-8')).hexdigest()
    content = base64.b64encode(md5_content.encode('ascii')).decode('UTF-8')
    headers["Content-MD5"] = content
    return headers

def headers_clamp_creator(base_header: Dict[str, str]):
    """
    Create the right headers for CLAMP generic type.

        base_header (Dict[str, str]): the base header to use
        data (str): payload data used to create an md5 content header

    Returns:
        Dict[str, str]: the needed headers

    """
    headers = base_header.copy()
    headers["Authorization"] = settings.CLAMP_AUTH
    headers["X-ECOMP-InstanceID"] = "onapsdk"
    return headers