- if (this.groups.add(newGroup)) {
- // save the new group in our properties and notify any listeners of the change
- groupChanged(newGroup);
- }
-
- }
-
-
-
-
- /**
- * Helper to create a new Group ID.
- * Use the Name field to create the Id.
- * The Name is expected to not be null; if it is then this method throws an exception.
- * The name is supposed to be unique within the current set of groups,
- * so creating the ID based on the name will create a unique string.
- *
- * @param name
- * @return
- */
- private String createNewPDPGroupId(String name) {
- String id = name;
- // replace "bad" characters with sequences that will be ok for file names and properties keys.
- id = id.replace(" ", "_sp_");
- id = id.replace("\t", "_tab_");
- id = id.replace("\\", "_bksl_");
- id = id.replace("/", "_sl_");
- id = id.replace(":", "_col_");
- id = id.replace("*", "_ast_");
- id = id.replace("?", "_q_");
- id = id.replace("\"", "_quo_");
- id = id.replace("<", "_lt_");
- id = id.replace(">", "_gt_");
- id = id.replace("|", "_bar_");
- id = id.replace("=", "_eq_");
- id = id.replace(",", "_com_");
- id = id.replace(";", "_scom_");
-
- return id;
- }
-
-
- @Override
- public OnapPDP getPDP(String pdpId) throws PAPException {
- for (OnapPDPGroup group : this.groups) {
- for (OnapPDP pdp : group.getOnapPdps()) {
- if (pdp.getId().equals(pdpId)) {
- return pdp;
- }
- }
- }
- return null;
- }
-
-
- @Override
- public void movePDP(OnapPDP pdp, OnapPDPGroup newGroup) throws PAPException {
- if (newGroup == null) {
- throw new NullPointerException("You must specify which group the PDP will belong to.");
- }
- PDPGroup currentGroup = this.getPDPGroup(pdp);
- if (currentGroup == null) {
- throw new PAPException("PDP must already belong to a group.");
- }
- if (currentGroup.equals(newGroup)) {
- logger.warn("Already in that group.");
- return;
- }
- if (currentGroup instanceof StdPDPGroup && newGroup instanceof StdPDPGroup) {
- if (((StdPDPGroup) currentGroup).removePDP(pdp)) {
- boolean result = ((StdPDPGroup) newGroup).addPDP(pdp);
- if (result) {
- //
- // Save the configuration
- //
- this.doSave();
- } else {
- PolicyLogger.error("Failed to add to new group, putting back into original group.");
- if (!((StdPDPGroup) currentGroup).removePDP(pdp)) {
- PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Failed to put PDP back into original group.");
- }
- }
- }
- } else {
- String message = "Unknown PDP group class: " + newGroup.getClass().getCanonicalName() + " and " + currentGroup.getClass().getCanonicalName();
- logger.warn(message);
- throw new PAPException(message);
- }
- }
-
-
- @Override
- public void updatePDP(OnapPDP pdp) throws PAPException {
- PDP currentPDP = this.getPDP(pdp.getId());
- if (currentPDP == null) {
- String message = "Unknown PDP id '" + pdp.getId() + "'";
- logger.warn(message);
- throw new PAPException(message);
- }
-
- // the only things that the user can change are name and description
- currentPDP.setDescription(pdp.getDescription());
- currentPDP.setName(pdp.getName());
- if (currentPDP instanceof OnapPDP) {
- ((OnapPDP)currentPDP).setJmxPort(pdp.getJmxPort());
- }
- this.doSave();
- }
-
- @Override
- public void removePDP(OnapPDP pdp) throws PAPException {
- PDPGroup group = this.getPDPGroup(pdp);
- if (group == null) {
- throw new NullPointerException();
- }
- if (group instanceof StdPDPGroup) {
- boolean result = ((StdPDPGroup) group).removePDP(pdp);
- if (result) {
- this.doSave();
- }
- return;
- }
- String message = "Unknown PDP group class: " + group.getClass().getCanonicalName();
- logger.warn(message);
- throw new PAPException(message);
- }
-
-
- @Override
- /**
- * Should never be called - Detailed status is held on the PDP, not the PAP
- */
- public PDPStatus getStatus(OnapPDP pdp) throws PAPException {
- return getPDP(pdp.getId()).getStatus();
- }
-
- @Override
- public void publishPolicy(String id, String name, boolean isRoot, InputStream policy, OnapPDPGroup group) throws PAPException {
- if (group == null) {
- throw new NullPointerException();
- }
- if (group instanceof StdPDPGroup && this.groups.contains(group)) {
- ((StdPDPGroup) group).publishPolicy(id, name, isRoot, policy);
- return;
- }
- logger.warn("unknown PDP Group: " + group);
- throw new PAPException("Unknown PDP Group: " + group.getId());
- }
-
-
- @Override
- public void copyPolicy(PDPPolicy policy, OnapPDPGroup group)
- throws PAPException {
- //
- // Currently not used on the PAP side. This is done by ((StdPDPGroup) group).copyPolicyToFile
- //
- }
-
-
- @Override
- public void removePolicy(PDPPolicy policy, OnapPDPGroup group) throws PAPException {
- if (group == null) {
- throw new NullPointerException();
- }
- if (group instanceof StdPDPGroup && this.groups.contains(group)) {
- ((StdPDPGroup) group).removePolicy(policy);
- return;
- }
- logger.warn("unknown PDP Group: " + group);
- throw new PAPException("Unknown PDP Group: " + group.getId());
- }
-
-
- //
- // HELPER methods
- //
-
- private Set<StdPDPGroup> readProperties(Path repository, Properties properties) throws PAPException {
- Set<StdPDPGroup> pdpGroups = new HashSet<>();
- //
- // See if there is a groups property
- //
- String groupList = properties.getProperty(PROP_PAP_GROUPS, "");
- if (groupList == null) {
- logger.warn("null group list " + PROP_PAP_GROUPS);
- groupList = "";
- }
- if (logger.isDebugEnabled()) {
- logger.debug("group list: " + groupList);
- }
- //
- // Iterate the groups, converting to a set ensures we have unique groups.
- //
- for (String id : Splitter.on(',').trimResults().omitEmptyStrings().split(groupList)) {
- //
- // Add our Group Object
- //
- StdPDPGroup g = new StdPDPGroup(id.trim(),
- id.equals(properties.getProperty(PROP_PAP_GROUPS_DEFAULT, PROP_PAP_GROUPS_DEFAULT_NAME)),
- properties,
- Paths.get(repository.toString(), id));
-
- //
- // Add it in
- //
- pdpGroups.add(g);
- }
- //
- // Dump what we got
- //
- if (logger.isDebugEnabled()) {
- logger.debug("PDP Group List: " + pdpGroups.toString());
- }
- return pdpGroups;
- }
-
- private void saveConfiguration() throws PAPException, IOException {
- //
- // Create our properties object
- //
- Properties properties = new Properties() {
- private static final long serialVersionUID = 1L;
- // For Debugging it is helpful for the file to be in a sorted order,
- // any by returning the keys in the natural Alpha order for strings we get close enough.
- // TreeSet is sorted, and this just overrides the normal Properties method to get the keys.
- @Override
- public synchronized Enumeration<Object> keys() {
- return Collections.enumeration(new TreeSet<Object>(super.keySet()));
- }
- };
- //
- // Iterate our groups
- //
- 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", 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", pdp.getDescription() == null ? "" : pdp.getDescription());
- if (pdp instanceof OnapPDP) {
- properties.setProperty(pdp.getId() + ".jmxport", (((OnapPDP)pdp).getJmxPort()==0 ? "" : ((OnapPDP)pdp).getJmxPort()).toString());
- }
- }
- String pdpList = "";
- if (pdps.size() == 1) {
- pdpList = pdps.get(0);
- } else if (pdps.size() > 1) {
- pdpList = Joiner.on(',').skipNulls().join(pdps);
- }
- if (logger.isDebugEnabled()) {
- logger.debug("Group " + group.getId() + " PDPS: " + pdpList);
- }
- properties.setProperty(group.getId() + ".pdps", pdpList);
- }
- if (ids.isEmpty()) {
- throw new PAPException("Inconsistency - we have NO groups. We should have at least one.");
- }
- String groupList = "";
- if (ids.size() == 1) {
- groupList = ids.get(0);
- } else if (ids.size() > 1){
- groupList = Joiner.on(',').skipNulls().join(ids);
- }
- logger.info("New Group List: " + groupList);
-
- properties.setProperty(PROP_PAP_GROUPS, groupList);
- //
- // Get the default group
- //
- PDPGroup defaultGroup = this.getDefaultGroup();
- if (defaultGroup == null) {
- throw new PAPException("Invalid state - no default group.");
- }
- properties.setProperty(PROP_PAP_GROUPS_DEFAULT, defaultGroup.getId());
- //
- // Now we can save the file
- //
- Path file = Paths.get(this.repository.toString(), "xacml.properties");
- try (OutputStream os = Files.newOutputStream(file)) {
- properties.store(os, "");
- }
- }
-
- public static void removeGroupProperties(String id, Properties properties) {
- for (Object key : properties.keySet()) {
- if (key.toString().startsWith(id + ".")) {
- properties.remove(key);
- }
- }
- }
-
- public static void setGroupProperties(PDPGroup group, Properties properties) {
- //
- // make sure its in the list of groups
- //
- Iterable<String> groups = Splitter.on(',').trimResults().omitEmptyStrings().split( properties.getProperty(PROP_PAP_GROUPS, ""));
- boolean inList = false;
- for (String g : groups) {
- if (g.equals(group.getId())) {
- inList = true;
- }
- }
- if (!inList) {
- Set<String> grps = Sets.newHashSet(groups);
- grps.add(group.getId());
- String newGroupList;
- if (grps.size() == 1) {
- newGroupList = grps.iterator().next();
- } else if (grps.size() > 1) {
- newGroupList = Joiner.on(',').skipNulls().join(grps);
- } else {
+ if (this.groups.add(newGroup)) {
+ // save the new group in our properties and notify any listeners of the change
+ groupChanged(newGroup);
+ }
+
+ }
+
+
+
+ /**
+ * Helper to create a new Group ID. Use the Name field to create the Id. The Name is expected to not be null; if it
+ * is then this method throws an exception. The name is supposed to be unique within the current set of groups, so
+ * creating the ID based on the name will create a unique string.
+ *
+ * @param name
+ * @return
+ */
+ private String createNewPDPGroupId(String name) {
+ String id = name;
+ // replace "bad" characters with sequences that will be ok for file names and properties keys.
+ id = id.replace(" ", "_sp_");
+ id = id.replace("\t", "_tab_");
+ id = id.replace("\\", "_bksl_");
+ id = id.replace("/", "_sl_");
+ id = id.replace(":", "_col_");
+ id = id.replace("*", "_ast_");
+ id = id.replace("?", "_q_");
+ id = id.replace("\"", "_quo_");
+ id = id.replace("<", "_lt_");
+ id = id.replace(">", "_gt_");
+ id = id.replace("|", "_bar_");
+ id = id.replace("=", "_eq_");
+ id = id.replace(",", "_com_");
+ id = id.replace(";", "_scom_");
+
+ return id;
+ }
+
+
+ @Override
+ public OnapPDP getPDP(String pdpId) throws PAPException {
+ for (OnapPDPGroup group : this.groups) {
+ for (OnapPDP pdp : group.getOnapPdps()) {
+ if (pdp.getId().equals(pdpId)) {
+ return pdp;
+ }
+ }
+ }
+ return null;
+ }
+
+
+ @Override
+ public void movePDP(OnapPDP pdp, OnapPDPGroup newGroup) throws PAPException {
+ if (newGroup == null) {
+ throw new NullPointerException("You must specify which group the PDP will belong to.");
+ }
+ PDPGroup currentGroup = this.getPDPGroup(pdp);
+ if (currentGroup == null) {
+ throw new PAPException("PDP must already belong to a group.");
+ }
+ if (currentGroup.equals(newGroup)) {
+ logger.warn("Already in that group.");
+ return;
+ }
+ if (currentGroup instanceof StdPDPGroup && newGroup instanceof StdPDPGroup) {
+ if (((StdPDPGroup) currentGroup).removePDP(pdp)) {
+ boolean result = ((StdPDPGroup) newGroup).addPDP(pdp);
+ if (result) {
+ //
+ // Save the configuration
+ //
+ this.doSave();
+ } else {
+ PolicyLogger.error("Failed to add to new group, putting back into original group.");
+ if (!((StdPDPGroup) currentGroup).removePDP(pdp)) {
+ PolicyLogger
+ .error(MessageCodes.ERROR_DATA_ISSUE + "Failed to put PDP back into original group.");
+ }
+ }
+ }
+ } else {
+ String message = "Unknown PDP group class: " + newGroup.getClass().getCanonicalName() + " and "
+ + currentGroup.getClass().getCanonicalName();
+ logger.warn(message);
+ throw new PAPException(message);
+ }
+ }
+
+
+ @Override
+ public void updatePDP(OnapPDP pdp) throws PAPException {
+ PDP currentPDP = this.getPDP(pdp.getId());
+ if (currentPDP == null) {
+ String message = "Unknown PDP id '" + pdp.getId() + "'";
+ logger.warn(message);
+ throw new PAPException(message);
+ }
+
+ // the only things that the user can change are name and description
+ currentPDP.setDescription(pdp.getDescription());
+ currentPDP.setName(pdp.getName());
+ if (currentPDP instanceof OnapPDP) {
+ ((OnapPDP) currentPDP).setJmxPort(pdp.getJmxPort());
+ }
+ this.doSave();
+ }
+
+ @Override
+ public void removePDP(OnapPDP pdp) throws PAPException {
+ PDPGroup group = this.getPDPGroup(pdp);
+ if (group == null) {
+ throw new NullPointerException();
+ }
+ if (group instanceof StdPDPGroup) {
+ boolean result = ((StdPDPGroup) group).removePDP(pdp);
+ if (result) {
+ this.doSave();
+ }
+ return;
+ }
+ String message = "Unknown PDP group class: " + group.getClass().getCanonicalName();
+ logger.warn(message);
+ throw new PAPException(message);
+ }
+
+
+ @Override
+ /**
+ * Should never be called - Detailed status is held on the PDP, not the PAP
+ */
+ public PDPStatus getStatus(OnapPDP pdp) throws PAPException {
+ return getPDP(pdp.getId()).getStatus();
+ }
+
+ @Override
+ public void publishPolicy(String id, String name, boolean isRoot, InputStream policy, OnapPDPGroup group)
+ throws PAPException {
+ if (group == null) {
+ throw new NullPointerException();
+ }
+ if (group instanceof StdPDPGroup && this.groups.contains(group)) {
+ ((StdPDPGroup) group).publishPolicy(id, name, isRoot, policy);
+ return;
+ }
+ logger.warn("unknown PDP Group: " + group);
+ throw new PAPException("Unknown PDP Group: " + group.getId());
+ }
+
+
+ @Override
+ public void copyPolicy(PDPPolicy policy, OnapPDPGroup group) throws PAPException {
+ //
+ // Currently not used on the PAP side. This is done by ((StdPDPGroup) group).copyPolicyToFile
+ //
+ }
+
+
+ @Override
+ public void removePolicy(PDPPolicy policy, OnapPDPGroup group) throws PAPException {
+ if (group == null) {
+ throw new NullPointerException();
+ }
+ if (group instanceof StdPDPGroup && this.groups.contains(group)) {
+ ((StdPDPGroup) group).removePolicy(policy);
+ return;
+ }
+ logger.warn("unknown PDP Group: " + group);
+ throw new PAPException("Unknown PDP Group: " + group.getId());
+ }
+
+
+ //
+ // HELPER methods
+ //
+
+ private Set<StdPDPGroup> readProperties(Path repository, Properties properties) throws PAPException {
+ Set<StdPDPGroup> pdpGroups = new HashSet<>();
+ //
+ // See if there is a groups property
+ //
+ String groupList = properties.getProperty(PROP_PAP_GROUPS, "");
+ if (groupList == null) {
+ logger.warn("null group list " + PROP_PAP_GROUPS);
+ groupList = "";
+ }
+ if (logger.isDebugEnabled()) {
+ logger.debug("group list: " + groupList);
+ }
+ //
+ // Iterate the groups, converting to a set ensures we have unique groups.
+ //
+ for (String id : Splitter.on(',').trimResults().omitEmptyStrings().split(groupList)) {
+ //
+ // Add our Group Object
+ //
+ StdPDPGroup g = new StdPDPGroup(id.trim(),
+ id.equals(properties.getProperty(PROP_PAP_GROUPS_DEFAULT, PROP_PAP_GROUPS_DEFAULT_NAME)),
+ properties, Paths.get(repository.toString(), id));
+
+ //
+ // Add it in
+ //
+ pdpGroups.add(g);
+ }
+ //
+ // Dump what we got
+ //
+ if (logger.isDebugEnabled()) {
+ logger.debug("PDP Group List: " + pdpGroups.toString());
+ }
+ return pdpGroups;
+ }
+
+ private void saveConfiguration() throws PAPException, IOException {
+ //
+ // Create our properties object
+ //
+ Properties properties = new Properties() {
+ private static final long serialVersionUID = 1L;
+
+ // For Debugging it is helpful for the file to be in a sorted order,
+ // any by returning the keys in the natural Alpha order for strings we get close enough.
+ // TreeSet is sorted, and this just overrides the normal Properties method to get the keys.
+ @Override
+ public synchronized Enumeration<Object> keys() {
+ return Collections.enumeration(new TreeSet<Object>(super.keySet()));
+ }
+ };
+ //
+ // Iterate our groups
+ //
+ 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",
+ 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",
+ pdp.getDescription() == null ? "" : pdp.getDescription());
+ if (pdp instanceof OnapPDP) {
+ properties.setProperty(pdp.getId() + ".jmxport",
+ (((OnapPDP) pdp).getJmxPort() == 0 ? "" : ((OnapPDP) pdp).getJmxPort()).toString());
+ }
+ }
+ String pdpList = "";
+ if (pdps.size() == 1) {
+ pdpList = pdps.get(0);
+ } else if (pdps.size() > 1) {
+ pdpList = Joiner.on(',').skipNulls().join(pdps);
+ }
+ if (logger.isDebugEnabled()) {
+ logger.debug("Group " + group.getId() + " PDPS: " + pdpList);
+ }
+ properties.setProperty(group.getId() + ".pdps", pdpList);
+ }
+ if (ids.isEmpty()) {
+ throw new PAPException("Inconsistency - we have NO groups. We should have at least one.");
+ }
+ String groupList = "";
+ if (ids.size() == 1) {
+ groupList = ids.get(0);
+ } else if (ids.size() > 1) {
+ groupList = Joiner.on(',').skipNulls().join(ids);
+ }
+ logger.info("New Group List: " + groupList);
+
+ properties.setProperty(PROP_PAP_GROUPS, groupList);
+ //
+ // Get the default group
+ //
+ PDPGroup defaultGroup = this.getDefaultGroup();
+ if (defaultGroup == null) {
+ throw new PAPException("Invalid state - no default group.");
+ }
+ properties.setProperty(PROP_PAP_GROUPS_DEFAULT, defaultGroup.getId());
+ //
+ // Now we can save the file
+ //
+ Path file = Paths.get(this.repository.toString(), "xacml.properties");
+ try (OutputStream os = Files.newOutputStream(file)) {
+ properties.store(os, "");
+ }
+ }
+
+ public static void removeGroupProperties(String id, Properties properties) {
+ for (Object key : properties.keySet()) {
+ if (key.toString().startsWith(id + ".")) {
+ properties.remove(key);
+ }
+ }
+ }
+
+ public static void setGroupProperties(PDPGroup group, Properties properties) {
+ //
+ // make sure its in the list of groups
+ //
+ Iterable<String> groups =
+ Splitter.on(',').trimResults().omitEmptyStrings().split(properties.getProperty(PROP_PAP_GROUPS, ""));
+ boolean inList = false;
+ for (String g : groups) {
+ if (g.equals(group.getId())) {
+ inList = true;
+ }
+ }
+ if (!inList) {
+ Set<String> grps = Sets.newHashSet(groups);
+ grps.add(group.getId());
+ String newGroupList;
+ if (grps.size() == 1) {
+ newGroupList = grps.iterator().next();
+ } else if (grps.size() > 1) {
+ newGroupList = Joiner.on(',').skipNulls().join(grps);
+ } else {