aboutsummaryrefslogtreecommitdiffstats
path: root/ECOMP-XACML/src/main/java/org/openecomp/policy/xacml/std/pap/StdPDPPolicy.java
blob: 06c2498f622af38b13830f937ef694a3df58ed92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*-
 * ============LICENSE_START=======================================================
 * ECOMP-XACML
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.policy.xacml.std.pap;

import org.openecomp.policy.common.logging.eelf.PolicyLogger;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Properties;

import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openecomp.policy.xacml.util.XACMLPolicyScanner;

import com.att.research.xacml.api.pap.PAPException;
import com.att.research.xacml.api.pap.PDPPolicy;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;


public class StdPDPPolicy implements PDPPolicy, Serializable {
	private static final long serialVersionUID = 1L;
	private static Log	logger	= LogFactory.getLog(StdPDPPolicy.class);
	
	private String id = null;
	
	private String name = null;

	private String policyId = null;
	
	private String description = null;
	
	private int[]	version = null;

	private boolean isRoot = false;
	
	private boolean isValid = false;
	
	private URI location = null;
	

	public StdPDPPolicy(String id, boolean isRoot) {
		this.id = id;
		this.isRoot = isRoot;
	}

	public StdPDPPolicy(String id, boolean isRoot, String name) {
		this(id, isRoot);
		this.name = name;
	}

	
	public StdPDPPolicy(String id, boolean isRoot, String name, URI location) throws IOException {
		this(id, isRoot);
		this.name = name;
		this.location = location;
		
		//
		// Read the policy data
		//		
		String theID = this.readPolicyData();

		if (this.id == null) {
			logger.debug("id is null so we are calling readPolicyData() to get the policyID");
			this.id = theID;
		}
		
		logger.debug("The final outcome of the constructor returned the following:  id = " + id + 
						", location = " + location + ", name = " + name);
		
	}
	
