Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / conformance / ConformanceRepository.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP
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.pdp.test.conformance;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Properties;
31
32 import com.att.research.xacml.util.StringUtils;
33 import com.att.research.xacml.util.XACMLProperties;
34 import com.att.research.xacmlatt.pdp.std.StdPolicyFinderFactory;
35
36 /**
37  * ConformanceRepository represents one or more policies for a single policy test, which will include one or more root policies, and
38  * zero or more referenced policies.
39  * 
40  * @version $Revision$
41  */
42 public class ConformanceRepository {
43         private List<File> rootPolicies                 = new ArrayList<>();
44         private List<File> referencedPolicies   = new ArrayList<>();
45         
46         private void setXACMLProperty(String propertyName, List<File> listFiles) {
47                 Iterator<File> iterFiles                        = listFiles.iterator();
48                 StringBuilder stringBuilderIdList       = new StringBuilder();
49                 while (iterFiles.hasNext()) {
50                         File file       = iterFiles.next();
51                         if (stringBuilderIdList.length() > 0) {
52                                 stringBuilderIdList.append(',');
53                         }
54                         stringBuilderIdList.append(file.getName());
55                         
56                         XACMLProperties.setProperty(file.getName() + StdPolicyFinderFactory.PROP_FILE, file.getAbsolutePath());
57                 }
58                 XACMLProperties.setProperty(propertyName, stringBuilderIdList.toString());
59         }
60         
61         public ConformanceRepository() {
62         }
63         
64         public void setXACMLProperties() {
65                 if (this.rootPolicies.size() > 0) {
66                         this.setXACMLProperty(XACMLProperties.PROP_ROOTPOLICIES, this.rootPolicies);
67                 }
68                 if (this.referencedPolicies.size() > 0) {
69                         this.setXACMLProperty(XACMLProperties.PROP_REFERENCEDPOLICIES, this.referencedPolicies);
70                 }
71         }
72         
73         private void loadProperty(File fileDir, Properties properties, String propertyName, List<File> listFiles) {
74                 String fileNameList     = properties.getProperty(propertyName);
75                 if (fileNameList != null) {
76                         String[] fileNameArray  = fileNameList.split("[,]",0);
77                         if (fileNameArray != null && fileNameArray.length > 0) {
78                                 for (String fileName : fileNameArray) {
79                                         File file       = new File(fileDir, fileName);
80                                         if (file.exists() && file.canRead()) {
81                                                 listFiles.add(file);
82                                         }
83                                 }
84                         }
85                 }
86         }
87         
88         public void load(File fileRepository) throws IOException {
89                 Properties propertiesRepository = new Properties();
90                 try (InputStream is = new FileInputStream(fileRepository)) {
91                         propertiesRepository.load(is);
92                 }
93                 this.loadProperty(fileRepository.getParentFile(), propertiesRepository, XACMLProperties.PROP_ROOTPOLICIES, this.rootPolicies);
94                 this.loadProperty(fileRepository.getParentFile(), propertiesRepository, XACMLProperties.PROP_REFERENCEDPOLICIES, this.referencedPolicies);
95         }
96         
97         public void addRootPolicy(File filePolicy) {
98                 this.rootPolicies.add(filePolicy);
99         }
100         
101         public boolean hasRootPolicy() {
102                 return (this.rootPolicies.size() > 0);
103         }
104         
105         @Override
106         public String toString() {
107                 StringBuilder stringBuilder     = new StringBuilder("{");
108                 boolean needComma                       = false;
109                 
110                 if (this.rootPolicies != null && this.rootPolicies.size() > 0) {
111                         stringBuilder.append("rootPolicies=");
112                         stringBuilder.append(StringUtils.toString(this.rootPolicies.iterator()));
113                         needComma       = true;
114                 }
115                 if (this.referencedPolicies != null && this.referencedPolicies.size() > 0) {
116                         if (needComma) {
117                                 stringBuilder.append(',');
118                         }
119                         stringBuilder.append("referencedPolicies=");
120                         stringBuilder.append(StringUtils.toString(this.referencedPolicies.iterator()));
121                         needComma       = true;
122                 }
123                 stringBuilder.append('}');
124                 return stringBuilder.toString();
125         }
126         
127 }