1 package org.onap.vid.services;
3 import jline.internal.Log;
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;
12 import java.util.HashMap;
15 public class RoleGenaratorServiceImpl implements RoleGeneratorService {
18 AaiClientInterface client;
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 = "_";
26 public String generateRoleScript(Boolean firstRun) {
27 String query = "USE " + dbName + ";\r\n" +
28 "SET SQL_SAFE_UPDATES = 0;\r\n";
30 AaiResponse<SubscriberList> subscribers = client.getAllSubscribers();
32 query += replaceRolesToTempDelimiter("subscriber",buildSubscribersValuesForMappingsTable(subscribers.getT()));
34 query += addAvailableRolesCombination(firstRun, subscribers);
38 Log.error("There was an error in updating roles "+e.getMessage());
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+"),";
56 availableRoles = availableRoles.substring(0,availableRoles.length()-1);
57 query = createTemporaryTableAvailableRoles(availableRoles);
59 query += replaceRolesToTempDelimiter("service",buildServicesValuesForMappingsTable(servicesNames));
60 query += replaceToNewDelimiter();
61 query += deleteWrongRecords();
64 query += insertAvailableRolesToFnRole();
65 query += dropTemporaryTable("available_roles");
69 private String buildSubscribersValuesForMappingsTable(SubscriberList subscribers){
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 + "') ,";
75 if(query.length() > 0)
76 query = query.substring(0, query.length()-1) + ";\r\n";
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) +"'),";
87 if(query[0].length() > 0)
88 query[0] = query[0].substring(0, query[0].length()-1) + ";\r\n";
92 private String replaceRolesToTempDelimiter(String entityName, String valuesForMappingsTable ) {
94 AaiResponse<Services> services = client.getServices();
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");
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" ;
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";
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";
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");
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";
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 + ";";
161 private String dropTemporaryTable(String table) {
162 return "DROP TEMPORARY TABLE IF EXISTS " + table + ";\r\n";