1599ff20bccdae9fa1a9d390f01f7ea6e4d18339
[policy/apex-pdp.git] /
1 #-------------------------------------------------------------------------------
2 # ============LICENSE_START=======================================================
3 #  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8
9 #      http://www.apache.org/licenses/LICENSE-2.0
10
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
19 #-------------------------------------------------------------------------------
20
21 model create name=MyFirstPolicyModel version=0.0.1 uuid=540226fb-55ee-4f0e-a444-983a0494818e description="This is my first Apex Policy Model."
22
23 schema create name=assistant_ID_type version=0.0.1 uuid=36df4c71-9616-4206-8b53-976a5cd4bd87 description="A type for 'assistant_ID' values" flavour=Java schema=java.lang.Long
24
25 schema create name=authorised_type version=0.0.1 uuid=d48b619e-d00d-4008-b884-02d76ea4350b description="A type for 'authorised' values" flavour=Java schema=java.lang.Boolean
26
27 schema create name=branch_ID_type version=0.0.1 uuid=6468845f-4122-4128-8e49-0f52c26078b5 description="A type for 'branch_ID' values" flavour=Java schema=java.lang.Long
28
29 schema create name=item_ID_type version=0.0.1 uuid=4f227ff1-aee0-453a-b6b6-9a4b2e0da932 description="A type for 'item_ID' values" flavour=Java schema=java.lang.Long
30
31 schema create name=message_type version=0.0.1 uuid=ad1431bb-3155-4e73-b5a3-b89bee498749 description="A type for 'message' values" flavour=Java schema=java.lang.String
32
33 schema create name=notes_type version=0.0.1 uuid=eecfde90-896c-4343-8f9c-2603ced94e2d description="A type for 'notes' values" flavour=Java schema=java.lang.String
34
35 schema create name=price_type version=0.0.1 uuid=52c2fc45-fd8c-463c-bd6f-d91b0554aea7 description="A type for 'amount'/'price' values" flavour=Java schema=java.lang.Long
36
37 schema create name=quantity_type version=0.0.1 uuid=ac3d9842-80af-4a98-951c-bd79a431c613 description="A type for 'quantity' values" flavour=Java schema=java.lang.Integer
38
39 schema create name=sale_ID_type version=0.0.1 uuid=cca47d74-7754-4a61-b163-ca31f66b157b description="A type for 'sale_ID' values" flavour=Java schema=java.lang.Long
40
41 schema create name=timestamp_type version=0.0.1 uuid=fd594e88-411d-4a94-b2be-697b3a0d7adf description="A type for 'time' values" flavour=Java schema=java.lang.Long
42
43 task create name=MorningBoozeCheck version=0.0.1 uuid=3351b0f4-cf06-4fa2-8823-edf67bd30223 description=LS
44 This task checks if the sales request is for an item that contains alcohol. 
45 If the local time is between 00:00:00 and 11:30:00 then the sale is not authorised. Otherwise the sale is authorised. 
46 In this implementation we assume that all items with item_ID values between 1000 and 2000 contain alcohol :-)
47 LE
48 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
49 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
50 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
51 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
52 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
53 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
54 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
55 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
56 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
57 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
58 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
59 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
60 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
61 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
62 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
63 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=authorised schemaName=authorised_type schemaVersion=0.0.1
64 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
65 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=message schemaName=message_type schemaVersion=0.0.1 optional=true
66 task logic create name=MorningBoozeCheck version=0.0.1 logicFlavour=MVEL logic=LS
67 /*
68  * ============LICENSE_START=======================================================
69  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
70  * ================================================================================
71  * Licensed under the Apache License, Version 2.0 (the "License");
72  * you may not use this file except in compliance with the License.
73  * You may obtain a copy of the License at
74  * 
75  *      http://www.apache.org/licenses/LICENSE-2.0
76  * 
77  * Unless required by applicable law or agreed to in writing, software
78  * distributed under the License is distributed on an "AS IS" BASIS,
79  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
80  * See the License for the specific language governing permissions and
81  * limitations under the License.
82  * 
83  * SPDX-License-Identifier: Apache-2.0
84  * ============LICENSE_END=========================================================
85  */
86 import java.util.Date;
87 import java.util.Calendar;
88 import java.util.TimeZone;
89 import java.text.SimpleDateFormat;
90
91 logger.info("Task Execution: '"+subject.id+"'. Input Fields: '"+inFields+"'");
92
93 outFields.put("amount"      , inFields.get("amount"));
94 outFields.put("assistant_ID", inFields.get("assistant_ID"));
95 outFields.put("notes"       , inFields.get("notes"));
96 outFields.put("quantity"    , inFields.get("quantity"));
97 outFields.put("branch_ID"   , inFields.get("branch_ID"));
98 outFields.put("item_ID"     , inFields.get("item_ID"));
99 outFields.put("time"        , inFields.get("time"));
100 outFields.put("sale_ID"     , inFields.get("sale_ID"));
101
102 item_id = inFields.get("item_ID");
103
104 //The events used later to test this task use GMT timezone!
105 gmt = TimeZone.getTimeZone("GMT");
106 timenow = Calendar.getInstance(gmt);
107 df = new SimpleDateFormat("HH:mm:ss z");
108 df.setTimeZone(gmt);
109 timenow.setTimeInMillis(inFields.get("time"));
110
111 midnight = timenow.clone();
112 midnight.set(
113     timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
114     timenow.get(Calendar.DATE),0,0,0);
115 eleven30 = timenow.clone();
116 eleven30.set(
117     timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
118     timenow.get(Calendar.DATE),11,30,0);
119
120 itemisalcohol = false;
121 if(item_id != null && item_id >=1000 && item_id < 2000)
122     itemisalcohol = true;
123
124 if( itemisalcohol
125     && timenow.after(midnight) && timenow.before(eleven30)){
126   outFields.put("authorised", false);
127   outFields.put("message", "Sale not authorised by policy task "+subject.taskName+
128     " for time "+df.format(timenow.getTime())+
129     ". Alcohol can not be sold between "+df.format(midnight.getTime())+
130     " and "+df.format(eleven30.getTime()));
131   return true;
132 }
133 else{
134   outFields.put("authorised", true);
135   outFields.put("message", "Sale authorised by policy task "+subject.taskName+
136     " for time "+df.format(timenow.getTime()));
137   return true;
138 }
139
140 /*
141 This task checks if a sale request is for an item that is an alcoholic drink.
142 If the local time is between 00:00:00 GMT and 11:30:00 GMT then the sale is not
143 authorised. Otherwise the sale is authorised. 
144 In this implementation we assume that items with item_ID value between 1000 and 
145 2000 are all alcoholic drinks :-)
146 */
147 LE
148
149 task create name=MorningBoozeCheckAlt1 version=0.0.1 uuid=bc6d90c9-c902-4686-afd3-925b30e39990 description=LS
150 This task checks if a sale request is for an item that is an alcoholic drink. 
151 If the local time is between 00:00:00 CET and 13:00:00 CET then the sale is not authorised. 
152 Also alcohol sales are not allowed on Sundays. Otherwise the sale is authorised. 
153 In this implementation we assume that items with item_ID between 1000 and 2000 are all alcoholic drinks
154 LE
155 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
156 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
157 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
158 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
159 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
160 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
161 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
162 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
163 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
164 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
165 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
166 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
167 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
168 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
169 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
170 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=authorised schemaName=authorised_type schemaVersion=0.0.1
171 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
172 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=message schemaName=message_type schemaVersion=0.0.1 optional=true
173 task logic create name=MorningBoozeCheckAlt1 version=0.0.1 logicFlavour=MVEL logic=LS
174 /*
175  * ============LICENSE_START=======================================================
176  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
177  * ================================================================================
178  * Licensed under the Apache License, Version 2.0 (the "License");
179  * you may not use this file except in compliance with the License.
180  * You may obtain a copy of the License at
181  * 
182  *      http://www.apache.org/licenses/LICENSE-2.0
183  * 
184  * Unless required by applicable law or agreed to in writing, software
185  * distributed under the License is distributed on an "AS IS" BASIS,
186  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
187  * See the License for the specific language governing permissions and
188  * limitations under the License.
189  * 
190  * SPDX-License-Identifier: Apache-2.0
191  * ============LICENSE_END=========================================================
192  */
193 import java.util.Date;
194 import java.util.Calendar;
195 import java.util.TimeZone;
196 import java.text.SimpleDateFormat;
197
198 logger.info("Task Execution: '"+subject.id+"'. Input Event: '"+inFields+"'");
199
200 outFields.put("amount"      , inFields.get("amount"));
201 outFields.put("assistant_ID", inFields.get("assistant_ID"));
202 outFields.put("notes"       , inFields.get("notes"));
203 outFields.put("quantity"    , inFields.get("quantity"));
204 outFields.put("branch_ID"   , inFields.get("branch_ID"));
205 outFields.put("item_ID"     , inFields.get("item_ID"));
206 outFields.put("time"        , inFields.get("time"));
207 outFields.put("sale_ID"     , inFields.get("sale_ID"));
208
209 item_id = inFields.get("item_ID");
210
211 //The events used later to test this task use CET timezone!
212 cet = TimeZone.getTimeZone("CET");
213 timenow = Calendar.getInstance(cet);
214 df = new SimpleDateFormat("HH:mm:ss z");
215 df.setTimeZone(cet);
216 timenow.setTimeInMillis(inFields.get("time"));
217
218 midnight = timenow.clone();
219 midnight.set(
220     timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
221     timenow.get(Calendar.DATE),0,0,0);
222 onepm = timenow.clone();
223 onepm.set(
224     timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
225     timenow.get(Calendar.DATE),13,0,0);
226
227 itemisalcohol = false;
228 if(item_id != null && item_id >=1000 && item_id < 2000)
229     itemisalcohol = true;
230
231 if( itemisalcohol &&
232     ( (timenow.after(midnight) && timenow.before(onepm))
233       ||
234       (timenow.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
235     )){
236   outFields.put("authorised", false);
237   outFields.put("message", "Sale not authorised by policy task "+subject.taskName+
238     " for time "+df.format(timenow.getTime())+
239     ". Alcohol can not be sold between "+df.format(midnight.getTime())+
240     " and "+df.format(onepm.getTime()) +" or on Sunday");
241   return true;
242 }
243 else{
244   outFields.put("authorised", true);
245   outFields.put("message", "Sale authorised by policy task "+subject.taskName+
246     " for time "+df.format(timenow.getTime()));
247   return true;
248 }
249
250 /*
251 This task checks if a sale request is for an item that is an alcoholic drink. 
252 If the local time is between 00:00:00 CET and 13:00:00 CET then the sale is not authorised. 
253 Also alcohol sales are not allowed on Sundays. Otherwise the sale is authorised. 
254 In this implementation we assume that items with item_ID between 1000 and 2000 are all alcoholic drinks :-)
255 */
256 LE
257
258 event create name=SALE_AUTH version=0.0.1 uuid=c4500941-3f98-4080-a9cc-5b9753ed050b description="An event emitted by the Policy to indicate whether the sale of an item has been authorised" nameSpace=com.hyperm source="APEX" target="POS"
259 event parameter create name=SALE_AUTH version=0.0.1 parName=amount schemaName=price_type schemaVersion=0.0.1
260 event parameter create name=SALE_AUTH version=0.0.1 parName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
261 event parameter create name=SALE_AUTH version=0.0.1 parName=authorised schemaName=authorised_type schemaVersion=0.0.1
262 event parameter create name=SALE_AUTH version=0.0.1 parName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
263 event parameter create name=SALE_AUTH version=0.0.1 parName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
264 event parameter create name=SALE_AUTH version=0.0.1 parName=message schemaName=message_type schemaVersion=0.0.1 optional=true
265 event parameter create name=SALE_AUTH version=0.0.1 parName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
266 event parameter create name=SALE_AUTH version=0.0.1 parName=quantity schemaName=quantity_type schemaVersion=0.0.1
267 event parameter create name=SALE_AUTH version=0.0.1 parName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
268 event parameter create name=SALE_AUTH version=0.0.1 parName=time schemaName=timestamp_type schemaVersion=0.0.1
269
270 event create name=SALE_INPUT version=0.0.1 uuid=4f04aa98-e917-4f4a-882a-c75ba5a99374 description="An event raised by the PoS system each time an item is scanned for purchase" nameSpace=com.hyperm source="POS" target="APEX"
271 event parameter create name=SALE_INPUT version=0.0.1 parName=amount schemaName=price_type schemaVersion=0.0.1
272 event parameter create name=SALE_INPUT version=0.0.1 parName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
273 event parameter create name=SALE_INPUT version=0.0.1 parName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
274 event parameter create name=SALE_INPUT version=0.0.1 parName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
275 event parameter create name=SALE_INPUT version=0.0.1 parName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
276 event parameter create name=SALE_INPUT version=0.0.1 parName=quantity schemaName=quantity_type schemaVersion=0.0.1
277 event parameter create name=SALE_INPUT version=0.0.1 parName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
278 event parameter create name=SALE_INPUT version=0.0.1 parName=time schemaName=timestamp_type schemaVersion=0.0.1
279
280
281 policy create name=MyFirstPolicy version=0.0.1 uuid=6c5e410f-489a-46ff-964e-982ce6e8b6d0 description="This is my first Apex policy. It checks if a sale should be authorised or not." template=FREEFORM firstState=BoozeAuthDecide
282 policy state create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide triggerName=SALE_INPUT triggerVersion=0.0.1 defaultTaskName=MorningBoozeCheck defaultTaskVersion=0.0.1
283 policy state output create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide outputName=MorningBoozeCheck_Output_Direct eventName=SALE_AUTH eventVersion=0.0.1 nextState=NULL
284 policy state taskref create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide taskLocalName=MorningBoozeCheckAlt1 taskName=MorningBoozeCheckAlt1 taskVersion=0.0.1 outputType=DIRECT outputName=MorningBoozeCheck_Output_Direct
285 policy state taskref create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide taskLocalName=MorningBoozeCheck taskName=MorningBoozeCheck taskVersion=0.0.1 outputType=DIRECT outputName=MorningBoozeCheck_Output_Direct
286 policy state selecttasklogic create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide logicFlavour=JAVASCRIPT logic=LS
287 /*
288  * ============LICENSE_START=======================================================
289  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
290  * ================================================================================
291  * Licensed under the Apache License, Version 2.0 (the "License");
292  * you may not use this file except in compliance with the License.
293  * You may obtain a copy of the License at
294  * 
295  *      http://www.apache.org/licenses/LICENSE-2.0
296  * 
297  * Unless required by applicable law or agreed to in writing, software
298  * distributed under the License is distributed on an "AS IS" BASIS,
299  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
300  * See the License for the specific language governing permissions and
301  * limitations under the License.
302  * 
303  * SPDX-License-Identifier: Apache-2.0
304  * ============LICENSE_END=========================================================
305  */
306
307 var returnValueType = Java.type("java.lang.Boolean");
308 var returnValue = new returnValueType(true);
309
310 executor.logger.info("Task Selection Execution: '"+executor.subject.id+"'. Input Event: '"+executor.inFields+"'");
311
312 branchid = executor.inFields.get("branch_ID");
313 taskorig = executor.subject.getTaskKey("MorningBoozeCheck");
314 taskalt = executor.subject.getTaskKey("MorningBoozeCheckAlt1");
315 taskdef = executor.subject.getDefaultTaskKey();
316
317 if(branchid >=0 && branchid <1000){
318   taskorig.copyTo(executor.selectedTask);
319 }
320 else if (branchid >=1000 && branchid <2000){
321   taskalt.copyTo(executor.selectedTask);
322 }
323 else{
324   taskdef.copyTo(executor.selectedTask);
325 }
326
327 /*
328 This task selection logic selects task "MorningBoozeCheck" for branches with 0<=branch_ID<1000 and selects task "MorningBoozeCheckAlt1" for branches with 1000<=branch_ID<2000. Otherwise the default task is selected. In this case the default task is also "MorningBoozeCheck"
329 */
330 LE
331
332
333