diff options
Diffstat (limited to 'PolicyEngineUtils/src/main')
-rw-r--r-- | PolicyEngineUtils/src/main/java/org/onap/policy/std/NotificationStore.java | 370 | ||||
-rw-r--r-- | PolicyEngineUtils/src/main/java/org/onap/policy/utils/AAFPolicyClientImpl.java | 341 |
2 files changed, 369 insertions, 342 deletions
diff --git a/PolicyEngineUtils/src/main/java/org/onap/policy/std/NotificationStore.java b/PolicyEngineUtils/src/main/java/org/onap/policy/std/NotificationStore.java index 24e126386..6164418d9 100644 --- a/PolicyEngineUtils/src/main/java/org/onap/policy/std/NotificationStore.java +++ b/PolicyEngineUtils/src/main/java/org/onap/policy/std/NotificationStore.java @@ -3,6 +3,7 @@ * PolicyEngineUtils * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,8 +42,8 @@ public class NotificationStore { private static StdPDPNotification notificationRecord = new StdPDPNotification(); private NotificationStore () { - // Sonar prefers that we have an empty public constructor - // as opposed to an implicit public constructor. + // Sonar prefers that we have an empty public constructor + // as opposed to an implicit public constructor. } public static StdPDPNotification getDeltaNotification(StdPDPNotification newNotification) { @@ -63,195 +64,218 @@ public class NotificationStore { } return notificationDelta; } + + if (newNotification == null) { + return notificationDelta; + } // do the Delta operation. - if (newNotification != null) { - // check for old removed policies. - if (!newNotification.getRemovedPolicies().isEmpty()) { - for (RemovedPolicy newRemovedPolicy : newNotification.getRemovedPolicies()) { - //Look for policy Not in Remove - Boolean removed = true; - String policyName = newRemovedPolicy.getPolicyName(); - String ver = newRemovedPolicy.getVersionNo(); - for (RemovedPolicy oldRemovedPolicy : notificationRecord.getRemovedPolicies()) { - if (policyName.equals(oldRemovedPolicy.getPolicyName()) - && ver.equals(oldRemovedPolicy.getVersionNo())) { - removed = false; - // Don't want a duplicate. - oldRemovedPolicies.remove(oldRemovedPolicy); - } - } - //We need to change our record we have an Update record of this remove. - for (LoadedPolicy oldUpdatedPolicy : notificationRecord.getLoadedPolicies()) { - if (policyName.equals(oldUpdatedPolicy.getPolicyName()) - && ver.equals(oldUpdatedPolicy.getVersionNo())) { - oldUpdatedPolicies.remove(oldUpdatedPolicy); - oldUpdatedLostPolicies.remove(oldUpdatedPolicy); - } - } - if (removed) { - remove = true; - notificationRecord.getRemovedPolicies().add(newRemovedPolicy); - removedDelta.add((StdRemovedPolicy) newRemovedPolicy); - } - // This will be converted to New Later. - oldRemovedPolicies.add(newRemovedPolicy); - } + // check for old removed policies. + if (!newNotification.getRemovedPolicies().isEmpty()) { + for (RemovedPolicy newRemovedPolicy : newNotification.getRemovedPolicies()) { + remove = updateRemovedPolicies(removedDelta, oldUpdatedLostPolicies, oldRemovedPolicies, oldUpdatedPolicies, remove, newRemovedPolicy); } - // Check for old Updated Policies. - if (!newNotification.getLoadedPolicies().isEmpty()) { - for (LoadedPolicy newUpdatedPolicy : newNotification.getLoadedPolicies()) { - // Look for policies which are not in Update - Boolean updated = true; - String policyName = newUpdatedPolicy.getPolicyName(); - String ver = newUpdatedPolicy.getVersionNo(); - for (LoadedPolicy oldUpdatedPolicy : notificationRecord.getLoadedPolicies()) { - if (policyName.equals(oldUpdatedPolicy.getPolicyName()) - && ver.equals(oldUpdatedPolicy.getVersionNo())) { - updated = false; - // Remove the policy from copy. - oldUpdatedLostPolicies.remove(oldUpdatedPolicy); - // Eliminating Duplicate. - oldUpdatedPolicies.remove(oldUpdatedPolicy); - } - } - // Change the record if the policy has been Removed earlier. - for (RemovedPolicy oldRemovedPolicy : notificationRecord.getRemovedPolicies()) { - if (oldRemovedPolicy.getPolicyName().equals(policyName) - && oldRemovedPolicy.getVersionNo().equals(ver)) { - oldRemovedPolicies.remove(oldRemovedPolicy); - } - } - if (updated) { - update = true; - updatedDelta.add((StdLoadedPolicy) newUpdatedPolicy); - } - // This will be converted to new Later - oldUpdatedPolicies.add(newUpdatedPolicy); - } - // Conversion of Update to Remove if that occurred. - if (!oldUpdatedLostPolicies.isEmpty()) { - for (LoadedPolicy updatedPolicy : oldUpdatedLostPolicies) { - StdRemovedPolicy removedPolicy = new StdRemovedPolicy(); - removedPolicy.setPolicyName(updatedPolicy.getPolicyName()); - removedPolicy.setVersionNo(updatedPolicy.getVersionNo()); - removedDelta.add(removedPolicy); - remove = true; - } - } + } + // Check for old Updated Policies. + if (!newNotification.getLoadedPolicies().isEmpty()) { + for (LoadedPolicy newUpdatedPolicy : newNotification.getLoadedPolicies()) { + update = modifyUpdatedPolicies(updatedDelta, oldUpdatedLostPolicies, oldRemovedPolicies, oldUpdatedPolicies, update, newUpdatedPolicy); } - // Update our Record. - if (!oldUpdatedPolicies.isEmpty()) { - for (LoadedPolicy updatedPolicy : oldUpdatedPolicies) { - newUpdatedPolicies.add((StdLoadedPolicy) updatedPolicy); + // Conversion of Update to Remove if that occurred. + if (!oldUpdatedLostPolicies.isEmpty()) { + for (LoadedPolicy updatedPolicy : oldUpdatedLostPolicies) { + StdRemovedPolicy removedPolicy = new StdRemovedPolicy(); + removedPolicy.setPolicyName(updatedPolicy.getPolicyName()); + removedPolicy.setVersionNo(updatedPolicy.getVersionNo()); + removedDelta.add(removedPolicy); + remove = true; } } - if (!oldRemovedPolicies.isEmpty()) { - for (RemovedPolicy removedPolicy : oldRemovedPolicies) { - newRemovedPolicies.add((StdRemovedPolicy) removedPolicy); - } + } + // Update our Record. + if (!oldUpdatedPolicies.isEmpty()) { + for (LoadedPolicy updatedPolicy : oldUpdatedPolicies) { + newUpdatedPolicies.add((StdLoadedPolicy) updatedPolicy); } - notificationRecord.setRemovedPolicies(newRemovedPolicies); - notificationRecord.setLoadedPolicies(newUpdatedPolicies); - // Update the notification Result. - notificationDelta.setRemovedPolicies(removedDelta); - notificationDelta.setLoadedPolicies(updatedDelta); - if (remove && update) { - notificationDelta.setNotificationType(NotificationType.BOTH); - } else if (remove) { - notificationDelta.setNotificationType(NotificationType.REMOVE); - } else if (update) { - notificationDelta.setNotificationType(NotificationType.UPDATE); + } + if (!oldRemovedPolicies.isEmpty()) { + for (RemovedPolicy removedPolicy : oldRemovedPolicies) { + newRemovedPolicies.add((StdRemovedPolicy) removedPolicy); } } + notificationRecord.setRemovedPolicies(newRemovedPolicies); + notificationRecord.setLoadedPolicies(newUpdatedPolicies); + // Update the notification Result. + notificationDelta.setRemovedPolicies(removedDelta); + notificationDelta.setLoadedPolicies(updatedDelta); + if (remove && update) { + notificationDelta.setNotificationType(NotificationType.BOTH); + } else if (remove) { + notificationDelta.setNotificationType(NotificationType.REMOVE); + } else if (update) { + notificationDelta.setNotificationType(NotificationType.UPDATE); + } + return notificationDelta; } + private static Boolean modifyUpdatedPolicies(ArrayList<StdLoadedPolicy> updatedDelta, Collection<LoadedPolicy> oldUpdatedLostPolicies, Collection<RemovedPolicy> oldRemovedPolicies, Collection<LoadedPolicy> oldUpdatedPolicies, Boolean update, LoadedPolicy newUpdatedPolicy) { + // Look for policies which are not in Update + Boolean updated = true; + String policyName = newUpdatedPolicy.getPolicyName(); + String ver = newUpdatedPolicy.getVersionNo(); + for (LoadedPolicy oldUpdatedPolicy : notificationRecord.getLoadedPolicies()) { + if (policyName.equals(oldUpdatedPolicy.getPolicyName()) + && ver.equals(oldUpdatedPolicy.getVersionNo())) { + updated = false; + // Remove the policy from copy. + oldUpdatedLostPolicies.remove(oldUpdatedPolicy); + // Eliminating Duplicate. + oldUpdatedPolicies.remove(oldUpdatedPolicy); + } + } + // Change the record if the policy has been Removed earlier. + for (RemovedPolicy oldRemovedPolicy : notificationRecord.getRemovedPolicies()) { + if (oldRemovedPolicy.getPolicyName().equals(policyName) + && oldRemovedPolicy.getVersionNo().equals(ver)) { + oldRemovedPolicies.remove(oldRemovedPolicy); + } + } + if (updated) { + update = true; + updatedDelta.add((StdLoadedPolicy) newUpdatedPolicy); + } + // This will be converted to new Later + oldUpdatedPolicies.add(newUpdatedPolicy); + return update; + } + + private static Boolean updateRemovedPolicies(ArrayList<StdRemovedPolicy> removedDelta, Collection<LoadedPolicy> oldUpdatedLostPolicies, Collection<RemovedPolicy> oldRemovedPolicies, Collection<LoadedPolicy> oldUpdatedPolicies, Boolean remove, RemovedPolicy newRemovedPolicy) { + //Look for policy Not in Remove + Boolean removed = true; + String policyName = newRemovedPolicy.getPolicyName(); + String ver = newRemovedPolicy.getVersionNo(); + for (RemovedPolicy oldRemovedPolicy : notificationRecord.getRemovedPolicies()) { + if (policyName.equals(oldRemovedPolicy.getPolicyName()) + && ver.equals(oldRemovedPolicy.getVersionNo())) { + removed = false; + // Don't want a duplicate. + oldRemovedPolicies.remove(oldRemovedPolicy); + } + } + //We need to change our record we have an Update record of this remove. + for (LoadedPolicy oldUpdatedPolicy : notificationRecord.getLoadedPolicies()) { + if (policyName.equals(oldUpdatedPolicy.getPolicyName()) + && ver.equals(oldUpdatedPolicy.getVersionNo())) { + oldUpdatedPolicies.remove(oldUpdatedPolicy); + oldUpdatedLostPolicies.remove(oldUpdatedPolicy); + } + } + if (removed) { + remove = true; + notificationRecord.getRemovedPolicies().add(newRemovedPolicy); + removedDelta.add((StdRemovedPolicy) newRemovedPolicy); + } + // This will be converted to New Later. + oldRemovedPolicies.add(newRemovedPolicy); + return remove; + } + public static void recordNotification(StdPDPNotification notification) { - if (notification != null) { - if (notificationRecord.getRemovedPolicies() == null || notificationRecord.getLoadedPolicies() == null) { - notificationRecord = notification; - } else { - // Check if there is anything new and update the record. - if (notificationRecord.getLoadedPolicies() != null || notificationRecord.getRemovedPolicies() != null) { - HashSet<StdRemovedPolicy> removedPolicies = new HashSet<>(); - for (RemovedPolicy rPolicy : notificationRecord.getRemovedPolicies()) { - StdRemovedPolicy sRPolicy = new StdRemovedPolicy(); - sRPolicy.setPolicyName(rPolicy.getPolicyName()); - sRPolicy.setVersionNo(rPolicy.getVersionNo()); - removedPolicies.add(sRPolicy); - } - HashSet<StdLoadedPolicy> updatedPolicies = new HashSet<>(); - for (LoadedPolicy uPolicy : notificationRecord.getLoadedPolicies()) { - StdLoadedPolicy sUPolicy = new StdLoadedPolicy(); - sUPolicy.setMatches(uPolicy.getMatches()); - sUPolicy.setPolicyName(uPolicy.getPolicyName()); - sUPolicy.setVersionNo(uPolicy.getVersionNo()); - sUPolicy.setUpdateType(uPolicy.getUpdateType()); - updatedPolicies.add(sUPolicy); - } + if (notification == null) { + return; + } - // Checking with the new updated policies. - if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) { - for (LoadedPolicy newUpdatedPolicy : notification.getLoadedPolicies()) { - // If it was removed earlier then we need to remove from our record - Iterator<StdRemovedPolicy> oldRemovedPolicy = removedPolicies.iterator(); - String policyName = newUpdatedPolicy.getPolicyName(); - String ver = newUpdatedPolicy.getVersionNo(); - while (oldRemovedPolicy.hasNext()) { - RemovedPolicy policy = oldRemovedPolicy.next(); - if (policyName.equals(policy.getPolicyName()) - && ver.equals(policy.getVersionNo())) { - oldRemovedPolicy.remove(); - } - } - // If it was previously updated need to Overwrite it to the record. - updatedPolicies.removeIf(policy -> policyName.equals(policy.getPolicyName()) - && ver.equals(policy.getVersionNo())); + if (notificationRecord.getRemovedPolicies() == null || notificationRecord.getLoadedPolicies() == null) { + notificationRecord = notification; + } else { + // Check if there is anything new and update the record. + if (notificationRecord.getLoadedPolicies() != null || notificationRecord.getRemovedPolicies() != null) { + HashSet<StdRemovedPolicy> removedPolicies = new HashSet<>(); + for (RemovedPolicy rPolicy : notificationRecord.getRemovedPolicies()) { + StdRemovedPolicy sRPolicy = new StdRemovedPolicy(); + sRPolicy.setPolicyName(rPolicy.getPolicyName()); + sRPolicy.setVersionNo(rPolicy.getVersionNo()); + removedPolicies.add(sRPolicy); + } + HashSet<StdLoadedPolicy> updatedPolicies = new HashSet<>(); + for (LoadedPolicy uPolicy : notificationRecord.getLoadedPolicies()) { + StdLoadedPolicy sUPolicy = new StdLoadedPolicy(); + sUPolicy.setMatches(uPolicy.getMatches()); + sUPolicy.setPolicyName(uPolicy.getPolicyName()); + sUPolicy.setVersionNo(uPolicy.getVersionNo()); + sUPolicy.setUpdateType(uPolicy.getUpdateType()); + updatedPolicies.add(sUPolicy); + } - StdLoadedPolicy sUPolicy = new StdLoadedPolicy(); - sUPolicy.setMatches(newUpdatedPolicy.getMatches()); - sUPolicy.setPolicyName(newUpdatedPolicy.getPolicyName()); - sUPolicy.setVersionNo(newUpdatedPolicy.getVersionNo()); - sUPolicy.setUpdateType(newUpdatedPolicy.getUpdateType()); - updatedPolicies.add(sUPolicy); - } - } - // Checking with New Removed Policies. - if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) { - for (RemovedPolicy newRemovedPolicy : notification.getRemovedPolicies()) { - // If it was previously removed Overwrite it to the record. - Iterator<StdRemovedPolicy> oldRemovedPolicy = removedPolicies.iterator(); - String policyName = newRemovedPolicy.getPolicyName(); - String ver = newRemovedPolicy.getVersionNo(); - while (oldRemovedPolicy.hasNext()) { - RemovedPolicy policy = oldRemovedPolicy.next(); - if (policyName.equals(policy.getPolicyName()) - && ver.equals(policy.getVersionNo())) { - oldRemovedPolicy.remove(); - } - } - // If it was added earlier then we need to remove from our record. - updatedPolicies.removeIf(policy -> policyName.equals(policy.getPolicyName()) - && ver.equals(policy.getVersionNo())); + // Checking with the new updated policies. + if (notification.getLoadedPolicies() != null && !notification.getLoadedPolicies().isEmpty()) { + checkNewUpdatedPolicies(notification, removedPolicies, updatedPolicies); + } + // Checking with New Removed Policies. + if (notification.getRemovedPolicies() != null && !notification.getRemovedPolicies().isEmpty()) { + checkNewRemovedPolicies(notification, removedPolicies, updatedPolicies); + } + notificationRecord.setRemovedPolicies(removedPolicies); + notificationRecord.setLoadedPolicies(updatedPolicies); + } + if (!notificationRecord.getLoadedPolicies().isEmpty() && !notificationRecord.getRemovedPolicies() + .isEmpty()) { + notificationRecord.setNotificationType(NotificationType.BOTH); + } else if (!notificationRecord.getLoadedPolicies().isEmpty()) { + notificationRecord.setNotificationType(NotificationType.UPDATE); + } else if (!notificationRecord.getRemovedPolicies().isEmpty()) { + notificationRecord.setNotificationType(NotificationType.REMOVE); + } + } + } - StdRemovedPolicy sRPolicy = new StdRemovedPolicy(); - sRPolicy.setPolicyName(policyName); - sRPolicy.setVersionNo(ver); - removedPolicies.add(sRPolicy); - } - } - notificationRecord.setRemovedPolicies(removedPolicies); - notificationRecord.setLoadedPolicies(updatedPolicies); + private static void checkNewUpdatedPolicies(StdPDPNotification notification, HashSet<StdRemovedPolicy> removedPolicies, HashSet<StdLoadedPolicy> updatedPolicies) { + for (LoadedPolicy newUpdatedPolicy : notification.getLoadedPolicies()) { + // If it was removed earlier then we need to remove from our record + Iterator<StdRemovedPolicy> oldRemovedPolicy = removedPolicies.iterator(); + String policyName = newUpdatedPolicy.getPolicyName(); + String ver = newUpdatedPolicy.getVersionNo(); + while (oldRemovedPolicy.hasNext()) { + RemovedPolicy policy = oldRemovedPolicy.next(); + if (policyName.equals(policy.getPolicyName()) + && ver.equals(policy.getVersionNo())) { + oldRemovedPolicy.remove(); } - if (!notificationRecord.getLoadedPolicies().isEmpty() && !notificationRecord.getRemovedPolicies() - .isEmpty()) { - notificationRecord.setNotificationType(NotificationType.BOTH); - } else if (!notificationRecord.getLoadedPolicies().isEmpty()) { - notificationRecord.setNotificationType(NotificationType.UPDATE); - } else if (!notificationRecord.getRemovedPolicies().isEmpty()) { - notificationRecord.setNotificationType(NotificationType.REMOVE); + } + // If it was previously updated need to Overwrite it to the record. + updatedPolicies.removeIf(policy -> policyName.equals(policy.getPolicyName()) + && ver.equals(policy.getVersionNo())); + + StdLoadedPolicy sUPolicy = new StdLoadedPolicy(); + sUPolicy.setMatches(newUpdatedPolicy.getMatches()); + sUPolicy.setPolicyName(newUpdatedPolicy.getPolicyName()); + sUPolicy.setVersionNo(newUpdatedPolicy.getVersionNo()); + sUPolicy.setUpdateType(newUpdatedPolicy.getUpdateType()); + updatedPolicies.add(sUPolicy); + } + } + + private static void checkNewRemovedPolicies(StdPDPNotification notification, HashSet<StdRemovedPolicy> removedPolicies, HashSet<StdLoadedPolicy> updatedPolicies) { + for (RemovedPolicy newRemovedPolicy : notification.getRemovedPolicies()) { + // If it was previously removed Overwrite it to the record. + Iterator<StdRemovedPolicy> oldRemovedPolicy = removedPolicies.iterator(); + String policyName = newRemovedPolicy.getPolicyName(); + String ver = newRemovedPolicy.getVersionNo(); + while (oldRemovedPolicy.hasNext()) { + RemovedPolicy policy = oldRemovedPolicy.next(); + if (policyName.equals(policy.getPolicyName()) + && ver.equals(policy.getVersionNo())) { + oldRemovedPolicy.remove(); } } + // If it was added earlier then we need to remove from our record. + updatedPolicies.removeIf(policy -> policyName.equals(policy.getPolicyName()) + && ver.equals(policy.getVersionNo())); + + StdRemovedPolicy sRPolicy = new StdRemovedPolicy(); + sRPolicy.setPolicyName(policyName); + sRPolicy.setVersionNo(ver); + removedPolicies.add(sRPolicy); } } diff --git a/PolicyEngineUtils/src/main/java/org/onap/policy/utils/AAFPolicyClientImpl.java b/PolicyEngineUtils/src/main/java/org/onap/policy/utils/AAFPolicyClientImpl.java index 732183d47..5c46c76f7 100644 --- a/PolicyEngineUtils/src/main/java/org/onap/policy/utils/AAFPolicyClientImpl.java +++ b/PolicyEngineUtils/src/main/java/org/onap/policy/utils/AAFPolicyClientImpl.java @@ -3,6 +3,7 @@ * PolicyEngineUtils * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,173 +43,175 @@ import org.onap.aaf.cadi.principal.UnAuthPrincipal; * */ public class AAFPolicyClientImpl implements AAFPolicyClient{ - private static Logger logger = Logger.getLogger(AAFPolicyClientImpl.class.getName()); - - private static final String ENVIRONMENT = "ENVIRONMENT"; - - // Warning Please don't Change these Values. Confirm with AAF team. - private static final String DEVL_AAF_URL = ""; - private static final String TEST_AAF_URL = ""; - private static final String PROD_AAF_URL = ""; - private static final String DEFAULT_AFT_LATITUDE = "32.780140"; - private static final String DEFAULT_AFT_LONGITUDE = "-96.800451"; - private static final String TEST_AFT_ENVIRONMENT = "AFTUAT"; - private static final String PROD_AFT_ENVIRONMENT = "AFTPRD"; - private static final String DEFAULT_AAF_USER_EXPIRES = Integer.toString(5*60000); // 5 minutes for found items to live in cache - private static final String DEFAULT_AAF_HIGH_COUNT = Integer.toString(400); // Maximum number of items in Cache - - private static AAFPolicyClientImpl instance = null; - - private static Properties props = new Properties(); - private static AAFCon<?> aafCon = null; - private static AAFLurPerm aafLurPerm = null; - private static AAFAuthn<?> aafAuthn = null; - private static PropAccess access = null; - - private AAFPolicyClientImpl(Properties properties) throws AAFPolicyException{ - setup(properties); - } - - /** - * Gets the instance of the AAFClient instance. Needs Proper properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT - * - * @param properties Properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT - * @return AAFClient instance. - * @throws AAFPolicyException Exceptions. - */ - public static synchronized AAFPolicyClientImpl getInstance(Properties properties) throws AAFPolicyException{ - if(instance == null) { - logger.info("Creating AAFClient Instance "); - instance = new AAFPolicyClientImpl(properties); - } - return instance; - } - - // To set Property values && Connections. - private static void setup(Properties properties) throws AAFPolicyException { - if(properties!=null && !properties.isEmpty()){ - props = System.getProperties(); - props.setProperty("AFT_LATITUDE", properties.getProperty("AFT_LATITUDE", DEFAULT_AFT_LATITUDE)); - props.setProperty("AFT_LONGITUDE", properties.getProperty("AFT_LONGITUDE", DEFAULT_AFT_LONGITUDE)); - String aftEnv = TEST_AFT_ENVIRONMENT; - props.setProperty("aaf_id",properties.getProperty("aaf_id", "aafID")); - props.setProperty("aaf_password", properties.getProperty("aaf_password", "aafPass")); - if(properties.containsKey(Config.AAF_URL)){ - // if given a value in properties file. - props.setProperty(Config.AAF_URL, properties.getProperty(Config.AAF_URL)); - }else{ - // Set Default values. - if(properties.getProperty(ENVIRONMENT, "DEVL").equalsIgnoreCase(AAFEnvironment.TEST.toString())){ - props.setProperty(Config.AAF_URL, TEST_AAF_URL); - }else if(properties.getProperty(ENVIRONMENT, "DEVL").equalsIgnoreCase(AAFEnvironment.PROD.toString())){ - props.setProperty(Config.AAF_URL, PROD_AAF_URL); - aftEnv = PROD_AFT_ENVIRONMENT; - }else{ - props.setProperty(Config.AAF_URL, DEVL_AAF_URL); - } - } - props.setProperty("AFT_ENVIRONMENT", properties.getProperty("AFT_ENVIRONMENT", aftEnv)); - props.setProperty(Config.AAF_USER_EXPIRES, properties.getProperty(Config.AAF_USER_EXPIRES, DEFAULT_AAF_USER_EXPIRES)); - props.setProperty(Config.AAF_HIGH_COUNT, properties.getProperty(Config.AAF_HIGH_COUNT, DEFAULT_AAF_HIGH_COUNT)); - }else{ - logger.error("Required Property value is missing : " + ENVIRONMENT); - throw new AAFPolicyException("Required Property value is missing : " + ENVIRONMENT); - } - access = new PolicyAccess(props, Level.valueOf(properties.getProperty("AAF_LOG_LEVEL", Level.ERROR.toString()))); - setUpAAF(); - } - - /** - * Updates the Properties file in case if required. - * - * @param properties Properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT - * @throws AAFPolicyException exceptions if any. - */ - @Override - public void updateProperties(Properties properties) throws AAFPolicyException{ - setup(properties); - } - - /** - * Checks the Authentication and Permissions for the given values. - * - * @param mechID MechID or ATT ID must be registered under the Name space. - * @param pass Password pertaining to the MechID or ATTID. - * @param type Permissions Type. - * @param instance Permissions Instance. - * @param action Permissions Action. - * @return - */ - @Override - public boolean checkAuthPerm(String mechID, String pass, String type, String instance, String action){ - return checkAuth(mechID, pass) && checkPerm(mechID, pass, type, instance, action); - } - - /** - * Checks the Authentication of the UserName and Password Given. - * - * @param userName UserName or MechID - * @param pass Password. - * @return True or False. - */ - @Override - public boolean checkAuth(String userName, String pass){ - if(aafAuthn!=null){ - try { - int i=0; - do{ - if(aafAuthn.validate(userName, pass)==null){ - return true; - } - i++; - }while(i<2); - } catch (Exception e) { - logger.error(e.getMessage() + e); - } - } - return false; - } - - /** - * Checks Permissions for the given UserName, Password and Type, Instance Action. - * - * @param userName UserName or MechID - * @param pass Password. - * @param type Permissions Type. - * @param instance Permissions Instance. - * @param action Permissions Action. - * @return True or False. - */ - @Override - public boolean checkPerm(String userName, String pass, String type, String instance, String action){ - int i =0; - Boolean result= false; - do{ - if(aafCon!=null && aafLurPerm !=null){ - try { - aafCon.basicAuth(userName, pass); - AAFPermission perm = new AAFPermission(type, instance, action); - final Principal p = new UnAuthPrincipal(userName); - result = aafLurPerm.fish(p, perm); - } catch (CadiException e) { - logger.error(e.getMessage() + e); - aafLurPerm.destroy(); - } - } - i++; - }while(i<2 && !result); // Try once more to check if this can be passed. AAF has some issues. - return result; - } - - private static boolean setUpAAF(){ - try { - aafCon = new AAFConHttp(access,new PropertyLocator("https://aaf-onap-beijing-test.osaaf.org:8100")); - aafLurPerm = aafCon.newLur(); - aafAuthn = aafCon.newAuthn(aafLurPerm); - return true; - } catch (Exception e) { - logger.error("Error while setting up AAF Connection " + e.getMessage() + e); - return false; - } - } + private static Logger logger = Logger.getLogger(AAFPolicyClientImpl.class.getName()); + + private static final String ENVIRONMENT = "ENVIRONMENT"; + + // Warning Please don't Change these Values. Confirm with AAF team. + private static final String DEVL_AAF_URL = ""; + private static final String TEST_AAF_URL = ""; + private static final String PROD_AAF_URL = ""; + private static final String DEFAULT_AFT_LATITUDE = "32.780140"; + private static final String DEFAULT_AFT_LONGITUDE = "-96.800451"; + private static final String TEST_AFT_ENVIRONMENT = "AFTUAT"; + private static final String PROD_AFT_ENVIRONMENT = "AFTPRD"; + private static final String DEFAULT_AAF_USER_EXPIRES = Integer.toString(5*60000); // 5 minutes for found items to live in cache + private static final String DEFAULT_AAF_HIGH_COUNT = Integer.toString(400); // Maximum number of items in Cache + + private static AAFPolicyClientImpl instance = null; + + private static Properties props = new Properties(); + private static AAFCon<?> aafCon = null; + private static AAFLurPerm aafLurPerm = null; + private static AAFAuthn<?> aafAuthn = null; + private static PropAccess access = null; + + private AAFPolicyClientImpl(Properties properties) throws AAFPolicyException{ + setup(properties); + } + + /** + * Gets the instance of the AAFClient instance. Needs Proper properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT + * + * @param properties Properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT + * @return AAFClient instance. + * @throws AAFPolicyException Exceptions. + */ + public static synchronized AAFPolicyClientImpl getInstance(Properties properties) throws AAFPolicyException{ + if(instance == null) { + logger.info("Creating AAFClient Instance "); + instance = new AAFPolicyClientImpl(properties); + } + return instance; + } + + // To set Property values && Connections. + private static void setup(Properties properties) throws AAFPolicyException { + if(properties!=null && !properties.isEmpty()){ + props = System.getProperties(); + props.setProperty("AFT_LATITUDE", properties.getProperty("AFT_LATITUDE", DEFAULT_AFT_LATITUDE)); + props.setProperty("AFT_LONGITUDE", properties.getProperty("AFT_LONGITUDE", DEFAULT_AFT_LONGITUDE)); + String aftEnv = TEST_AFT_ENVIRONMENT; + props.setProperty("aaf_id",properties.getProperty("aaf_id", "aafID")); + props.setProperty("aaf_password", properties.getProperty("aaf_password", "aafPass")); + if(properties.containsKey(Config.AAF_URL)){ + // if given a value in properties file. + props.setProperty(Config.AAF_URL, properties.getProperty(Config.AAF_URL)); + }else{ + // Set Default values. + if(properties.getProperty(ENVIRONMENT, "DEVL").equalsIgnoreCase(AAFEnvironment.TEST.toString())){ + props.setProperty(Config.AAF_URL, TEST_AAF_URL); + }else if(properties.getProperty(ENVIRONMENT, "DEVL").equalsIgnoreCase(AAFEnvironment.PROD.toString())){ + props.setProperty(Config.AAF_URL, PROD_AAF_URL); + aftEnv = PROD_AFT_ENVIRONMENT; + }else{ + props.setProperty(Config.AAF_URL, DEVL_AAF_URL); + } + } + props.setProperty("AFT_ENVIRONMENT", properties.getProperty("AFT_ENVIRONMENT", aftEnv)); + props.setProperty(Config.AAF_USER_EXPIRES, properties.getProperty(Config.AAF_USER_EXPIRES, DEFAULT_AAF_USER_EXPIRES)); + props.setProperty(Config.AAF_HIGH_COUNT, properties.getProperty(Config.AAF_HIGH_COUNT, DEFAULT_AAF_HIGH_COUNT)); + }else{ + logger.error("Required Property value is missing : " + ENVIRONMENT); + throw new AAFPolicyException("Required Property value is missing : " + ENVIRONMENT); + } + access = new PolicyAccess(props, Level.valueOf(properties.getProperty("AAF_LOG_LEVEL", Level.ERROR.toString()))); + setUpAAF(); + } + + /** + * Updates the Properties file in case if required. + * + * @param properties Properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT + * @throws AAFPolicyException exceptions if any. + */ + @Override + public void updateProperties(Properties properties) throws AAFPolicyException{ + setup(properties); + } + + /** + * Checks the Authentication and Permissions for the given values. + * + * @param mechID MechID or ATT ID must be registered under the Name space. + * @param pass Password pertaining to the MechID or ATTID. + * @param type Permissions Type. + * @param instance Permissions Instance. + * @param action Permissions Action. + * @return + */ + @Override + public boolean checkAuthPerm(String mechID, String pass, String type, String instance, String action){ + return checkAuth(mechID, pass) && checkPerm(mechID, pass, type, instance, action); + } + + /** + * Checks the Authentication of the UserName and Password Given. + * + * @param userName UserName or MechID + * @param pass Password. + * @return True or False. + */ + @Override + public boolean checkAuth(String userName, String pass){ + if (aafAuthn == null) { + return false; + } + try { + int i=0; + do{ + if(aafAuthn.validate(userName, pass)==null){ + return true; + } + i++; + }while(i<2); + } catch (Exception e) { + logger.error(e.getMessage() + e); + } + + return false; + } + + /** + * Checks Permissions for the given UserName, Password and Type, Instance Action. + * + * @param userName UserName or MechID + * @param pass Password. + * @param type Permissions Type. + * @param instance Permissions Instance. + * @param action Permissions Action. + * @return True or False. + */ + @Override + public boolean checkPerm(String userName, String pass, String type, String instance, String action){ + int i =0; + Boolean result= false; + do{ + if(aafCon!=null && aafLurPerm !=null){ + try { + aafCon.basicAuth(userName, pass); + AAFPermission perm = new AAFPermission(type, instance, action); + final Principal p = new UnAuthPrincipal(userName); + result = aafLurPerm.fish(p, perm); + } catch (CadiException e) { + logger.error(e.getMessage() + e); + aafLurPerm.destroy(); + } + } + i++; + }while(i<2 && !result); // Try once more to check if this can be passed. AAF has some issues. + return result; + } + + private static boolean setUpAAF(){ + try { + aafCon = new AAFConHttp(access,new PropertyLocator("https://aaf-onap-beijing-test.osaaf.org:8100")); + aafLurPerm = aafCon.newLur(); + aafAuthn = aafCon.newAuthn(aafLurPerm); + return true; + } catch (Exception e) { + logger.error("Error while setting up AAF Connection " + e.getMessage() + e); + return false; + } + } } |