2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.xacml.std.pap;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.Serializable;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Enumeration;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Properties;
37 import java.util.TreeSet;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.onap.policy.common.logging.eelf.MessageCodes;
42 import org.onap.policy.common.logging.eelf.PolicyLogger;
43 import org.onap.policy.xacml.api.XACMLErrorConstants;
44 import org.onap.policy.xacml.api.pap.OnapPDP;
45 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
46 import org.onap.policy.xacml.std.pap.StdPDPItemSetChangeNotifier.StdItemSetChangeListener;
48 import com.att.research.xacml.api.pap.PAPException;
49 import com.att.research.xacml.api.pap.PDP;
50 //import com.att.research.xacml.api.pap.PDPGroup;
51 import com.att.research.xacml.api.pap.PDPGroupStatus;
52 import com.att.research.xacml.api.pap.PDPGroupStatus.Status;
53 import com.att.research.xacml.api.pap.PDPPIPConfig;
54 import com.att.research.xacml.api.pap.PDPPolicy;
55 import com.att.research.xacml.util.XACMLProperties;
56 import com.fasterxml.jackson.annotation.JsonIgnore;
57 import com.google.common.base.Joiner;
58 import com.google.common.base.Splitter;
59 import com.google.common.io.ByteStreams;
61 public class StdPDPGroup extends StdPDPItemSetChangeNotifier implements OnapPDPGroup, StdItemSetChangeListener, Comparable<Object>, Serializable {
62 private static final long serialVersionUID = 1L;
63 private static Log logger = LogFactory.getLog(StdPDPGroup.class);
67 private boolean isDefault = false;
71 private String description;
73 private transient StdPDPGroupStatus status = new StdPDPGroupStatus(Status.UNKNOWN);
75 private transient Set<OnapPDP> pdps = new HashSet<>();
77 private transient Set<PDPPolicy> policies = new HashSet<>();
79 private transient Set<PDPPolicy> selectedPolicies = new HashSet<>();
81 private transient Set<PDPPIPConfig> pipConfigs = new HashSet<>();
83 private String operation;
86 private transient Path directory;
89 private Integer jmxport;
92 public StdPDPGroup(String id, Path directory) {
94 this.directory = directory;
97 public StdPDPGroup(String id, boolean isDefault, Path directory) {
99 this.isDefault = isDefault;
102 public StdPDPGroup(String id, boolean isDefault, String name, String description, Path directory) {
103 this(id, isDefault, directory);
105 // force all policies to have a name
109 this.description = description;
112 public StdPDPGroup(String id, String name, String description, Path directory) {
113 this(id, false, name, description, directory);
117 public StdPDPGroup(String id, boolean isDefault, Properties properties, Path directory) throws PAPException {
118 this(id, isDefault, directory);
119 this.initialize(properties, directory);
123 private void initialize(Properties properties, Path directory) throws PAPException {
124 if (this.id == null || this.id.length() == 0) {
125 logger.warn("Cannot initialize with a null or zero length id");
129 // Pull the group's properties
131 for (Object key : properties.keySet()) {
132 if (key.toString().startsWith(this.id + ".")) {
133 if (key.toString().endsWith(".name")) {
134 this.name = properties.getProperty(key.toString());
135 } else if (key.toString().endsWith(".description")) {
136 this.description = properties.getProperty(key.toString());
137 } else if (key.toString().endsWith(".pdps")) {
138 String pdpList = properties.getProperty(key.toString());
139 if (pdpList != null && pdpList.length() > 0) {
140 for (String id : Splitter.on(',').omitEmptyStrings().trimResults().split(pdpList)) {
141 StdPDP pdp = new StdPDP(id, properties);
142 pdp.addItemSetChangeListener(this);
148 // force all policies to have a name
149 if (this.name == null) {
154 // Validate our directory
156 if (Files.notExists(directory)) {
157 logger.warn("Group directory does NOT exist: " + directory.toString());
159 Files.createDirectory(directory);
160 this.status.addLoadWarning("Group directory does NOT exist");
161 } catch (IOException e) {
162 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdPDPGroup", "Group directory does NOT exist");
163 this.status.addLoadError("Group directory does NOT exist");
164 this.status.setStatus(PDPGroupStatus.Status.LOAD_ERRORS);
170 this.loadPolicies(Paths.get(directory.toString(), "xacml.policy.properties"));
174 this.loadPIPConfig(Paths.get(directory.toString(), "xacml.pip.properties"));
177 public void loadPolicies(Path file) throws PAPException {
179 // Read the Groups Policies
181 Properties policyProperties = new Properties();
182 if ( ! file.toFile().exists()) {
183 // need to create the properties file with default values
184 policyProperties.setProperty(XACMLProperties.PROP_ROOTPOLICIES, "");
185 policyProperties.setProperty(XACMLProperties.PROP_REFERENCEDPOLICIES, "");
186 // save properties to file
187 try (OutputStream os = Files.newOutputStream(file)) {
188 policyProperties.store(os, "");
189 } catch (Exception e) {
190 throw new PAPException("Failed to create new default policy properties file '" + file +"'", e);
193 // load previously existing file
196 // Load the properties
198 try (InputStream is = Files.newInputStream(file)) {
199 policyProperties.load(is);
202 // Parse the policies
204 this.readPolicyProperties(directory, policyProperties);
205 } catch (IOException e) {
206 logger.warn("Failed to load group policy properties file: " + file, e);
207 this.status.addLoadError("Not policy properties defined");
208 this.status.setStatus(Status.LOAD_ERRORS);
209 throw new PAPException("Failed to load group policy properties file: " + file);
214 public void loadPIPConfig(Path file) throws PAPException {
216 // Read the Groups' PIP configuration
218 Properties pipProperties = new Properties();
219 if ( ! file.toFile().exists()) {
220 // need to create the properties file with no values
221 pipProperties = setPIPProperties(pipProperties);
222 // save properties to file
224 try (OutputStream os = Files.newOutputStream(file)) {
225 pipProperties.store(os, "");
227 } catch (Exception e) {
228 throw new PAPException("Failed to create new default pip properties file '" + file +"'", e);
230 //Even if we create a new pip file, we still need to parse and load the properties
232 this.readPIPProperties(directory, pipProperties);
234 throw new PAPException("Failed to load the new pip properties file", e);
239 // Load the properties
241 try (InputStream is = Files.newInputStream(file)) {
242 pipProperties.load(is);
244 // For all old PIP config's modify to the new PIP Configuration.
245 // If PIP is empty add the new values and save it.
246 if(pipProperties.get(XACMLProperties.PROP_PIP_ENGINES).toString().trim().equals("")){
247 pipProperties = setPIPProperties(pipProperties);
248 try (OutputStream os = Files.newOutputStream(file)) {
249 pipProperties.store(os, "");
255 this.readPIPProperties(directory, pipProperties);
256 } catch (IOException e) {
257 logger.warn("Failed to open group PIP Config properties file: " + file, e);
258 this.status.addLoadError("Not PIP config properties defined");
259 this.status.setStatus(Status.LOAD_ERRORS);
260 throw new PAPException("Failed to load group policy properties file: " + file);
266 public void resetStatus() {
268 // // If we are updating, don't allow reset
270 // if (this.status.getStatus() == Status.UPDATING_CONFIGURATION) {
271 // logger.warn("We are updating, chill.");
275 // // Load errors take precedence
277 // if (this.status.getStatus() == Status.LOAD_ERRORS) {
278 // logger.warn("We had load errors.");
282 // Reset our status object
286 // Determine our status
288 for (PDP pdp : this.pdps) {
289 switch (pdp.getStatus().getStatus()) {
291 this.status.addOutOfSynchPDP(pdp);
293 case LAST_UPDATE_FAILED:
294 this.status.addLastUpdateFailedPDP(pdp);
297 this.status.addFailedPDP(pdp);
299 case UPDATING_CONFIGURATION:
300 this.status.addUpdatingPDP(pdp);
303 this.status.addInSynchPDP(pdp);
309 this.status.addUnknownPDP(pdp);
314 // priority is worst-cast to best case
315 if (this.status.getUnknownPDPs().size() > 0) {
316 this.status.setStatus(Status.UNKNOWN);
317 } else if (this.status.getFailedPDPs().size() > 0 || this.status.getLastUpdateFailedPDPs().size() > 0) {
318 this.status.setStatus(Status.LOAD_ERRORS);
319 } else if (this.status.getOutOfSynchPDPs().size() > 0) {
320 this.status.setStatus(Status.OUT_OF_SYNCH);
321 } else if (this.status.getUpdatingPDPs().size() > 0) {
322 this.status.setStatus(Status.UPDATING_CONFIGURATION);
324 this.status.setStatus(Status.OK);
329 public String getId() {
333 public void setId(String id) {
338 public boolean isDefaultGroup() {
339 return this.isDefault;
342 public void setDefaultGroup(boolean isDefault) {
343 this.isDefault = isDefault;
345 // Cannot fire this because 2 operations have
346 // to occur: 1) old default=false (don't want to fire) and
347 // then 2) new default=true (yes fire - but we'll have to do that
349 //this.firePDPGroupChanged(this);
353 public String getName() {
358 public void setName(String groupName) {
359 this.name = groupName;
360 this.firePDPGroupChanged(this);
364 public String getDescription() {
365 return this.description;
369 public void setDescription(String groupDescription) {
370 this.description = groupDescription;
371 this.firePDPGroupChanged(this);
374 public Path getDirectory() {
375 return this.directory;
378 public void setDirectory(Path groupDirectory) {
379 this.directory = groupDirectory;
380 // this is used only for transmission on the RESTful interface, so no need to fire group changed?
384 public PDPGroupStatus getStatus(){
389 public Set<PDPPolicy> getSelectedPolicies() {
390 return this.selectedPolicies;
394 public String getOperation() {
395 return this.operation;
399 public Set<PDP> getPdps() {
400 return Collections.unmodifiableSet(pdps);
403 public void setOnapPdps(Set<OnapPDP> pdps) {
407 public Set<OnapPDP> getOnapPdps(){
408 return Collections.unmodifiableSet(pdps);
411 public boolean addPDP(OnapPDP pdp) {
412 return this.pdps.add(pdp);
415 public boolean removePDP(PDP pdp) {
416 return this.pdps.remove(pdp);
420 public Set<PDPPolicy> getPolicies() {
421 return Collections.unmodifiableSet(this.policies);
425 public PDPPolicy getPolicy(String id) {
426 for (PDPPolicy policy : this.policies) {
427 if (policy.getId().equals(id)) {
434 public Properties getPolicyProperties()
436 Properties properties = new Properties(){
437 private static final long serialVersionUID = 1L;
438 // For Debugging it is helpful for the file to be in a sorted order,
439 // any by returning the keys in the natural Alpha order for strings we get close enough.
440 // TreeSet is sorted, and this just overrides the normal Properties method to get the keys.
442 public synchronized Enumeration<Object> keys() {
443 return Collections.enumeration(new TreeSet<Object>(super.keySet()));
446 List<String> roots = new ArrayList<String>();
447 List<String> refs = new ArrayList<String>();
449 for (PDPPolicy policy : this.policies) {
450 // for all policies need to tell PDP the "name", which is the base name for the file id
451 if (policy.getName() != null) {
452 properties.setProperty(policy.getId() + ".name", policy.getName());
454 // put the policy on the correct list
455 if (policy.isRoot()) {
456 roots.add(policy.getId());
458 refs.add(policy.getId());
462 properties.setProperty(XACMLProperties.PROP_ROOTPOLICIES, Joiner.on(',').join(roots));
463 properties.setProperty(XACMLProperties.PROP_REFERENCEDPOLICIES, Joiner.on(',').join(refs));
468 public PDPPolicy publishPolicy(String id, String name, boolean isRoot, InputStream policy) throws PAPException {
470 // Does it exist already?
472 if (this.getPolicy(id) != null) {
473 throw new PAPException("Policy with id " + id + " already exists - unpublish it first.");
475 Path tempFile = null;
478 // Copy the policy over
480 tempFile = Files.createFile(Paths.get(this.directory.toAbsolutePath().toString(), id));
482 try (OutputStream os = Files.newOutputStream(tempFile)) {
483 num = ByteStreams.copy(policy, os);
485 logger.info("Copied " + num + " bytes for policy " + name);
487 StdPDPPolicy tempRootPolicy = new StdPDPPolicy(id, isRoot, name, tempFile.toUri());
488 if (tempRootPolicy.isValid() == false) {
490 Files.delete(tempFile);
491 } catch(Exception ee) {
492 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, ee, "StdPDPGroup", "Policy was invalid, could NOT delete it.");
494 throw new PAPException("Policy is invalid");
499 this.policies.add(tempRootPolicy);
503 this.firePDPGroupChanged(this);
505 // Return our new object.
507 return tempRootPolicy;
508 } catch (IOException e) {
509 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "StdPDPGroup", "Failed to publishPolicy");
515 * Copy one policy file into the Group's directory but do not change the configuration.
516 * This is one part of a multi-step process of publishing policies.
517 * There may be multiple changes in the group (adding multiple policies, deleting policies, changine root<->referenced)
518 * that must be done all at once, so we just copy the file in preparation for a later "update whole group" operation.
525 * @throws PAPException
527 public void copyPolicyToFile(String id, InputStream policy) throws PAPException {
530 // Copy the policy over
533 Path policyFilePath = Paths.get(this.directory.toAbsolutePath().toString(), id);
536 if (Files.exists(policyFilePath)) {
537 policyFile = policyFilePath;
539 policyFile = Files.createFile(policyFilePath);
542 try (OutputStream os = Files.newOutputStream(policyFile)) {
543 num = ByteStreams.copy(policy, os);
546 logger.info("Copied " + num + " bytes for policy " + name);
548 for (PDPPolicy p : policies) {
549 if (p.getId().equals(id)) {
550 // we just re-copied/refreshed/updated the policy file for a policy that already exists in this group
551 logger.info("Policy '" + id + "' already exists in group '" + getId() + "'");
556 // policy is new to this group
557 StdPDPPolicy tempRootPolicy = new StdPDPPolicy(id, true, name, policyFile.toUri());
558 if (tempRootPolicy.isValid() == false) {
560 Files.delete(policyFile);
561 } catch(Exception ee) {
562 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, ee, "StdPDPGroup", "Policy was invalid, could NOT delete it.");
564 throw new PAPException("Policy is invalid");
569 this.policies.add(tempRootPolicy);
573 this.firePDPGroupChanged(this);
574 } catch (IOException e) {
575 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdPDPGroup", "Failed to copyPolicyToFile");
576 throw new PAPException("Failed to copy policy to file: " + e);
582 * Policy Engine API Copy one policy file into the Group's directory but do not change the configuration.
588 * @throws PAPException
590 public void copyPolicyToFile(String id, String name, InputStream policy) throws PAPException {
593 // Copy the policy over
596 Path policyFilePath = Paths.get(this.directory.toAbsolutePath().toString(), id);
599 if (Files.exists(policyFilePath)) {
600 policyFile = policyFilePath;
602 policyFile = Files.createFile(policyFilePath);
605 try (OutputStream os = Files.newOutputStream(policyFile)) {
606 num = ByteStreams.copy(policy, os);
609 logger.info("Copied " + num + " bytes for policy " + name);
610 for (PDPPolicy p : policies) {
611 if (p.getId().equals(id)) {
612 // we just re-copied/refreshed/updated the policy file for a policy that already exists in this group
613 logger.info("Policy '" + id + "' already exists in group '" + getId() + "'");
618 // policy is new to this group
619 StdPDPPolicy tempRootPolicy = new StdPDPPolicy(id, true, name, policyFile.toUri());
620 if (tempRootPolicy.isValid() == false) {
622 Files.delete(policyFile);
623 } catch(Exception ee) {
624 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, ee, "StdPDPGroup", "Policy was invalid, could NOT delete it.");
626 throw new PAPException("Policy is invalid");
631 this.policies.add(tempRootPolicy);
635 this.firePDPGroupChanged(this);
637 } catch (IOException e) {
638 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdPDPGroup", "Failed to copyPolicyToFile");
639 throw new PAPException("Failed to copy policy to file: " + e);
644 public boolean removePolicyFromGroup(PDPPolicy policy) {
645 PolicyLogger.info("policy: " + policy.getId());
646 PolicyLogger.info("Policy ID:" + policy.getPolicyId());
647 PolicyLogger.info("Policy Version: " + policy.getVersion());
648 PolicyLogger.info("StdPDPPolicy Class cast: " + this.getPolicy(policy.getId()).toString());
649 StdPDPPolicy currentPolicy = (StdPDPPolicy) this.getPolicy(policy.getId());
650 if (currentPolicy == null) {
651 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Policy " + policy.getId() + " does not exist.");
656 // Remove it from our list
658 this.policies.remove(currentPolicy);
662 this.firePDPGroupChanged(this);
664 } catch (Exception e) {
665 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdPDPGroup", "Failed to delete policy");
670 public boolean removePolicy(PDPPolicy policy) {
671 PDPPolicy currentPolicy = this.getPolicy(policy.getId());
672 if (currentPolicy == null) {
673 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Policy " + policy.getId() + " does not exist.");
680 Files.delete(Paths.get(currentPolicy.getLocation()));
682 // Remove it from our list
684 this.policies.remove(currentPolicy);
688 this.firePDPGroupChanged(this);
690 } catch (Exception e) {
691 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdPDPGroup", "Failed to delete policy " + policy);
697 public Set<PDPPIPConfig> getPipConfigs() {
698 return Collections.unmodifiableSet(this.pipConfigs);
702 public PDPPIPConfig getPipConfig(String id) {
703 for (PDPPIPConfig config : this.pipConfigs) {
704 if (config.getId().equals(id)) {
711 public void setPipConfigs(Set<PDPPIPConfig> pipConfigs) {
712 this.pipConfigs = pipConfigs;
713 this.firePDPGroupChanged(this);
716 public void removeAllPIPConfigs() {
717 this.pipConfigs.clear();
718 this.firePDPGroupChanged(this);
721 public Properties getPipConfigProperties() {
722 Properties properties = new Properties();
723 List<String> configs = new ArrayList<String>();
725 for (PDPPIPConfig config : this.pipConfigs) {
726 configs.add(config.getId());
727 properties.putAll(config.getConfiguration());
730 properties.setProperty(XACMLProperties.PROP_PIP_ENGINES, Joiner.on(',').join(configs));
736 public void repair() {
738 // Reset the status object
742 // Validate our directory
744 boolean fire = false;
745 if (Files.notExists(directory)) {
746 logger.warn("Group directory does NOT exist: " + directory.toString());
748 Files.createDirectory(directory);
750 this.status.addLoadWarning("Created missing group directory");
751 } catch (IOException e) {
752 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdPDPGroup", "Failed to create missing Group directory.");
753 this.status.addLoadError("Failed to create missing Group directory.");
754 this.status.setStatus(PDPGroupStatus.Status.LOAD_ERRORS);
758 // Validate our PIP config file
760 Path pipPropertiesFile = Paths.get(directory.toString(), "xacml.pip.properties");
761 if (Files.notExists(pipPropertiesFile)) {
763 Files.createFile(pipPropertiesFile);
765 this.status.addLoadWarning("Created missing PIP properties file");
766 } catch (IOException e) {
767 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdPDPGroup", "Failed to create missing PIP properties file");
768 this.status.addLoadError("Failed to create missing PIP properties file");
769 this.status.setStatus(PDPGroupStatus.Status.LOAD_ERRORS);
773 // Valid our policy properties file
775 Path policyPropertiesFile = Paths.get(directory.toString(), "xacml.policy.properties");
776 if (Files.notExists(policyPropertiesFile)) {
778 Files.createFile(policyPropertiesFile);
780 this.status.addLoadWarning("Created missing Policy properties file");
781 } catch (IOException e) {
782 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdPDPGroup", "Failed to create missing Policy properties file");
783 this.status.addLoadError("Failed to create missing Policy properties file");
784 this.status.setStatus(PDPGroupStatus.Status.LOAD_ERRORS);
793 private void readPolicyProperties(Path directory, Properties properties) {
795 // There are 2 property values that hold policies, root and referenced
797 String[] lists = new String[2];
798 lists[0] = properties.getProperty(XACMLProperties.PROP_ROOTPOLICIES);
799 lists[1] = properties.getProperty(XACMLProperties.PROP_REFERENCEDPOLICIES);
801 // Iterate each policy list
803 boolean isRoot = true;
804 for (String list : lists) {
806 // Was there actually a property?
808 if (list == null || list.length() == 0) {
815 Iterable<String> policyList = Splitter.on(',').trimResults().omitEmptyStrings().split(list);
817 // Was there actually a list
819 if (policyList == null) {
823 for (String id : policyList) {
825 // Construct the policy filename
827 Path policyPath = Paths.get(directory.toString(), id );
829 // Create the Policy Object
833 policy = new StdPDPPolicy(id, isRoot, policyPath.toUri(), properties);
834 } catch (IOException e) {
835 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdPDPGroup", "Failed to create policy object");
841 if (policy != null && policy.isValid()) {
842 this.policies.add(policy);
843 this.status.addLoadedPolicy(policy);
845 this.status.addFailedPolicy(policy);
846 this.status.setStatus(Status.LOAD_ERRORS);
848 // force all policies to have a name
849 if (policy!=null && policy.getName() == null) {
850 policy.setName(policy.getId());
857 private void readPIPProperties(Path directory, Properties properties) {
858 String list = properties.getProperty(XACMLProperties.PROP_PIP_ENGINES);
859 if (list == null || list.length() == 0) {
862 for (String id : list.split("[,]")) {
863 StdPDPPIPConfig config = new StdPDPPIPConfig(id, properties);
864 if (config.isConfigured()) {
865 this.pipConfigs.add(config);
866 this.status.addLoadedPipConfig(config);
868 this.status.addFailedPipConfig(config);
869 this.status.setStatus(Status.LOAD_ERRORS);
875 public int hashCode() {
876 final int prime = 31;
878 result = prime * result + ((id == null) ? 0 : id.hashCode());
883 public boolean equals(Object obj) {
888 if (getClass() != obj.getClass())
890 StdPDPGroup other = (StdPDPGroup) obj;
892 if (other.id != null)
894 } else if (!id.equals(other.id))
900 public String toString() {
901 return "StdPDPGroup [id=" + id + ", isDefault=" + isDefault + ", name="
902 + name + ", description=" + description + ", status=" + status
903 + ", pdps=" + pdps + ", policies=" + policies + ", pipConfigs="
904 + pipConfigs + ", directory=" + directory + ",selectedPolicies="
905 + selectedPolicies + ",operation=" + operation + "]";
909 public void changed() {
911 // save the (changed) properties
913 saveGroupConfiguration();
914 } catch (PAPException | IOException e) {
915 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "StdPDPGroup", "Unable to save group configuration change");
916 // don't notify other things of change if we cannot save it???
920 this.firePDPGroupChanged(this);
925 public void groupChanged(OnapPDPGroup group) {
930 public void pdpChanged(OnapPDP pdp) {
932 // If one of the group's PDP's changed, then the group changed
939 // Methods needed for JSON deserialization
941 public StdPDPGroup() {
945 public StdPDPGroup(OnapPDPGroup group) {
946 this.id = group.getId();
947 this.name = group.getName();
948 this.description = group.getDescription();
949 this.isDefault = group.isDefaultGroup();
950 this.pdps = group.getOnapPdps();
951 this.policies = group.getPolicies();
952 this.pipConfigs = group.getPipConfigs();
955 public boolean isDefault() {
958 public void setDefault(boolean isDefault) {
959 this.isDefault = isDefault;
961 public void setStatus(PDPGroupStatus status) {
962 this.status = new StdPDPGroupStatus(status);
964 public void setPolicies(Set<PDPPolicy> policies) {
965 this.policies = policies;
967 public void setSelectedPolicies(Set<PDPPolicy> selectedPolicies) {
968 this.selectedPolicies = selectedPolicies;
970 public void setOperation(String operation) {
971 this.operation = operation;
974 public void saveGroupConfiguration() throws PAPException, IOException {
976 // First save the Policy properties
978 // save the lists of policies
979 Properties policyProperties = this.getPolicyProperties();
981 // save info about each policy
982 for (PDPPolicy policy : this.policies){
983 policyProperties.put(policy.getId() + ".name", policy.getName());
987 // Now we can save the file
989 Path file = Paths.get(this.directory.toString(), "xacml.policy.properties");
990 try (OutputStream os = Files.newOutputStream(file)) {
991 policyProperties.store(os, "");
992 } catch (Exception e) {
993 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "STdPDPGroup", "Group Policies Config save failed");
994 throw new PAPException("Failed to save policy properties file '" + file +"'");
998 // Now save the PIP Config properties
999 Properties pipProperties = this.getPipConfigProperties();
1002 // Now we can save the file
1004 file = Paths.get(this.directory.toString(), "xacml.pip.properties");
1005 try (OutputStream os = Files.newOutputStream(file)) {
1006 pipProperties.store(os, "");
1007 } catch (Exception e) {
1008 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdPDPGroup", "Group PIP Config save failed");
1009 throw new PAPException("Failed to save pip properties file '" + file +"'");
1014 // Comparable Interface
1017 public int compareTo(Object arg0) {
1021 if ( ! (arg0 instanceof StdPDPGroup)) {
1024 if (((StdPDPGroup)arg0).name == null) {
1031 return name.compareTo(((StdPDPGroup)arg0).name);
1034 //Adding Default PIP engine(s) while Loading initially. We don't want
1035 // Programmer intervention with the PIP engines.
1036 private Properties setPIPProperties(Properties props){
1037 props.setProperty("AAF.name", "AAFEngine");
1038 props.setProperty("AAF.description", "AAFEngine to communicate with AAF to take decisions");
1039 props.setProperty("AAF.classname","org.onap.policy.xacml.std.pip.engines.aaf.AAFEngine");
1040 props.setProperty(XACMLProperties.PROP_PIP_ENGINES, "AAF");
1041 // read from PIP properties file.
1042 Path file = Paths.get(StdEngine.pipPropertyFile);
1043 if (!Files.notExists(file)) {
1045 Properties prop = new Properties();
1047 in = new FileInputStream(file.toFile());
1049 } catch (IOException e) {
1050 PolicyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "can not load the pip properties from file" +e);