aboutsummaryrefslogtreecommitdiffstats
path: root/services/api/api_virtual_function.py
blob: 46610e83d9bcbd67fbe5e82808d9b4c51f4e7a51 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# ============LICENSE_START==========================================
# org.onap.vvp/test-engine
# ===================================================================
# Copyright © 2017 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the “License”);
# you may not use this software 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.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# 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.
#
# ============LICENSE_END============================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
import datetime
import json
import time

from django.conf import settings
import requests

from services.api.api_gitlab import APIGitLab
from services.api.api_user import APIUser
from services.constants import Constants, ServiceProvider
from services.database.db_general import DBGeneral
from services.helper import Helper
from services.logging_service import LoggingServiceFactory


logger = LoggingServiceFactory.get_logger()


class APIVirtualFunction:

    @staticmethod
    def add_next_step(user_content, files=[]):
        r1 = None
        postURL = settings.ICE_EM_URL + '/v1/engmgr/engagements/' + \
            user_content['engagement_uuid'] + '/nextsteps'
        logger.debug("Post add next step URL: " + postURL)
        headers = dict()  # Create header for post request.
        headers['Content-type'] = 'application/json'
        headers['Authorization'] = user_content['session_token']
        data = dict()  # Create JSON data for post request.
        files_list = list()
        if type(files) is list:
            for file in files:
                files_list.append(file)
        else:
            files_list.append(files)
        data['files'] = files_list
        data['assigneesUuids'] = [user_content['uuid']]
        data['duedate'] = str(datetime.date.today())
        data['description'] = "API test - add next step."
        list_data = []
        list_data.append(data)
        try:
            r1 = requests.post(
                postURL, json=list_data, headers=headers, verify=False)
            Helper.internal_assert(r1.status_code, 200)
            logger.debug("Next step was added to the engagement!")
            ns_uuid = r1.json()
            return ns_uuid[0]['uuid']
        except:
            if r1 is None:
                logger.error(
                    "Failed to add next step to VF " + user_content['vfName'])
            else:
                logger.error("Failed to add next step to VF " + user_content[
                             'vfName'] + ", see response >>> %s %s.\nContent: %s" % (r1.status_code, r1.reason, str(r1.content, 'utf-8')))
            raise

    @staticmethod
    def create_vf(token):
        r1 = None
        postUrl = settings.EM_REST_URL + "vf/"
        targetVersion = DBGeneral.select_from(
            "uuid", "ice_deployment_target", 1)
        ecompRelease = DBGeneral.select_from("uuid", "ice_ecomp_release", 1)
        headers = dict()  # Create header for post request.
        headers['Content-type'] = 'application/json'
        headers['Authorization'] = 'token ' + token
        jdata = [{"virtual_function": Helper.rand_string("randomString"),
                  "version": Helper.rand_string("randomString") + Helper.rand_string("randomNumber"),
                  "target_lab_entry_date": time.strftime("%Y-%m-%d"),
                  "target_aic_uuid": targetVersion,
                  "ecomp_release": ecompRelease,
                  "is_service_provider_internal": False}]
        try:
            r1 = requests.post(
                postUrl, json=jdata, headers=headers, verify=False)
            Helper.internal_assert(r1.status_code, 200)
            logger.debug("Virtual Function created successfully!")
            content = r1.content[1:-1]
            return content
        except:
            if r1 is None:
                logger.debug("Failed to create VF >>> request failed!")
            else:
                logger.debug(
                    "Failed to create VF >>> %s %s \n %s" % (r1.status_code, r1.reason, str(r1.content, 'utf-8')))
            raise

    @staticmethod
    def get_engagement(user_content):
        r1 = None
        postUrl = settings.EM_REST_URL + 'single-engagement/' + \
            str(user_content['engagement_uuid'],)
        headers = dict()  # Create header for post request.
        headers['Content-type'] = 'application/json'
        headers['Authorization'] = user_content['session_token']
        try:
            r1 = requests.get(
                postUrl, headers=headers, verify=False)
            Helper.internal_assert(r1.status_code, 200)
            logger.debug("Retrieved the Engagement successfully!")
            content = r1.content
            return json.loads(content)
        except:
            if r1 is None:
                logger.debug(
                    "Failed to Retrieve the Engagement >>> request failed!")
            else:
                logger.debug(
                    "Failed to Retrieve the Engagement >>> %s %s \n %s" % (r1.status_code, r1.reason, str(r1.content, 'utf-8')))
            raise

    @staticmethod
    def invite_team_member(user_content):
        r1 = None
        postURL = settings.ICE_EM_URL + '/v1/engmgr/invite-team-members/'
        logger.debug("Post invite user URL: " + postURL)
        headers = dict()  # Create header for post request.
        headers['Content-type'] = 'application/json'
        headers['Authorization'] = user_content['session_token']
        data = dict()  # Create JSON data for post request.
        data['email'] = Helper.rand_string(
            'randomString') + "@" + ServiceProvider.email
        data['eng_uuid'] = user_content['engagement_uuid']
        list_data = []
        list_data.append(data)
        try:
            r1 = requests.post(
                postURL, json=list_data, headers=headers, verify=False)
            Helper.internal_assert_boolean(r1.status_code, 200)
            logger.debug("Invite sent successfully to email " + data['email'])
            invite_token = DBGeneral.select_where_and("invitation_token", "ice_invitation", "email", data[
                                                      'email'], "engagement_uuid", user_content['engagement_uuid'], 1)
            invite_url = settings.ICE_PORTAL_URL + "/#/signUp?invitation=" + invite_token + \
                "&email=" + data['email']
            logger.debug("Invitation URL is: " + invite_url)
            return data['email'], invite_token, invite_url
        except:
            if r1 is None:
                logger.error("Failed to invite team member.")
            else:
                logger.error(
                    "POST request failed to invite team member, see response >>> %s %s" % (r1.status_code, r1.reason))
            raise

    @staticmethod
    def add_contact(user_content):
        r1 = None
        postURL = settings.ICE_EM_URL + '/v1/engmgr/add-contact/'
        logger.debug("Post invite vendor contact URL: " + postURL)
        headers = dict()  # Create header for post request.
        headers['Content-type'] = 'application/json'
        headers['Authorization'] = user_content['session_token']
        data = dict()  # Create JSON data for post request.
        data['company'] = user_content['vendor_uuid']
        data['email'] = Helper.rand_string(
            'randomString') + "@" + ServiceProvider.email
        data['eng_uuid'] = user_content['engagement_uuid']
        data['full_name'] = Helper.rand_string('randomString')
        data['phone_number'] = "+1201" + Helper.rand_string("randomNumber", 6)
        try:
            r1 = requests.post(
                postURL, json=data, headers=headers, verify=False)
            Helper.internal_assert_boolean(r1.status_code, 200)
            logger.debug("Invite sent successfully to email " + data['email'])
            invite_token = DBGeneral.select_where_and("invitation_token", "ice_invitation", "email", data[
                                                      'email'], "engagement_uuid", user_content['engagement_uuid'], 1)
            invite_url = settings.ICE_PORTAL_URL + "/#/signUp?invitation=" + invite_token + \
                "&email=" + data['email'] + "&full_name=" + data['full_name'] + \
                "&phone_number=" + \
                data['phone_number'] + "&company=" + \
                data['company'] + "&is_contact_user=true"
            logger.debug("Invitation URL is: " + invite_url)
            return data['email'], invite_token, invite_url
        except:
            if r1 is None:
                logger.error("Failed to invite vendor contact.")
            else:
                logger.error("POST request failed to invite vendor contact, see response >>> %s %s \n %s" % (
                    r1.status_code, r1.reason, str(r1.content, 'utf-8')))
            raise

    @staticmethod
    def edit_next_step(user_content, ns_uuid):
        r1 = None
        postURL = settings.ICE_EM_URL + '/v1/engmgr/nextsteps/' + ns_uuid + \
            '/engagement/' + user_content['engagement_uuid'] + '/modify/'
        logger.debug("Put next step URL: " + postURL)
        headers = dict()  # Create header for post request.
        headers['Content-type'] = 'application/json'
        headers['Authorization'] = user_content['session_token']
        data = dict()  # Create JSON data for post request.
        data['files'] = []
        data['assigneesUuids'] = [user_content['uuid']]
        data['duedate'] = str(datetime.date.today())
        data['description'] = "API edit next step test " + \
            Helper.rand_string('randomString')
        try:
            r1 = requests.put(
                postURL, json=data, headers=headers, verify=False)
            Helper.internal_assert_boolean(r1.status_code, 202)
            logger.debug("Next step was edited successfully!")
        except:
            if r1 is None:
                logger.error("Failed to edit next step uuid: " + ns_uuid)
            else:
                logger.error("Failed to edit next step uuid: " + ns_uuid +
                             ", see response >>> %s %s" % (r1.status_code, r1.reason))
            raise

    @staticmethod
    def get_export_dasboard_excel(token, keywords=""):
        postUrl = settings.EM_REST_URL + \
            "engagement/export/?stage=All&keyword=" + keywords
        headers = {"Authorization": token}
        r1 = requests.get(postUrl, headers=headers, verify=False)
        Helper.internal_assert(r1.status_code, 200)
        if (r1.status_code == 200):
            logger.debug("APIUser activated successfully!")
            return r1.content
        else:
            raise Exception(
                "Failed to activate user >>> %s %s" % (r1.status_code, r1.reason))
            return False

    @staticmethod
    def create_engagement(wait_for_gitlab=True):
        user_content = APIUser.create_new_user()
        APIUser.activate_user(
            user_content['uuid'], user_content['user']['activation_token'])
        token = APIUser.login_user(user_content['email'])
        vf_content = json.loads(APIVirtualFunction.create_vf(token))
        user_content['vfName'] = vf_content['name']
        user_content['vf_uuid'] = vf_content['uuid']
        user_content['target_aic'] = vf_content['deployment_target']['version']
        # <-- ECOMP RELEASE
        user_content['ecomp_release'] = vf_content[
            'ecomp_release']['name']
        user_content['vnf_version'] = vf_content['version']
        if(vf_content['vendor']['name'] == "AT&amp;T"):
            user_content['vendor'] = "AT&T"
        else:
            user_content['vendor'] = vf_content['vendor']['name']
        user_content['vendor_uuid'] = vf_content['vendor']['uuid']
        user_content['engagement_manual_id'] = vf_content[
            'engagement']['engagement_manual_id']
        user_content['target_lab_entry_date'] = vf_content[
            'target_lab_entry_date']
        user_content['el_email'] = vf_content[
            'engagement']['reviewer']['email']
        user_content['el_name'] = vf_content[
            'engagement']['reviewer']['full_name']
        user_content['pr_email'] = vf_content[
            'engagement']['peer_reviewer']['email']
        user_content['pr_name'] = vf_content[
            'engagement']['peer_reviewer']['full_name']
        user_content['engagement_uuid'] = vf_content['engagement']['uuid']
        user_content['session_token'] = 'token ' + token
        user_content['engagement'] = vf_content['engagement']
        user_content['vfStage'] = vf_content['engagement']['engagement_stage']

        return user_content

    @staticmethod
    def set_eng_stage(user_content, requested_stage):
        token = APIUser.login_user(user_content['el_email'])
        r1 = None
        putUrl = Constants.Default.URL.Engagement.SingleEngagement.TEXT + \
            user_content['engagement_uuid'] + "/stage/" + str(requested_stage)
        headers = dict()  # Create header for post request.
        headers['Content-type'] = 'application/json'
        headers['Authorization'] = 'token ' + token
        try:
            r1 = requests.put(
                putUrl, headers=headers, verify=False)
            Helper.internal_assert(r1.status_code, 202)
            logger.debug(
                "Engagement stage was successfully changed to " + str(requested_stage) + "!")
            content = r1.content[1:-1]
            return content
        except:
            if r1 is None:
                logger.debug("Failed to set eng stage >>> request failed!")
            else:
                logger.debug(
                    "Failed to set eng stage >>> %s %s \n %s" % (r1.status_code, r1.reason, str(r1.content, 'utf-8')))
            raise

    @staticmethod
    def update_aic_version(eng_uuid, aic_version_uuid, session_token):
        r1 = None
        putURL = Constants.Default.URL.Engagement.EngagementOperations.TEXT + \
            eng_uuid + '/deployment-targets/' + aic_version_uuid
        logger.debug("Put next step URL: " + putURL)
        headers = dict()  # Create header for post request.
        headers['Content-type'] = 'application/json'
        headers['Authorization'] = session_token
        try:
            r1 = requests.put(putURL, headers=headers, verify=False)
            Helper.internal_assert_boolean(r1.status_code, 200)
            logger.debug("AIC version has changed!")
        except:
            if r1 is None:
                msg = "Failed to edit AIC version"
            else:
                msg = "Failed to edit AIC version, see response >>> %s %s" % (
                    r1.status_code, r1.reason)
            raise msg

    @staticmethod
    def update_ecomp_release(eng_uuid, ecomp_release_uuid, session_token):
        r1 = None
        putURL = Constants.Default.URL.Engagement.EngagementOperations.TEXT + \
            eng_uuid + '/ecomp-releases/' + ecomp_release_uuid
        logger.debug("Put next step URL: " + putURL)
        headers = dict()  # Create header for post request.
        headers['Content-type'] = 'application/json'
        headers['Authorization'] = session_token
        try:
            r1 = requests.put(putURL, headers=headers, verify=False)
            Helper.internal_assert_boolean(r1.status_code, 200)
            logger.debug("AIC version has changed!")
        except:
            if r1 is None:
                msg = "Failed to update ECOMP release"
            else:
                msg = "Failed to update ECOMP release, see response >>> %s %s" % (
                    r1.status_code, r1.reason)
            raise msg