From 45e3a42c37d8c2501a4971a4959b032566dbb5ec Mon Sep 17 00:00:00 2001 From: Alessandro Pieve Date: Fri, 17 Feb 2017 14:55:03 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/resource-management/quota-manager@142703 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../quota/manager/check/QuotaCheck.java | 8 ++- .../quota/manager/check/QuotaUsage.java | 67 +++++++++++------- .../manager/util/DiscoveryConfiguration.java | 40 ++++++++++- .../quota/manager/util/DiscoveryListUser.java | 32 ++++++--- .../quota/manager/util/ListUserInfo.java | 38 ++++++++++ .../quota/manager/util/SendNotification.java | 69 ++++++++++++------- 6 files changed, 192 insertions(+), 62 deletions(-) create mode 100644 src/main/java/org/gcube/resource/management/quota/manager/util/ListUserInfo.java diff --git a/src/main/java/org/gcube/resource/management/quota/manager/check/QuotaCheck.java b/src/main/java/org/gcube/resource/management/quota/manager/check/QuotaCheck.java index 5151991..521f453 100644 --- a/src/main/java/org/gcube/resource/management/quota/manager/check/QuotaCheck.java +++ b/src/main/java/org/gcube/resource/management/quota/manager/check/QuotaCheck.java @@ -72,8 +72,12 @@ public class QuotaCheck { DiscoveryListUser discoveryListUser= new DiscoveryListUser(this.context); log.debug("QuotaCheck - for context:{} , list user:{}",context,discoveryListUser.getListUser()); + + + //verify for each user if have a specify quota and Overwrite if exist and traduce into object for query accounting quotaUtility.verifyListUser(discoveryListUser.getListUser()); + log.debug("QuotaCheck - quotaUtility.getUsageToBeVerified():{}",quotaUtility.getUsageToBeVerified()); Boolean error=true; @@ -100,12 +104,12 @@ public class QuotaCheck { //insert into db for(UsageValue usageIndex:quotaUtility.getUsageToBeVerified()){ if (usageIndex.getClz()==QuotaType.STORAGE.getQuotaTypeClass()){ - log.debug("save quota storage identifier:{},temporalConstraint:{}",usageIndex.getIdentifier(),usageIndex.getTemporalConstraint()); + log.debug("----Elaborate a identifier:{}, temporalConstraint:{} , insert a quota storage",usageIndex.getIdentifier(),usageIndex.getTemporalConstraint()); QuotaUsageStorageValue usageStorVal=(QuotaUsageStorageValue) usageIndex; queryQuotaUsage.insertStorageQuota(usageStorVal); } if (usageIndex.getClz()==QuotaType.SERVICE.getQuotaTypeClass()){ - log.debug("save quota service identifier:{},temporalConstraint:{}",usageIndex.getIdentifier(),usageIndex.getTemporalConstraint()); + log.debug("----Elaborate a identifier:{}, temporalConstraint:{} , insert a quota service",usageIndex.getIdentifier(),usageIndex.getTemporalConstraint()); QuotaUsageServiceValue usageSerVal=(QuotaUsageServiceValue) usageIndex; queryQuotaUsage.insertServiceQuota(usageSerVal); } diff --git a/src/main/java/org/gcube/resource/management/quota/manager/check/QuotaUsage.java b/src/main/java/org/gcube/resource/management/quota/manager/check/QuotaUsage.java index a6388a3..60814d1 100644 --- a/src/main/java/org/gcube/resource/management/quota/manager/check/QuotaUsage.java +++ b/src/main/java/org/gcube/resource/management/quota/manager/check/QuotaUsage.java @@ -142,7 +142,7 @@ public class QuotaUsage { Double percent=((usageStorVal.getD()/usageStorVal.getdQuota())*100); percent=Math.round(percent * 100.0) / 100.0; - log.debug("Quota Percent quota:{} used:{},use:{}",usageStorVal.getdQuota(),usageStorVal.getD(),percent); + log.debug("Quota Percent quota:{} used:{} percent:{}",usageStorVal.getdQuota(),usageStorVal.getD(),percent); String queryVerify="SELECT NOTIFICATIONWARNING1,NOTIFICATIONWARNING2,NOTIFICATIONEXCEED,QUOTAASSIGNED FROM "+ConstantsDb.DATABASE_PREFIX+ConstantsDb.SEPARATOR+ConstantsDb.DATABASE_TYPE_STORAGE+ConstantsDb.SEPARATOR+periodQuota+"" + " WHERE IDENTIFIER ='"+usageStorVal.getIdentifier()+"'"; @@ -154,32 +154,37 @@ public class QuotaUsage { quotaAssignedOld= rs.getDouble("QUOTAASSIGNED"); } - if (quotaAssignedOld!=usageStorVal.getdQuota()){ + if (!quotaAssignedOld.equals(usageStorVal.getdQuota())){ //verify if your quota assigned is changed log.debug("quota changed old value:{} new value:{}",quotaAssignedOld,usageStorVal.getdQuota()); + if (!quotaAssignedOld.equals(0.0)){ + //verify if your old quota is consistent + log.debug("your old quota is consistent:{}",quotaAssignedOld); + sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"change"); + } + } - //verify if a getdQuota() == -1 considery a infinity quota - - log.debug("usageStorVal.getdQuota()!=-1 :"+usageStorVal.getdQuota()); - if (usageStorVal.getdQuota()!=-1){ - + if (!usageStorVal.getdQuota().equals(-1)){ if ((percent>Constants.LIMIT_MSG_QUOTA_PERC_USAGE_1)&&(percentConstants.LIMIT_MSG_QUOTA_PERC_USAGE_2)&&(percent<100)){ //verify if your quota is between 95 and 100 % second warning if (!notificationWarning2){ - sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"warning"); - - notificationWarning2=true; - notificationExceed=false; + if (sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"warning")){ + log.debug("notification Send! warning level2"); + notificationWarning2=true; + notificationExceed=false; + } } } else if ((percent>=100)){ @@ -187,10 +192,11 @@ public class QuotaUsage { log.debug("Quota exceed for:{} identifier:{} notification:{} ",percent,usageStorVal.getIdentifier(),notificationExceed); if (!notificationExceed){ - sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"exceed"); + if(sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"exceed")){ + log.debug("notification Send! warning level3"); + notificationExceed=true; + } userExceedQuota.add(usageStorVal.getIdentifier()); - notificationExceed=true; - } } else if (notificationWarning1 || notificationWarning2 || notificationExceed){ @@ -199,13 +205,18 @@ public class QuotaUsage { usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD()); log.debug("---Quota reset for notificationWarning1:{} and notificationWarning2 :{} and notificationExceed:{}", notificationWarning1,notificationWarning2,notificationExceed); - sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"reset"); - notificationWarning1=false; - notificationWarning2=false; - notificationExceed=false; + if(sendNotification.sendNotificationUser(usageStorVal.getIdentifier(),usageStorVal.getdQuota(),usageStorVal.getD(),percent,"reset")){ + log.debug("reset notification Send!"); + notificationWarning1=false; + notificationWarning2=false; + notificationExceed=false; + } } } + else{ + log.debug("quote infinite for identifier:{}",usageStorVal.getIdentifier()); + } queryVerify="SELECT count(*) FROM "+ConstantsDb.DATABASE_PREFIX+ConstantsDb.SEPARATOR+ConstantsDb.DATABASE_TYPE_STORAGE+ConstantsDb.SEPARATOR+periodQuota+"" + " WHERE IDENTIFIER ='"+usageStorVal.getIdentifier()+"'"; @@ -257,7 +268,13 @@ public class QuotaUsage { } } - + /** + * selectStorageQuota + * @param identifier + * @param interval + * @return Quota Storage Status + * @throws Exception + */ public QuotaStorageStatus selectStorageQuota(String identifier, TimeInterval interval)throws Exception { Connection connection = getDBConnection(); @@ -294,8 +311,6 @@ public class QuotaUsage { connection.close(); } return quotaStorageStatus; - - } /** @@ -321,8 +336,8 @@ public class QuotaUsage { * @return connection */ private Connection getDBConnection() { - Connection dbConnection = null; + Connection dbConnection = null; try { Class.forName(ConstantsDb.DB_DRIVER); } catch (ClassNotFoundException e) { diff --git a/src/main/java/org/gcube/resource/management/quota/manager/util/DiscoveryConfiguration.java b/src/main/java/org/gcube/resource/management/quota/manager/util/DiscoveryConfiguration.java index c8a9a5b..4dd6957 100644 --- a/src/main/java/org/gcube/resource/management/quota/manager/util/DiscoveryConfiguration.java +++ b/src/main/java/org/gcube/resource/management/quota/manager/util/DiscoveryConfiguration.java @@ -34,11 +34,17 @@ public class DiscoveryConfiguration { private String textQuotaUserExceedSubject="ATTENTION Your storage space exceeds the assigned quota capacity"; private String textQuotaUserExceed="Hi {identifier}, your storage space is full, Your quota occupation is {percent}% , you are using {quotaUsage} out of {quotaAssigned} available. Please free up some space."; + private String textQuotaUserChangeSubject="INFO your quota has changed"; + private String textQuotaUserChange="Hi {identifier},your quota has changed to {quotaAssigned}. Your are currently occupying {percent}% of the available space."; + + private String textQuotaAdminExceedSubject="INFO List User quota Exceed"; private String textQuotaAdminExceed="Dear, now this user have a quota exceed: {listuser}"; private String textQuotaUserReset="Hi {identifier}, Your quota occupation decreased and is now {percent}%, you are using {quotaUsage}, out of {quotaAssigned} available. Good job!"; private String textQuotaUserResetSubject="INFO Your storage space is now sufficiently below the quota capacity"; + + private String roleNotifier="Administrator"; @@ -57,6 +63,9 @@ public class DiscoveryConfiguration { private final static String TEXT_QUOTA_USER_EXCEED_SUBJECT="textQuotaUserExceedSubject"; private final static String TEXT_QUOTA_USER_EXCEED="textQuotaUserExceed"; + private final static String TEXT_QUOTA_USER_CHANGE_SUBJECT="textQuotaUserChangeSubject"; + private final static String TEXT_QUOTA_USER_CHANGE="textQuotaUserChange"; + private final static String TEXT_QUOTA_ADMIN_EXCEED_SUBJECT="textQuotaAdminExceedSubject"; private final static String TEXT_QUOTA_ADMIN_EXCEED="textQuotaAdminExceed"; @@ -153,6 +162,12 @@ public class DiscoveryConfiguration { case TEXT_QUOTA_USER_EXCEED: textQuotaUserExceed=value; break; + case TEXT_QUOTA_USER_CHANGE_SUBJECT: + textQuotaUserChangeSubject=value; + break; + case TEXT_QUOTA_USER_CHANGE: + textQuotaUserChange=value; + break; case TEXT_QUOTA_ADMIN_EXCEED_SUBJECT: textQuotaAdminExceedSubject=value; break; @@ -182,13 +197,25 @@ public class DiscoveryConfiguration { + @Override public String toString() { return "DiscoveryConfiguration [databasePath=" + databasePath + ", usernameDb=" + usernameDb + ", pwdnameDb=" + pwdnameDb + ", dbname=" + dbname + ", notifierUser=" + notifierUser + ", notifierAdmin=" + notifierAdmin + ", refreshTimeQuota=" - + refreshTimeQuota + "]"; + + refreshTimeQuota + ", textQuotaUserWarning=" + + textQuotaUserWarning + ", textQuotaUserWarningSubject=" + + textQuotaUserWarningSubject + ", textQuotaUserExceedSubject=" + + textQuotaUserExceedSubject + ", textQuotaUserExceed=" + + textQuotaUserExceed + ", textQuotaUserChangeSubject=" + + textQuotaUserChangeSubject + ", textQuotaUserChange=" + + textQuotaUserChange + ", textQuotaAdminExceedSubject=" + + textQuotaAdminExceedSubject + ", textQuotaAdminExceed=" + + textQuotaAdminExceed + ", textQuotaUserReset=" + + textQuotaUserReset + ", textQuotaUserResetSubject=" + + textQuotaUserResetSubject + ", roleNotifier=" + roleNotifier + + "]"; } public String getDatabasePath() { @@ -251,5 +278,16 @@ public class DiscoveryConfiguration { public String getRoleNotifier() { return roleNotifier; } + + public String getTextQuotaUserChangeSubject() { + return textQuotaUserChangeSubject; + } + + public String getTextQuotaUserChange() { + return textQuotaUserChange; + } + + + } diff --git a/src/main/java/org/gcube/resource/management/quota/manager/util/DiscoveryListUser.java b/src/main/java/org/gcube/resource/management/quota/manager/util/DiscoveryListUser.java index 7fe5598..0f32ab7 100644 --- a/src/main/java/org/gcube/resource/management/quota/manager/util/DiscoveryListUser.java +++ b/src/main/java/org/gcube/resource/management/quota/manager/util/DiscoveryListUser.java @@ -7,6 +7,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.slf4j.Logger; @@ -22,10 +23,17 @@ import com.google.gson.Gson; public class DiscoveryListUser { private static Logger log = LoggerFactory.getLogger(DiscoveryListUser.class); - private final static String NAME_SERVICE_ALL_USERNAMES = "2/users/get-all-usernames?gcube-token"; + //private final static String NAME_SERVICE_ALL_USERNAMES = "2/users/get-all-usernames?gcube-token"; + + private final static String NAME_SERVICE_ALL_USERNAMES = "2/users/get-all-fullnames-and-usernames?gcube-token"; + + private List listUser = null; + + private static Map listFullUser; + public DiscoveryListUser(String context) { String token =SecurityTokenProvider.instance.get(); @@ -34,13 +42,13 @@ public class DiscoveryListUser { String urlService=discoveryList.getBasePath()+NAME_SERVICE_ALL_USERNAMES+"="+token; log.debug("service DiscoveryServiceListUser:"+urlService); String data = getJSON(urlService); - log.debug("data read:{}",data); - ListUser msg = new Gson().fromJson(data, ListUser.class); - - //TODO for debug limit a list user: - //listUser=msg.getResult(); - //REMOVE TODO - + //log.debug("data read:{}",data); + ListUserInfo msg = new Gson().fromJson(data, ListUserInfo.class); + listFullUser=msg.getResult(); + listUser = new ArrayList(listFullUser.keySet()); + log.debug("listuser:{}",listUser); + //ONLY DEBUG + /* listUser=new ArrayList(); listUser.add("lucio.lelii"); listUser.add("alessandro.pieve"); @@ -49,6 +57,8 @@ public class DiscoveryListUser { listUser.add("costantino.perciante"); listUser.add("luca.frosini"); listUser.add("roberto.cirillo"); + log.debug("list username:{}",listUser); + */ } /** @@ -59,6 +69,12 @@ public class DiscoveryListUser { return listUser; } + + public static String getMapUser(String identifier) { + return listFullUser.get(identifier); + } + + /** * * @param url diff --git a/src/main/java/org/gcube/resource/management/quota/manager/util/ListUserInfo.java b/src/main/java/org/gcube/resource/management/quota/manager/util/ListUserInfo.java new file mode 100644 index 0000000..971669c --- /dev/null +++ b/src/main/java/org/gcube/resource/management/quota/manager/util/ListUserInfo.java @@ -0,0 +1,38 @@ +package org.gcube.resource.management.quota.manager.util; + +import java.util.Map; +/** + * + * @author pieve + * + */ +public class ListUserInfo { + /* + "success" : false, + "message" : "String index out of range: 5", + "result" : null + */ + private Boolean success; + private String message; + private Map result; + public Boolean getSuccess() { + return success; + } + public void setSuccess(Boolean success) { + this.success = success; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + public Map getResult() { + return result; + } + public void setResult(Map result) { + this.result = result; + } + + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/resource/management/quota/manager/util/SendNotification.java b/src/main/java/org/gcube/resource/management/quota/manager/util/SendNotification.java index d0d4539..c1554d3 100644 --- a/src/main/java/org/gcube/resource/management/quota/manager/util/SendNotification.java +++ b/src/main/java/org/gcube/resource/management/quota/manager/util/SendNotification.java @@ -34,7 +34,7 @@ public class SendNotification { private DiscoveryConfiguration discoveryCheck; private final static String WRITE_MESSAGE = "2/messages/write-message"; - private final static String NAME_USERNAME_GLOBAL_ROLE = "2/users/get-usernames-by-global-role?role-name=Administrator&gcube-token"; + //private final static String NAME_USERNAME_GLOBAL_ROLE = "2/users/get-usernames-by-global-role?role-name=Administrator&gcube-token"; private static Logger log = LoggerFactory.getLogger(QuotaUsage.class); public SendNotification(DiscoveryConfiguration discoveryCheck){ @@ -51,9 +51,10 @@ public class SendNotification { this.percent=percent; } - public void sendNotificationUser(String identifier, Double quotaAssigned, Double quotaUsage, Double percent, String type) throws JSONException{ + public boolean sendNotificationUser(String identifier, Double quotaAssigned, Double quotaUsage, Double percent, String type) throws JSONException{ + Boolean resultSend= false; if (discoveryCheck.getNotifierUser()){ - Boolean resultSend= false; + configure(identifier, quotaAssigned, quotaUsage, percent); log.debug("Notification Quota limit used:{} identifier:{} quotaAssigned:{} quotaUsage:{} ",this.percent,this.identifier,this.quotaAssigned,this.quotaUsage); @@ -71,18 +72,20 @@ public class SendNotification { body=CleanNotification(discoveryCheck.getTextQuotaUserReset()); subject=discoveryCheck.getTextQuotaUserResetSubject(); } + if (type=="change"){ + body=CleanNotification(discoveryCheck.getTextQuotaUserChange()); + subject=discoveryCheck.getTextQuotaUserChangeSubject(); + } String token = SecurityTokenProvider.instance.get(); JSONObject urlParameters = new JSONObject(); - List user=new ArrayList(); - //TODO delete - //user.add("alessandro.pieve"); - user.add(identifier); - + List user=new ArrayList(); + user.add("alessandro.pieve"); + //user.add(identifier); urlParameters.put("recipients", user); urlParameters.put("body", body); urlParameters.put("subject", subject); try { - resultSend = writeMsg(urlService, urlParameters,token); + resultSend = writeMsg(urlService, urlParameters,token); } catch (Exception e) { e.printStackTrace(); } @@ -91,7 +94,7 @@ public class SendNotification { else{ log.debug("Not notification Quota limit used:{} identifier:{} quotaAssigned:{} quotaUsage:{} ",percent,identifier,quotaAssigned,quotaUsage); } - + return resultSend; } @@ -100,17 +103,18 @@ public class SendNotification { * @param userExceedQuota * @throws Exception */ - public void SendNotificationAdmin(List userExceedQuota) throws Exception{ + public boolean SendNotificationAdmin(List userExceedQuota) throws Exception{ log.debug("SendNotificationAdmin for user:{}",userExceedQuota.toString()); + Boolean resultSend= false; if (userExceedQuota.size()>0){ if (discoveryCheck.getNotifierAdmin()){ - Boolean resultSend= false; + String token = SecurityTokenProvider.instance.get(); String context = ScopeProvider.instance.get(); DiscoveryService discoveryList= new DiscoveryService(context); - List sendersList=sendersList(discoveryList.getBasePath()+NAME_USERNAME_GLOBAL_ROLE,token,discoveryCheck.getRoleNotifier()); + //List sendersList=sendersList(discoveryList.getBasePath()+NAME_USERNAME_GLOBAL_ROLE,token,discoveryCheck.getRoleNotifier()); configure(identifier, quotaAssigned, quotaUsage, percent); String text=CleanNotification(discoveryCheck.getTextQuotaAdminExceed()); @@ -128,12 +132,16 @@ public class SendNotification { JSONObject urlParameters = new JSONObject(); List user=new ArrayList(); - identifier="alessandro.pieve"; + //identifier="alessandro.pieve"; + List sendersList =new ArrayList(); + sendersList.add("alessandro.pieve"); + //sendersList.add("scarponi"); for (String cc :sendersList){ - // user.add(cc); + user.add(cc); } - user.add(identifier); + + //user.add(identifier); urlParameters.put("recipients", user); urlParameters.put("body", text); urlParameters.put("subject", discoveryCheck.getTextQuotaAdminExceedSubject()); @@ -148,7 +156,7 @@ public class SendNotification { log.debug("not send notification quota exceed :{}",userExceedQuota.toString()); } } - + return resultSend; } @@ -158,19 +166,30 @@ public class SendNotification { * @return notification with user,quota and percent */ private String CleanNotification(String notification){ + + String fullname=DiscoveryListUser.getMapUser(identifier); + log.debug("Clean Notification from username:{} to fullname:{}",identifier,fullname); + if (notification.contains("{identifier}")){ - notification=notification.replace("{identifier}", identifier); + notification=notification.replace("{identifier}", fullname); } String quotaAssignedLabel; String quotaUsageLabel; + String percentLabel; if (quotaAssigned>1024){ quotaAssignedLabel=String.valueOf(Math.round((quotaAssigned/1024) * 100.0) / 100.0)+"GB"; quotaUsageLabel= String.valueOf(Math.round((quotaUsage/1024) * 100.0) / 100.0)+"GB"; + percentLabel=percent.toString(); } else{ quotaAssignedLabel=String.valueOf(quotaAssigned)+"MB"; quotaUsageLabel= String.valueOf(quotaUsage)+"MB"; + percentLabel=percent.toString(); + } + if (quotaAssigned==-1){ + quotaAssignedLabel="∞"; + percentLabel="0"; } if (notification.contains("{quotaAssigned}")){ @@ -180,7 +199,7 @@ public class SendNotification { notification=notification.replace("{quotaUsage}", quotaUsageLabel); } if (notification.contains("{percent}")){ - notification=notification.replace("{percent}", percent.toString()); + notification=notification.replace("{percent}",percentLabel); } return notification; } @@ -206,14 +225,13 @@ public class SendNotification { request.setEntity(params); HttpResponse response = httpClient.execute(request); log.debug("writeMsg path:{} urlParameters:{} request:{}",path,urlParameters,request.toString()); - /* - log.info("urlParameters:{}",urlParameters.toString()); - log.info("request:{}",request.toString()); - */ int status = response.getStatusLine().getStatusCode(); // check the response status and look if it was a redirect problem - if (status != HttpURLConnection.HTTP_OK && (status == HttpURLConnection.HTTP_MOVED_TEMP || + if ((status ==HttpURLConnection.HTTP_INTERNAL_ERROR)|| (status==HttpURLConnection.HTTP_BAD_GATEWAY)){ + result=false; + } + else if (status != HttpURLConnection.HTTP_OK && (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)) { @@ -231,7 +249,8 @@ public class SendNotification { request.setEntity(params); response = httpClient.execute(request); log.info(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); - } else{ + result=true; + }else{ log.debug(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); result=true; }