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.openecomp.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.openecomp.policy.common.logging.eelf.MessageCodes;
32 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
33 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
34 import org.openecomp.policy.common.logging.flexlogger.Logger;
35 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
36 import org.openecomp.policy.xacml.api.pap.PAPPolicyEngine;
37 import org.openecomp.policy.xacml.std.pap.StdPDPGroup;
38 import org.openecomp.policy.xacml.std.pap.StdPDPPolicy;
40 import com.att.research.xacml.api.pap.PDPPolicy;
42 * Auto Push Policy based on the property file properties.
46 public class AutoPushPolicy {
48 private static final Logger LOGGER = FlexLogger.getLogger(AutoPushPolicy.class);
50 private String filePath = null;
51 private Properties properties;
52 private Long newModified;
53 private Long oldModified;
54 private File propFile;
58 * Constructor Pass in the property file path.
60 public AutoPushPolicy(String file){
62 properties = new Properties();
63 propFile = Paths.get(filePath).toFile();
68 * Checks Policy with all the Groups which has set such Property.
69 * Else returns Empty Set.
71 * @param policyToCreateUpdate
74 public Set<StdPDPGroup> checkGroupsToPush(String policyToCreateUpdate, PAPPolicyEngine papEngine) {
75 Set<StdPDPGroup> changedGroups= new HashSet<StdPDPGroup>();
76 // Check if the file has been modified. then re-load the properties file.
77 newModified = propFile.lastModified();
79 if(newModified!=oldModified){
80 // File has been updated.
83 // Read the File name as its made.
84 String gitPath = PolicyDBDao.getGitPath();
85 String policyId = policyToCreateUpdate.substring(policyToCreateUpdate.indexOf(gitPath)+gitPath.length()+1);
86 String policyName = policyId.substring(policyId.lastIndexOf(File.separator)+1,policyId.lastIndexOf("."));
87 policyName = policyName.substring(0,policyName.lastIndexOf("."));
88 policyId = policyId.replace("/", ".");
89 if(policyId.contains("\\")){
90 policyId = policyId.replace("\\", ".");
92 LOGGER.info("Policy ID : " + policyId);
93 LOGGER.info("Policy Name : " + policyName);
95 for(EcompPDPGroup pdpGroup: papEngine.getEcompPDPGroups()){
96 String groupName = pdpGroup.getName();
97 Boolean typeFlag = false;
98 Boolean scopeFlag = false;
99 if(properties.containsKey(groupName + ".policyType")){
100 String type= properties.getProperty(groupName + ".policyType").replaceAll(" ","");
104 typeFlag = policyName.contains(type);
106 if(properties.containsKey(groupName + ".policyScope")){
107 String scope = properties.getProperty(groupName + ".policyScope").replaceAll(" ", "");
108 if(scope.equals("")){
111 scopeFlag = policyId.contains(scope);
113 if(typeFlag || scopeFlag){
114 StdPDPGroup group = addToGroup(policyId,policyName, policyToCreateUpdate, (StdPDPGroup)pdpGroup);
115 changedGroups.add(group);
118 } catch (Exception e) {
119 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "AutoPushPolicy", "Error while processing the auto push for " + policyToCreateUpdate);
121 return changedGroups;
124 private void readFile(){
126 properties.load(new FileInputStream(propFile));
127 oldModified = propFile.lastModified();
128 } catch (Exception e) {
129 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "AutoPushPolicy", "Error while loading in the auto push properties file.");
133 private StdPDPGroup addToGroup(String policyId, String policyName, String policyToCreateUpdate, StdPDPGroup pdpGroup) throws Exception{
134 // Add to group. Send Notification.
135 StdPDPPolicy policy = new StdPDPPolicy(policyId, true, policyName, null);
136 //Get the current policies from the Group and Add the new one
137 Set<PDPPolicy> currentPoliciesInGroup = pdpGroup.getPolicies();
138 Set<PDPPolicy> policies = new HashSet<PDPPolicy>();
140 policies.add(policy);
142 pdpGroup.copyPolicyToFile(policyId, new FileInputStream(Paths.get(policyToCreateUpdate).toFile()));
143 //If the selected policy is in the group we must remove it because the name is default
144 Iterator<PDPPolicy> policyIterator = policies.iterator();
145 while (policyIterator.hasNext()) {
146 PDPPolicy selPolicy = policyIterator.next();
147 for (PDPPolicy existingPolicy : currentPoliciesInGroup) {
148 if (existingPolicy.getId().equals(selPolicy.getId())) {
149 pdpGroup.removePolicyFromGroup(existingPolicy);
150 LOGGER.debug("Removing policy: " + existingPolicy);
155 if(currentPoliciesInGroup!=null){
156 policies.addAll(currentPoliciesInGroup);
158 pdpGroup.setPolicies(policies);