aboutsummaryrefslogtreecommitdiffstats
path: root/services/database/db_virtual_function.py
blob: f61d1b7019f4c33bbf0610a3356d31e03d8b12dc (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
# ============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 uuid

from django.conf import settings
import psycopg2

from services.constants import Constants
from services.database.db_bridge import DBBridge
from services.database.db_general import DBGeneral
from services.logging_service import LoggingServiceFactory


logger = LoggingServiceFactory.get_logger()


class DBVirtualFunction:

    @staticmethod
    def insert_ecomp_release(uuid, name, ui_visibility="TRUE"):
        try:
            queryTableName = "ice_ecomp_release"
            # Connect to General 'default'.
            dbConn = psycopg2.connect(
                DBGeneral.return_db_native_connection("em_db"))
            dbConn = dbConn
            cur = dbConn.cursor()
            logger.debug("DATABASE_TYPE: " + settings.DATABASE_TYPE)
        except Exception as e:
            errorMsg = "Failed to create connection to General: " + str(e)
            raise Exception(errorMsg)
        try:
            logger.debug("DATABASE_TYPE: " + settings.DATABASE_TYPE)
            # Create INSERT query.
            queryStr = "INSERT INTO %s " % queryTableName +\
                "(""uuid, name, weight, ui_visibility"") VALUES " +\
                "('%s', '%s', " % (uuid, name) +\
                "'%s', '%s');" % (0, ui_visibility)
            logger.debug("Query: " + queryStr)
            cur.execute(queryStr)  # Execute query.
            dbConn.commit()
            logger.debug("Test results are in General now.")
        except Exception as e:
            errorMsg = "Failed to insert ECOMP release to General:" + str(e)
            raise Exception(errorMsg)
        dbConn.close()

    @staticmethod
    def delete_ecomp_release(uuid, name):
        try:
            queryTableName = "ice_ecomp_release"
            # Connect to General 'default'.
            dbConn = psycopg2.connect(
                DBGeneral.return_db_native_connection("em_db"))
            dbConn = dbConn
            cur = dbConn.cursor()
        except Exception as e:
            errorMsg = "Failed to create connection to DBGeneral.because :" + \
                str(e)
            raise Exception(errorMsg)
        try:
            # Create INSERT query.
            queryStr = "DELETE FROM %s WHERE uuid = '%s';" % (
                queryTableName, uuid)
            logger.debug("Query: " + queryStr)
            cur.execute(queryStr)  # Execute query.
            dbConn.commit()
            logger.debug("Test results are in General now.")
        except Exception as e:
            errorMsg = "Failed to delete ECOMP release from General ." +\
                " because :" + \
                str(e)
            raise Exception(errorMsg)
            raise
        dbConn.close()

    @staticmethod
    def select_next_steps_ids(engagement_uuid):
        ice_next_steps = DBGeneral.select_where(
            "uuid", "ice_next_step", "engagement_id", engagement_uuid, 0)
        return ice_next_steps

    @staticmethod
    def select_next_steps_uuids_by_stage(engagement_uuid, engagement_stage):
        query = "SELECT uuid FROM %s WHERE " % (
            Constants.DBConstants.IceTables.NEXT_STEP) + "engagement_id=" +\
            "'%s' AND engagement_stage='%s' ORDER BY position;" % (
            engagement_uuid, engagement_stage)
        return DBGeneral.select_query(query, "list", 0)

    @staticmethod
    def update_next_step_position(next_step_uuid, new_index):
        DBGeneral.update_where(
            "ice_next_step", "position", new_index, "uuid", next_step_uuid)

    @staticmethod
    def select_next_step_description(next_step_uuid):
        return DBGeneral.select_where(
            "description",
            "ice_next_step",
            "uuid",
            next_step_uuid,
            1)

    @staticmethod
    def select_eng_uuid(vf_name):
        return DBGeneral.select_where(
            "engagement_id", "ice_vf", "name", vf_name, 1)

    @staticmethod
    def select_engagment_uuid_by_vf_name(vfName):
        engagement_id = DBGeneral.select_where(
            "engagement_id", "ice_vf", "name", vfName, 1)
        engagement_manual_id = DBGeneral.select_where(
            "engagement_manual_id", "ice_engagement", "uuid", engagement_id, 1)
        enguuid = DBGeneral.select_where(
            "uuid",
            "ice_engagement",
            "engagement_manual_id",
            engagement_manual_id,
            1)
        return enguuid

    @staticmethod
    def select_vf_version_by_vf_name(vfName):
        queryStr = "SELECT version FROM ice_vf WHERE name= '%s';" % vfName
        version_name = str(DBGeneral.select_query(queryStr))
        return version_name

    @staticmethod
    def select_vf_name_by_vf_version(version_name):
        queryofname = "SELECT name FROM ice_vf WHERE " +\
            "version= '%s';" % version_name
        vfNameDb = str(DBGeneral.select_query(queryofname))
        return vfNameDb

    @staticmethod
    def return_expected_steps(engagement_uuid, stage, user_email):
        steps_uuids = DBVirtualFunction.select_next_steps_uuids_by_stage(
            engagement_uuid, stage)
        personal_step_uuid = DBBridge.select_personal_next_step(user_email)
        DBVirtualFunction.update_next_step_position(personal_step_uuid, 1)
        steps_uuids.insert(0, personal_step_uuid)
        return steps_uuids

    @staticmethod
    def get_engagement():
        """Use this function instead of creating a new """ +\
            """engagement where no need to"""
        queryStr = "SELECT DISTINCT ice_engagement.uuid, " +\
            "engagement_manual_id, ice_vf.name, ice_user_profile.full_name, \
                    ice_user_profile.email, reviewer_table.full_name, " +\
            "reviewer_table.email, \
                    ice_deployment_target.version, ice_ecomp_release.name \
                    FROM ice_engagement LEFT JOIN ice_vf ON engagement_id " +\
            "= ice_engagement.uuid \
                    LEFT JOIN ice_user_profile reviewer_table ON " +\
            "reviewer_table.id = ice_engagement.reviewer_id \
                    LEFT JOIN ice_user_profile ON ice_user_profile.id = " +\
            "ice_engagement.peer_reviewer_id \
                    LEFT JOIN ice_deployment_target ON " +\
            "ice_deployment_target.uuid = " +\
            "ice_vf.deployment_target_id \
                    LEFT JOIN ice_ecomp_release ON " +\
            "ice_ecomp_release.uuid = ice_vf.ecomp_release_id \
                    WHERE ice_user_profile.id IS NOT NULL LIMIT 1;"
        list_of_values = DBGeneral.select_query(queryStr, return_type="list")
        list_of_keys = [
            "engagement_uuid",
            "engagement_manual_id",
            "vfName",
            "pr_name",
            "pr_email",
            "el_name",
            "el_email",
            "target_aic",
            "ecomp_release"]
        return dict(zip(list_of_keys, list_of_values))

    @staticmethod
    def insert_aic_version(ui_visibility="TRUE"):
        new_aic_version = {
            "uuid": str(
                uuid.uuid4()),
            "name": "AIC",
            "version": DBBridge.helper_rand_string(
                "randomNumber",
                2),
            "ui_visibility": ui_visibility,
            "weight": 0}
        queryStr = "INSERT INTO public.ice_deployment_target( \
                    uuid, name, version, ui_visibility, weight) \
                    VALUES " +\
            "('%s', '%s', '%s', '%s', %s);" % (
                new_aic_version['uuid'],
                new_aic_version['name'],
                new_aic_version['version'],
                new_aic_version['ui_visibility'],
                new_aic_version['weight'])
        DBGeneral.insert_query(queryStr)
        return new_aic_version

    @staticmethod
    def delete_aic_version(aic_uuid):
        DBGeneral.insert_query(
            "DELETE FROM public.ice_deployment_target WHERE uuid='%s';" %
            aic_uuid)

    @staticmethod
    def change_aic_version_weight(new_weight, old_weight):
        DBGeneral.insert_query(
            "UPDATE public.ice_deployment_target " +
            "SET weight=%s " % new_weight +
            "WHERE weight=%s" % old_weight)

    @staticmethod
    def change_ecomp_release_weight(new_weight, old_weight):
        DBGeneral.insert_query(
            "UPDATE public.ice_ecomp_release SET weight=%s WHERE weight=%s" %
            (new_weight, old_weight))

    @staticmethod
    def select_aic_version_uuid(aic_version):
        return DBGeneral.select_where(
            "uuid", "ice_deployment_target", "version", aic_version, 1)

    @staticmethod
    def select_ecomp_release_uuid(ecomp_release):
        return DBGeneral.select_where(
            "uuid", "ice_ecomp_release", "name", ecomp_release, 1)

    @staticmethod
    def add_admin_to_eng_team(eng_uuid):
        admin_db_id = DBGeneral.select_where(
            'id',
            Constants.DBConstants.IceTables.USER_PROFILE,
            'email',
            Constants.Users.Admin.EMAIL,
            1)
        queryStr = "INSERT INTO public.ice_engagement_engagement_team" +\
            "(engagement_id, iceuserprofile_id) VALUES ('%s', '%s');" % (
                eng_uuid, admin_db_id)
        logger.debug("add_admin_to_eng_team Query: %s" % queryStr)
        DBGeneral.insert_query(queryStr)

    @staticmethod
    def remove_engagement_from_recent(vf_uuid):
        DBGeneral.insert_query(
            "DELETE FROM %s WHERE vf_id='%s'" % (Constants.DBConstants.
                                                 IceTables.RECENT, vf_uuid))