aboutsummaryrefslogtreecommitdiffstats
path: root/ECOMP-TEST/src/test/java/org/openecomp/policy/pdp/test/annotations/TestAnnotation.java
blob: 84b3f92929ef0989d759355085cf3e92ed682ff8 (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
/*-
 * ============LICENSE_START=======================================================
 * ECOMP-TEST
 * ================================================================================
 * 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.pdp.test.annotations;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.TimeZone;

import org.apache.commons.cli.ParseException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openecomp.policy.pdp.test.TestBase;

import com.att.research.xacml.api.DataTypeException;
import com.att.research.xacml.api.Response;
import com.att.research.xacml.std.annotations.RequestParser;
import com.att.research.xacml.std.annotations.XACMLAction;
import com.att.research.xacml.std.annotations.XACMLAttribute;
import com.att.research.xacml.std.annotations.XACMLEnvironment;
import com.att.research.xacml.std.annotations.XACMLMultiRequest;
import com.att.research.xacml.std.annotations.XACMLRequest;
import com.att.research.xacml.std.annotations.XACMLRequestReference;
import com.att.research.xacml.std.annotations.XACMLResource;
import com.att.research.xacml.std.annotations.XACMLSubject;
import com.att.research.xacml.std.datatypes.HexBinary;
import com.att.research.xacml.std.datatypes.IPAddress;
import com.att.research.xacml.std.datatypes.IPv4Address;
import com.att.research.xacml.std.datatypes.ISO8601DateTime;
import com.att.research.xacml.std.datatypes.ISO8601Time;
import com.att.research.xacml.util.FactoryException;

/**
 * This example application shows how to use annotations for Java classes to create requests to send to the
 * engine.
 * 
 *
 */
public class TestAnnotation extends TestBase {
	private static final Log logger	= LogFactory.getLog(TestAnnotation.class);
	
	private int	num;
	
	/**
	 * This is a sample class that uses annotations. In addition to demonstrating how to use XACML annotations,
	 * it also demonstrates the various Java objects that can be used and how the request parser will
	 * resolve each object's datatype.
	 * 
	 *
	 */
	@XACMLRequest(ReturnPolicyIdList=true)
	public class MyRequestAttributes {
		
		public MyRequestAttributes(String user, String action, String resource) {
			this.userID = user;
			this.action = action;
			this.resource = resource;
			this.today = new Date();
			this.yesterday = Calendar.getInstance();
			this.yesterday.add(Calendar.DAY_OF_MONTH, -1);
		}

		@XACMLSubject(includeInResults=true)
		String	userID;
		
		@XACMLSubject(attributeId="urn:oasis:names:tc:xacml:1.0:subject:subject-id-qualifier")
		boolean admin = false;
		
		@XACMLSubject(attributeId="urn:oasis:names:tc:xacml:1.0:subject:key-info", issuer="com:foo:security")
		HexBinary publicKey = new HexBinary(new byte[] {'1', '0'});
		
		@XACMLSubject(attributeId="urn:oasis:names:tc:xacml:1.0:subject:authentication-time")
		ISO8601Time	authenticationTime = new ISO8601Time(8, 0, 0, 0);
		
		/**
		 * Here our base object is "Object", but it is reflected as a Java "String". The parser
		 * will then use the XACML http://www.w3.org/2001/XMLSchema#string as the datatype.
		 */
		@XACMLSubject(attributeId="urn:oasis:names:tc:xacml:1.0:subject:authentication-method")
		Object authenticationMethod = new String("RSA Public Key");
		
		/**
		 * Here our base object is "String", but we use the annotation for datatype to clarify
		 * that the real XACML data type is http://www.w3.org/2001/XMLSchema#time. The parser will
		 * use the data type factory to convert the "String" to a "ISO8601Time" Java object.
		 */
		@XACMLSubject(attributeId="urn:oasis:names:tc:xacml:1.0:subject:request-time", datatype="http://www.w3.org/2001/XMLSchema#time")
		String requestTime = new String("13:20:00-05:00");
		
		@XACMLSubject(attributeId="urn:oasis:names:tc:xacml:1.0:subject:session-start-time")
		ISO8601DateTime sessionStart = new ISO8601DateTime(TimeZone.getDefault().getID(), 2014, 1, 1, 10, 0, 0, 0);
		
		@XACMLSubject(attributeId="urn:oasis:names:tc:xacml:3.0:subject:authn-locality:ip-address")
		IPAddress ip = new IPv4Address(new short[] {123, 134, 156, 255 }, null, null);
		
		@XACMLSubject(attributeId="urn:oasis:names:tc:xacml:3.0:subject:authn-locality:dns-name")
		String dnsName = "localhost";
		
		@XACMLAction()
		String	action;
		
		@XACMLAction(attributeId="urn:oasis:names:tc:xacml:1.0:action:implied-action")
		long	impliedAction;
		
		@XACMLResource()
		String	resource;
		
		@XACMLEnvironment()
		Date		today;
		
		@XACMLEnvironment()
		Calendar	yesterday;
		
		/**
		 * This field demonstrates how the parser can detect collections and build a bag of values.
		 */
		@XACMLAttribute(attributeId="foo:bar:attribute")
		Collection<Double>		fooBar = Arrays.asList(2.5, 3.5);
		
		/**
		 * The XACMLAttribute annotation allows one to specify all the 
		 */
		@XACMLAttribute(category="foo:bar:category", attributeId="foo:bar:attribute2")
		double		fooBar2 = 3.999;
		
		/**
		 * This field demonstrates how the parser can detect arrays and build a bag of values.
		 */
		@XACMLAttribute(category="foo:bar:category", attributeId="foo:bar:attribute:many")
		URI[]		fooBarMany = new URI[] {URI.create("file://opt/app/test"), URI.create("https://localhost:8443/")};
		
	};

	@XACMLRequest(
		Defaults="http://www.w3.org/TR/1999/Rec-xpath-19991116",
		multiRequest=@XACMLMultiRequest(values={
			@XACMLRequestReference(values={"subject1", "action", "resource"}),
			@XACMLRequestReference(values={"subject2", "action", "resource"})})
	)
	public class MyMultiRequestAttributes {
		
		@XACMLSubject(id="subject1")
		String	userID1 = "John";
		
		@XACMLSubject(id="subject2")
		String	userID2 = "Ringo";

		@XACMLAction(id="action")
		String	action = "access";

		@XACMLResource(id="resource")
		String	resource = "www.mywebsite.com";
	}

	public TestAnnotation(String[] args) throws MalformedURLException, ParseException, HelpException {
		super(args);
	}

	@Override
	public void run() throws IOException, FactoryException {
		//
		// We are not going to iterate any existing request files. So we will override
		// any TestBase code that assumes there are request files present.
		//
		//
		// Configure ourselves
		//
		this.configure();
		//
		// Cycle through creating a few objects
		//
		this.num = 0;
		this.doRequest(new MyRequestAttributes("John", "access", "www.mywebsite.com"));
		this.num++;
		this.doRequest(new MyRequestAttributes("Ringo", "access", "www.mywebsite.com"));
		this.num++;
		this.doRequest(new MyMultiRequestAttributes());
		this.num++;
	}

	private void doRequest(Object info) {
		try {
			Response response = this.callPDP(RequestParser.parseRequest(info));
			Path resultFile;
			if (this.output != null) {
				resultFile = Paths.get(this.output.toString(), "Response." + String.format("%03d", this.num) + ".json");
			} else {
				resultFile = Paths.get(this.directory, "results", "Response." + String.format("%03d", this.num) + ".json");
			}
			//
			// Write the response to the result file
			//
			logger.info("Response is: " + response.toString());
			if (resultFile != null) {
				Files.write(resultFile, response.toString().getBytes());
			}
		} catch (IllegalArgumentException | IllegalAccessException | DataTypeException | IOException e) {
			logger.error(e);
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		try {
			new TestAnnotation(args).run();
		} catch (ParseException | IOException | FactoryException e) {
			logger.error(e);
		} catch (HelpException e) {
			//
			// ignore this, its thrown just to exit the application
			// after dumping help to stdout.
			//
		}		
	}
}