[POLICY-122] Policy GUI Fixes
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / components / AutoPushPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.pap.xacml.rest.components;
22
23 import java.io.File;
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;
29 import java.util.Set;
30
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.std.pap.StdPDPGroup;
36 import org.openecomp.policy.xacml.std.pap.StdPDPPolicy;
37
38 import com.att.research.xacml.api.pap.PDPPolicy;
39 /**
40  * Auto Push Policy based on the property file properties. 
41  * 
42  * @version 0.1
43  */
44 public class AutoPushPolicy {
45         
46         private static final Logger LOGGER = FlexLogger.getLogger(AutoPushPolicy.class);
47         
48         private String filePath = null;
49         private Properties properties;
50         private Long newModified;
51         private Long oldModified;
52         private File propFile;
53         
54         
55         /**
56          * Constructor Pass in the property file path. 
57          */
58         public AutoPushPolicy(String file){
59                 filePath = file;
60                 properties = new Properties();
61                 propFile = Paths.get(filePath).toFile();
62                 readFile();
63         }
64         
65         private void readFile(){
66                 try {
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.");
71                 }
72         }
73         
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<>();
80                 policies.add(policy);
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);
90                                         break;
91                                 }
92                         }
93                 }
94                 if(currentPoliciesInGroup!=null){
95             policies.addAll(currentPoliciesInGroup);
96         }
97                 pdpGroup.setPolicies(policies);
98                 return pdpGroup;
99         }
100 }