	public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isValid, String policyId, 
							String description, String version) throws IOException {
		this(id, isRoot);
		this.name = name;
		this.location = location;
		this.policyId = policyId;
		this.description = description;
		this.version = versionStringToArray(version);
		this.isValid = isValid;
		
		logger.debug("The final outcome of the constructor returned the following:  id = " + id + 
						", location = " + location + ", name = " + name + ", policyId = " + policyId +
							", description = " + description + ", Version = " + version);
		
	}
	
	public StdPDPPolicy(String id, boolean isRoot, String name, URI location, boolean isFromAPI) throws IOException {
		this(id, isRoot);
		this.name = name;
		this.location = location;
		this.isValid = isFromAPI;
		
		logger.debug("The final outcome of the constructor returned the following:  id = " + id + 
						", location = " + location + ", name = " + name);
		
	}

	public StdPDPPolicy(String id, boolean isRoot, URI location, Properties properties) throws IOException {
		this(id, isRoot);
		this.location = location;
		//
		// Read the policy data
		//
		this.readPolicyData();
		//
		// See if there's a name
		//
		for (Object key : properties.keySet()) {
			if (key.toString().equals(id + ".name")) {
				this.name = properties.getProperty(key.toString());
				break;
			}
		}
	}
	
	
	private String readPolicyData() throws IOException {
		//
		// Extract XACML policy information
		//
		URL url = this.location.toURL();
		Object rootElement = XACMLPolicyScanner.readPolicy(url.openStream());
		if (rootElement == null ||
			(
				! (rootElement instanceof PolicySetType) &&
				! (rootElement instanceof PolicyType)
			)	) {
			logger.warn("No root policy element in URI: " + this.location.toString() + " : " + rootElement);
			this.isValid = false;
		} else {
			this.version = versionStringToArray(XACMLPolicyScanner.getVersion(rootElement));
			if (rootElement instanceof PolicySetType) {
				this.policyId = ((PolicySetType)rootElement).getPolicySetId();
				this.description = ((PolicySetType)rootElement).getDescription();
				this.isValid = true;
				this.version = versionStringToArray(((PolicySetType)rootElement).getVersion());
			} else if (rootElement instanceof PolicyType) {
				this.policyId = ((PolicyType)rootElement).getPolicyId();
				this.description = ((PolicyType)rootElement).getDescription();
				this.version = versionStringToArray(((PolicyType)rootElement).getVersion());
				this.isValid = true;
			} else {
				//TODO:EELF Cleanup - Remove logger
				//logger.error("Unknown root element: " + rootElement.getClass().getCanonicalName());
				PolicyLogger.error("Unknown root element: " + rootElement.getClass().getCanonicalName());
			}
		}
		if (this.policyId != null) {
			ArrayList<String> foo = Lists.newArrayList(Splitter.on(':').split(this.policyId));
			if (foo.isEmpty() == false) {
				return foo.get(foo.size() - 1);
			}
		}
		return null;
	}
	
	@Override
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String getName() {
		return this.name;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getPolicyId() {
		return this.policyId;
	}
	
	@Override
	public String getDescription() {
		return this.description;
	}
	
	@Override
	public String getVersion() {
		return versionArrayToString(this.version);
	}
	
	@Override
	@JsonIgnore
	public int[] getVersionInts() {
		return version;
	}

	@Override
	public boolean isRoot() {
		return this.isRoot;
	}

	@Override
	public boolean isValid()
	{
		return this.isValid;
	}

	@Override
	@JsonIgnore
	public InputStream getStream() throws PAPException, IOException {
		try {
			if (this.location != null) {
				URL url = this.location.toURL();
				return url.openStream();
			}
			return null;
		} catch (FileNotFoundException e) {
			throw new PAPException(e);
		}
	}

	@Override
	public URI getLocation() throws PAPException {
		return this.location;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((policyId == null) ? 0 : policyId.hashCode());
		result = prime * result;
		if (version != null) {
			for (int i = 0; i < version.length; i++) {
				result += version[i];
			}
		}
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		StdPDPPolicy other = (StdPDPPolicy) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (policyId == null) {
			if (other.policyId != null)
				return false;
		} else if (!policyId.equals(other.policyId))
			return false;
		if (version != other.version)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "StdPDPPolicy [id=" + id + ", name=" + name + ", policyId="
				+ policyId + ", description=" + description + ", version="
				+ this.getVersion() + ", isRoot=" + isRoot + ", isValid=" + isValid
				+ ", location=" + location + "]";
	}
	
	
	/**
	 * Given a version string consisting of integers with dots between them, convert it into an array of ints.
	 * 
	 * @param version
	 * @return
	 * @throws NumberFormatException
	 */
	public static int[] versionStringToArray(String version) throws NumberFormatException {
		if (version == null || version.length() == 0) {
			return new int[0];
		}
		String[] stringArray = version.split("\\.");
		int[] resultArray = new int[stringArray.length];
		for (int i = 0; i < stringArray.length; i++) {
			resultArray[i] = Integer.parseInt(stringArray[i]);
		}
		return resultArray;
	}
	
	/**
	 * Given an array representing a version, create the corresponding dot-separated string.
	 * 
	 * @param array
	 * @return
	 */
	public static String versionArrayToString(int[] array) {
		if (array == null || array.length == 0) {
			return "";
		}
		String versionString = "";
		if (array.length > 0) {
			versionString = "" + array[0];
			for (int i = 1; i < array.length; i++) {
				versionString += "." + array[i];
			}
		}
		return versionString;
	}
	

	
	//
	// Methods needed for JSON Deserialization
	//
	public StdPDPPolicy() {}
	
	public void setPolicyId(String policyId) {
		this.policyId = policyId;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public void setVersion(String version) {
		this.version = versionStringToArray(version);
	}
	public void setRoot(boolean isRoot) {
		this.isRoot = isRoot;
	}
	public void setValid(boolean isValid) {
		this.isValid = isValid;
	}
	public void setLocation(URI location) {
		this.location = location;
	}
	
}