org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / services / RoleGenaratorServiceImpl.java
1 package org.onap.vid.services;
2
3 import jline.internal.Log;
4 import org.junit.Test;
5 import org.onap.vid.aai.*;
6 import org.onap.vid.model.ModelConstants;
7 import org.onap.vid.model.Subscriber;
8 import org.onap.vid.model.SubscriberList;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Service;
11
12 import java.util.HashMap;
13
14 @Service
15 public class RoleGenaratorServiceImpl implements RoleGeneratorService {
16
17     @Autowired
18     AaiClientInterface client;
19
20     public static final String dbName =  "vid_portal";
21     public static final String  tblName = "fn_role";
22     public static final String tempDelimiter ="***";
23     public static final String oldDelimiter = "_";
24
25     @Override
26     public String generateRoleScript(Boolean firstRun) {
27         String query =  "USE " + dbName + ";\r\n" +
28                 "SET SQL_SAFE_UPDATES = 0;\r\n";
29         try {
30             AaiResponse<SubscriberList> subscribers = client.getAllSubscribers();
31             if (firstRun) {
32                 query += replaceRolesToTempDelimiter("subscriber",buildSubscribersValuesForMappingsTable(subscribers.getT()));
33             }
34             query += addAvailableRolesCombination(firstRun, subscribers);
35
36         }
37         catch (Exception e) {
38             Log.error("There was an error in updating roles "+e.getMessage());
39         }
40         return query;
41     }
42
43     private String addAvailableRolesCombination(Boolean firstRun, AaiResponse<SubscriberList> subscribers) {
44         String query, availableRoles="";
45         HashMap<String,String> servicesNames = new HashMap<String,String>();
46         for (Subscriber subscriber: subscribers.getT().customer) {
47             AaiResponse<Services> subscriberResponse = client.getSubscriberData(subscriber.globalCustomerId);
48             for(ServiceSubscription service: subscriberResponse.getT().serviceSubscriptions.serviceSubscription) {
49                 servicesNames.put(service.serviceType,"");
50                 String roleName = "'" + subscriber.subscriberName + ModelConstants.ROLE_DELIMITER + service.serviceType + "'";
51                 availableRoles += "("+roleName+"),";
52
53
54             }
55         }
56         availableRoles = availableRoles.substring(0,availableRoles.length()-1);
57         query = createTemporaryTableAvailableRoles(availableRoles);
58         if (firstRun){
59             query += replaceRolesToTempDelimiter("service",buildServicesValuesForMappingsTable(servicesNames));
60             query += replaceToNewDelimiter();
61             query += deleteWrongRecords();
62
63         }
64         query += insertAvailableRolesToFnRole();
65         query += dropTemporaryTable("available_roles");
66         return query;
67     }
68
69     private String buildSubscribersValuesForMappingsTable(SubscriberList subscribers){
70         String query="";
71         for (Subscriber subscriber : subscribers.customer) {
72             String subscriberName = subscriber.subscriberName.contains(oldDelimiter) ? subscriber.subscriberName.replace(oldDelimiter, tempDelimiter) : subscriber.subscriberName;
73             query = query + "('" + subscriber.globalCustomerId + "','" + subscriberName + "') ,";
74         }
75         if(query.length() > 0)
76             query = query.substring(0, query.length()-1) + ";\r\n";
77         return query;
78     }
79
80     private String buildServicesValuesForMappingsTable(HashMap<String,String> servicesNames){
81         final String[] query = {""};
82         servicesNames.forEach((k,v)->{
83             if (k.contains(oldDelimiter)) {
84                 query[0] += "('" + k + "' ,'" + k.replace(oldDelimiter, tempDelimiter) +"'),";
85             }
86         });
87         if(query[0].length() > 0)
88             query[0] = query[0].substring(0, query[0].length()-1) + ";\r\n";
89         return query[0];
90     }
91
92     private String replaceRolesToTempDelimiter(String entityName, String valuesForMappingsTable ) {
93
94         AaiResponse<Services> services = client.getServices();
95         String query = "";
96         if (valuesForMappingsTable.length() > 0) {
97             query = "CREATE TEMPORARY TABLE IF NOT EXISTS " + entityName + "Mappings(mapKey VARCHAR(255),mapValue VARCHAR(255));\r\n" +
98                     "INSERT INTO " + entityName + "Mappings VALUES ";
99             query += valuesForMappingsTable;
100             query += "UPDATE " + tblName + "\r\n" +
101                     "INNER JOIN " + entityName + "Mappings ON role_name LIKE concat('%',mapKey, '%')\r\n" +
102                     "SET ROLE_NAME = REPLACE(ROLE_NAME, mapKey, mapValue) ;  \r\n" +
103                     dropTemporaryTable(entityName + "Mappings");
104         }
105         return query;
106     }
107
108     private String replaceToNewDelimiter(){
109         String query =  "UPDATE " + tblName + "\r\n" +
110                 "SET ROLE_NAME = REPLACE(ROLE_NAME, '" + oldDelimiter + "', '" + ModelConstants.ROLE_DELIMITER + "');\r\n" ;
111         query += "UPDATE fn_role\r\n" +
112                 "SET ROLE_NAME = REPLACE(ROLE_NAME, '" + tempDelimiter + "', '" + oldDelimiter + "');\r\n" ;
113         return query;
114     }
115
116     private String insertAvailableRolesToFnRole(){
117         String query="INSERT INTO fn_role (ROLE_NAME, ACTIVE_YN, PRIORITY)\r\n" +
118                 "SELECT RNAME, 'Y', 5\r\n" +
119                 "FROM available_roles\r\n" +
120                 "WHERE NOT EXISTS (SELECT ROLE_NAME\r\n" +
121                 "FROM fn_role \r\n" +
122                 "where RNAME = ROLE_NAME);\r\n";
123         return query;
124     }
125
126
127
128     private String createTemporaryTableAvailableRoles(String availableRoles) {
129         String query = "CREATE TEMPORARY TABLE IF NOT EXISTS available_roles(rname VARCHAR(255));\r\n";
130         query += "INSERT INTO available_roles VALUES "+availableRoles+";\r\n";
131                 return query;
132     }
133
134     private String deleteWrongRecords(){
135         String query ="CREATE TEMPORARY TABLE IF NOT EXISTS wrong_roles(roleID INT);\r\n" +
136                 "INSERT INTO wrong_roles (roleID)\r\n" +
137                 "SELECT ROLE_ID FROM fn_role LEFT JOIN available_roles ON role_name LIKE concat(rname, '%')\r\n" +
138                 "WHERE available_roles.rname IS NULL AND ROLE_ID NOT IN (1,16);\r\n";
139         query += deleteCascade();
140         query += dropTemporaryTable("wrong_roles");
141         return query;
142     }
143
144     private String deleteCascade() {
145         String query = deleteFromTableByRoles("fn_role_composite", "PARENT_ROLE_ID");
146         query = query.substring(0, query.length()-1);
147         query += " OR wrong_roles.ROLEID = fn_role_composite.CHILD_ROLE_ID;\r\n";
148         query += deleteFromTableByRoles("fn_role_function", "ROLE_ID")+ "\r\n";
149         query += deleteFromTableByRoles("fn_user_role", "ROLE_ID")+ "\r\n";
150         query += deleteFromTableByRoles("fn_role", "ROLE_ID")+ "\r\n";
151         return query;
152     }
153
154     private String deleteFromTableByRoles(String table, String column) {
155         String query = "DELETE FROM " + table + "\r\n";
156         query += "using  " + table + " inner join wrong_roles\r\n" +
157                 "where wrong_roles.ROLEID = " + table + "." + column + ";";
158         return query;
159     }
160
161     private String dropTemporaryTable(String table) {
162         return "DROP TEMPORARY TABLE IF EXISTS " + table + ";\r\n";
163     }
164 }