*
*/
public class StdEngine extends StdPDPItemSetChangeNotifier implements PAPPolicyEngine {
- public static final String pipPropertyFile = "pip.properties";
+ public static final String PIP_PROPERTY_FILE = "pip.properties";
- private static final String addGroup = "addGroup ";
+ private static final String STR_ADDGROUP = "addGroup ";
+ private static final String STR_CLASS = "StdEngine";
+ private static final String STR_APPEND_NAME = ".name";
+ private static final String STR_APPEND_DESCRIPTION = ".description";
+ private static final String STR_APPEND_PDPS = ".pdps";
private static Log logger = LogFactory.getLog(StdEngine.class);
if (this.repository == null) {
throw new PAPException("No repository specified.");
}
- if (Files.notExists(this.repository)) {
+ if (! this.repository.toFile().exists()) {
Files.createDirectory(repository);
}
- if (!Files.isDirectory(this.repository)) {
+ if (! this.repository.toFile().isDirectory()) {
throw new PAPException("Repository is NOT a directory: " + this.repository.toAbsolutePath());
}
- if (!Files.isWritable(this.repository)) {
+ if (! this.repository.toFile().canWrite()) {
throw new PAPException("Repository is NOT writable: " + this.repository.toAbsolutePath());
}
//
//
this.groups = this.readProperties(this.repository, properties);
} catch (IOException e) {
- PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdEngine", "Failed to load properties file");
+ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, STR_CLASS, "Failed to load properties file");
this.groups = new HashSet<>();
}
//
//
// Does it exist?
//
- if (Files.notExists(defaultPath)) {
+ if (! defaultPath.toFile().exists()) {
//
// Create its directory
//
try (OutputStream os = Files.newOutputStream(policyPath)) {
props.store(os, "");
} catch (IOException e) {
- PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdEngine",
+ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, STR_CLASS,
"Failed to write default policy properties");
}
}
try (OutputStream os = Files.newOutputStream(pipPath)) {
props.store(os, "");
} catch (IOException e) {
- PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdEngine",
+ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, STR_CLASS,
"Failed to write default pip properties");
}
}
//
// Create the default group
//
- StdPDPGroup newDefault = new StdPDPGroup(defaultId, true, "default",
+ StdPDPGroup newDefault = new StdPDPGroup(defaultId, true, PROP_PAP_GROUPS_DEFAULT_NAME,
"The default group where new PDP's are put.", defaultPath);
//
// Add it to our list
properties.store(os, "");
}
} catch (IOException e) {
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "StdEngine",
+ PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, STR_CLASS,
"Failed to save properties with new default group information.");
}
//
wasDefaultGroupJustAdded = true;
return newDefault;
} catch (IOException e) {
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "StdEngine", "Failed to create default group");
+ PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, STR_CLASS, "Failed to create default group");
throw new PAPException("Failed to create default group");
}
}
// If it exists already
//
if (Files.exists(groupPath)) {
- logger.warn(addGroup + id + " directory exists");
+ logger.warn(STR_ADDGROUP + id + " directory exists");
} else {
try {
//
//
Files.createDirectory(groupPath);
} catch (IOException e) {
- PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdEngine", "Failed to create " + groupPath);
+ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, STR_CLASS, "Failed to create " + groupPath);
throw new PAPException("Failed to create " + id);
}
}
//
Path policyProperties = Paths.get(groupPath.toString(), "xacml.policy.properties");
- if (Files.exists(policyProperties)) {
- logger.warn(addGroup + id + " file exists");
+ if (policyProperties.toFile().exists()) {
+ logger.warn(STR_ADDGROUP + id + " file exists");
} else {
Properties props = new Properties();
props.setProperty(XACMLProperties.PROP_REFERENCEDPOLICIES, "");
props.store(os, "");
}
} catch (IOException e) {
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "StdEngine", "Failed to create policyProperties");
+ PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, STR_CLASS, "Failed to create policyProperties");
throw new PAPException("Failed to create " + id);
}
}
//
Path pipProperties = Paths.get(groupPath.toString(), "xacml.pip.properties");
Properties props = new Properties();
- if (Files.exists(pipProperties)) {
- logger.warn(addGroup + id + " file exists.");
+ if (pipProperties.toFile().exists()) {
+ logger.warn(STR_ADDGROUP + id + " file exists.");
} else {
try {
props = setPipProperties(props);
props.store(os, "");
}
} catch (IOException e) {
- PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdEngine", "Failed to create pipProperties");
+ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, STR_CLASS, "Failed to create pipProperties");
throw new PAPException("Failed to create " + id);
}
}
@Override
- public void copyPolicy(PDPPolicy policy, OnapPDPGroup group) throws PAPException {
+ public void copyPolicy(PDPPolicy policy, OnapPDPGroup group, String userId) throws PAPException {
//
// Currently not used on the PAP side. This is done by ((StdPDPGroup) group).copyPolicyToFile
//
List<String> ids = new ArrayList<>();
for (PDPGroup group : this.groups) {
ids.add(group.getId());
- properties.setProperty(group.getId() + ".name", group.getName() == null ? "" : group.getName());
- properties.setProperty(group.getId() + ".description",
+ properties.setProperty(group.getId() + STR_APPEND_NAME, group.getName() == null ? "" : group.getName());
+ properties.setProperty(group.getId() + STR_APPEND_DESCRIPTION,
group.getDescription() == null ? "" : group.getDescription());
//
// Iterate its PDPs
List<String> pdps = new ArrayList<>();
for (PDP pdp : group.getPdps()) {
pdps.add(pdp.getId());
- properties.setProperty(pdp.getId() + ".name", pdp.getName() == null ? "" : pdp.getName());
- properties.setProperty(pdp.getId() + ".description",
+ properties.setProperty(pdp.getId() + STR_APPEND_NAME, pdp.getName() == null ? "" : pdp.getName());
+ properties.setProperty(pdp.getId() + STR_APPEND_DESCRIPTION,
pdp.getDescription() == null ? "" : pdp.getDescription());
if (pdp instanceof OnapPDP) {
properties.setProperty(pdp.getId() + ".jmxport",
if (logger.isDebugEnabled()) {
logger.debug("Group " + group.getId() + " PDPS: " + pdpList);
}
- properties.setProperty(group.getId() + ".pdps", pdpList);
+ properties.setProperty(group.getId() + STR_APPEND_PDPS, pdpList);
}
if (ids.isEmpty()) {
throw new PAPException("Inconsistency - we have NO groups. We should have at least one.");
}
}
- /**
- * removeGroupProperties.
- *
- * @param id String
- * @param properties Properties
- */
- public static void removeGroupProperties(String id, Properties properties) {
- for (Object key : properties.keySet()) {
- if (key.toString().startsWith(id + ".")) {
- properties.remove(key);
- }
- }
- }
-
/**
* setGroupProperties.
*
//
// Set its properties
//
- properties.setProperty(group.getId() + ".name", group.getName());
- properties.setProperty(group.getId() + ".description", group.getDescription());
+ properties.setProperty(group.getId() + STR_APPEND_NAME, group.getName());
+ properties.setProperty(group.getId() + STR_APPEND_DESCRIPTION, group.getDescription());
//
// Set its PDP list
//
}
pdpList = Joiner.on(',').skipNulls().join(ids);
}
- properties.setProperty(group.getId() + ".pdps", pdpList);
+ properties.setProperty(group.getId() + STR_APPEND_PDPS, pdpList);
} else {
- properties.setProperty(group.getId() + ".pdps", "");
+ properties.setProperty(group.getId() + STR_APPEND_PDPS, "");
}
}
//
this.saveConfiguration();
} catch (IOException | PAPException e) {
- PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "StdEngine", "Failed to save configuration");
+ PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, STR_CLASS, "Failed to save configuration");
}
}
props.setProperty("AAF.description", "AAFEngine to communicate with AAF to take decisions");
props.setProperty("AAF.classname", "org.onap.policy.xacml.std.pip.engines.aaf.AAFEngine");
// read from PIP properties file.
- Path file = Paths.get(pipPropertyFile);
- if (!Files.notExists(file)) {
+ Path file = Paths.get(PIP_PROPERTY_FILE);
+ if (file.toFile().exists()) {
InputStream in;
Properties prop = new Properties();
try {
throw new PAPException("Unable to rename directory; reason unknown");
}
} catch (Exception e) {
- PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "StdEngine", "Unable to rename directory");
+ PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, STR_CLASS, "Unable to rename directory");
throw new PAPException(
"Unable to move directory from '" + oldPath + "' to '" + newPath + "': " + e.getMessage(), e);
}
//
// If it exists already
//
- if (!Files.exists(groupPath)) {
+ if (! groupPath.toFile().exists()) {
logger.warn("removeGroup " + id + " directory does not exist" + groupPath.toString());
} else {
try {
//
Files.delete(groupPath);
} catch (IOException e) {
- PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdEngine", "Failed to delete " + groupPath);
+ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, STR_CLASS, "Failed to delete " + groupPath);
throw new PAPException("Failed to delete " + id);
}
}