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=========================================================
21 package org.onap.policy.pap.xacml.rest.components;
24 import java.io.FileInputStream;
25 import java.nio.file.Paths;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.Properties;
31 import org.onap.policy.common.logging.eelf.MessageCodes;
32 import org.onap.policy.common.logging.eelf.PolicyLogger;
33 import org.onap.policy.common.logging.flexlogger.FlexLogger;
34 import org.onap.policy.common.logging.flexlogger.Logger;
35 import org.onap.policy.xacml.std.pap.StdPDPGroup;
36 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
38 import com.att.research.xacml.api.pap.PDPPolicy;
40 * Auto Push Policy based on the property file properties.
44 public class AutoPushPolicy {
46 private static final Logger LOGGER = FlexLogger.getLogger(AutoPushPolicy.class);
48 private String filePath = null;
49 private Properties properties;
50 private Long newModified;
51 private Long oldModified;
52 private File propFile;
56 * Constructor Pass in the property file path.
58 public AutoPushPolicy(String file){
60 properties = new Properties();
61 propFile = Paths.get(filePath).toFile();
65 private void readFile(){
67 properties.load(new FileInputStream(propFile));
68 oldModified = propFile.lastModified();
69 } catch (Exception e) {
70 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "AutoPushPolicy", "Error while loading in the auto push properties file.");
74 private StdPDPGroup addToGroup(String policyId, String policyName, String policyToCreateUpdate, StdPDPGroup pdpGroup) throws Exception{
75 // Add to group. Send Notification.
76 StdPDPPolicy policy = new StdPDPPolicy(policyId, true, policyName, null);
77 //Get the current policies from the Group and Add the new one
78 Set<PDPPolicy> currentPoliciesInGroup = pdpGroup.getPolicies();
79 Set<PDPPolicy> policies = new HashSet<>();
81 pdpGroup.copyPolicyToFile(policyId, new FileInputStream(Paths.get(policyToCreateUpdate).toFile()));
82 //If the selected policy is in the group we must remove it because the name is default
83 Iterator<PDPPolicy> policyIterator = policies.iterator();
84 while (policyIterator.hasNext()) {
85 PDPPolicy selPolicy = policyIterator.next();
86 for (PDPPolicy existingPolicy : currentPoliciesInGroup) {
87 if (existingPolicy.getId().equals(selPolicy.getId())) {
88 pdpGroup.removePolicyFromGroup(existingPolicy);
89 LOGGER.debug("Removing policy: " + existingPolicy);
94 if(currentPoliciesInGroup!=null){
95 policies.addAll(currentPoliciesInGroup);
97 pdpGroup.setPolicies(policies);