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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
|
/*
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
* Copyright (C) 2020-2022 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2023-2024 Nordix Foundation.
* ================================================================================
* 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.onap.policy.controlloop;
import java.time.Instant;
import java.util.Collections;
import java.util.stream.Collectors;
import org.onap.policy.controlloop.CanonicalOnset;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
import org.onap.policy.controlloop.VirtualControlLoopNotification;
import org.onap.policy.controlloop.ControlLoopNotificationType;
import org.onap.policy.controlloop.actor.xacml.XacmlActor;
import org.onap.policy.controlloop.actorserviceprovider.Operation;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.controlloop.actorserviceprovider.OperationFinalResult;
import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
import org.onap.policy.controlloop.eventmanager.ActorConstants;
import org.onap.policy.controlloop.eventmanager.Step;
import org.onap.policy.controlloop.utils.ControlLoopUtils;
import org.onap.policy.drools.apps.controller.usecases.UsecasesEventManager;
import org.onap.policy.controlloop.eventmanager.ClEventManagerWithSteps.State;
import org.onap.policy.controlloop.eventmanager.ClEventManagerWithOutcome.OperationOutcome2;
import org.onap.policy.controlloop.eventmanager.ClEventManagerWithEvent.NewEventStatus;
import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
import org.onap.policy.controlloop.eventmanager.EventManagerServices;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.onap.policy.drools.system.PolicyEngineConstants;
/*
* Called at initial start-up, to create the event services that will be used by all
* event managers in this rule engine instance.
*/
rule "CREATE.EVENT.SERVICES"
when
not (EventManagerServices())
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}", drools.getRule().getName());
try {
insert(new EventManagerServices("event-manager"));
} catch(RuntimeException e) {
logger.warn("{}: cannot create event services", drools.getRule().getName(), e);
}
end
/*
*
* Called when the ControlLoopParams object has been inserted into working memory from the PAP.
*
*/
rule "INSERT.PARAMS"
when
$params : ControlLoopParams()
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {} : TOSCA-POLICY=[{}]", $params.getClosedLoopControlName(), $params.getPolicyName() + "."
+ drools.getRule().getName(), $params.getToscaPolicy());
end
/*
*
* Called when a Tosca Policy is present.
*
*/
rule "NEW.TOSCA.POLICY"
when
$policy : ToscaPolicy()
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: [{}|{}|{}|{}]: CONTENT: {}", drools.getRule().getName(),
$policy.getType(), $policy.getTypeVersion(), $policy.getName(),
$policy.getVersion(), $policy);
ControlLoopParams params = ControlLoopUtils.toControlLoopParams($policy);
if (params != null) {
insert(params);
}
end
/*
* Remove Control Loop Parameters.
*/
rule "REMOVE.PARAMS"
when
$params : ControlLoopParams( $policyName : getPolicyName(), $policyVersion : getPolicyVersion() )
not ( ToscaPolicy( getName() == $policyName, getVersion() == $policyVersion ) )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: [{}|{}|{}]", drools.getRule().getName(),
$params.getPolicyScope(), $params.getPolicyName(), $params.getPolicyVersion());
retract($params);
end
/*
*
* This rule responds to DCAE Events where there is no manager yet. Either it is
* the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
*
*/
rule "EVENT"
when
$params : ControlLoopParams( $clName : getClosedLoopControlName() )
$event : CanonicalOnset( closedLoopControlName == $clName )
$services : EventManagerServices()
not ( UsecasesEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
getEvent() == $event ) )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: event={}",
$clName, $params.getPolicyName(), drools.getRule().getName(),
$event);
//
// Retract the event from memory; it will be managed by the manager from now on
//
retract($event);
VirtualControlLoopNotification notification = null;
try {
//
// Check the event, because we need it to not be null when
// we create the UsecasesEventManager. The UsecasesEventManager
// will do extra syntax checking as well as check if the closed loop is disabled.
//
if ($event.getRequestId() == null) {
notification = new VirtualControlLoopNotification($event);
notification.setNotification(ControlLoopNotificationType.REJECTED);
notification.setFrom("policy");
notification.setMessage("Missing requestId");
notification.setPolicyScope(drools.getRule().getName());
notification.setPolicyName($params.getPolicyName());
notification.setPolicyVersion($params.getPolicyVersion());
} else {
UsecasesEventManager manager =
new UsecasesEventManager($services, $params, $event, drools.getWorkingMemory());
insert(manager);
try {
// load the first policy/step
manager.start();
if (manager.getSteps().isEmpty()) {
// no steps imply no policies, thus go straight to DONE state
manager.setState(State.DONE);
manager.setAccepted(true);
notification = manager.makeNotification();
notification.setNotification(ControlLoopNotificationType.ACTIVE);
notification.setPolicyScope(drools.getRule().getName());
notification.setPolicyName($params.getPolicyName());
notification.setPolicyVersion($params.getPolicyVersion());
} else {
// Note: the notification will be generated lazily
manager.setState(State.POLICY_LOADED);
}
} catch(Exception e) {
retract(manager);
throw e;
}
}
} catch (Exception e) {
logger.warn("{}: {}.{}: error starting manager", $clName, $params.getPolicyName(),
drools.getRule().getName(), e);
notification = new VirtualControlLoopNotification($event);
notification.setNotification(ControlLoopNotificationType.REJECTED);
notification.setMessage("Exception occurred: " + e.getMessage());
notification.setPolicyScope(drools.getRule().getName());
notification.setPolicyName($params.getPolicyName());
notification.setPolicyVersion($params.getPolicyVersion());
}
//
// Generate notification
//
try {
if (notification != null) {
PolicyEngineConstants.getManager().deliver("policy-cl-mgt", notification);
}
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: event={} exception generating notification",
$clName, $params.getPolicyName(), drools.getRule().getName(),
$event, e);
}
end
/*
*
* This rule fires when we get a subsequent event.
*
*/
rule "EVENT.MANAGER.NEW.EVENT"
when
$event : VirtualControlLoopEvent( )
$manager : UsecasesEventManager( closedLoopControlName == $event.getClosedLoopControlName(),
getEvent() == $event )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: event={} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$event, $manager);
//
// Remove the event from memory
//
retract($event);
//
// Check what kind of event this is
//
switch($manager.onNewEvent($event)) {
case SYNTAX_ERROR:
//
// Ignore any bad syntax events
//
logger.warn("{}: {}.{}: syntax error",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName());
break;
case FIRST_ABATEMENT:
case SUBSEQUENT_ABATEMENT:
//
// TODO: handle the abatement. Currently, it's just discarded.
//
logger.info("{}: {}.{}: abatement",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName());
break;
case FIRST_ONSET:
case SUBSEQUENT_ONSET:
default:
//
// We don't care about subsequent onsets
//
logger.warn("{}: {}.{}: subsequent onset",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName());
break;
}
end
/*
*
* All steps have been executed, load the next policy.
*
*/
rule "EVENT.MANAGER.LOAD.NEXT.POLICY"
when
$manager : UsecasesEventManager(
isActive(),
getState() == State.POLICY_LOADED,
getSteps().isEmpty() )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager);
try {
$manager.loadNextPolicy($manager.getResult());
if ($manager.getSteps().isEmpty()) {
// no steps - must be the final policy
$manager.setState(State.DONE);
}
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception loading next policy",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to load next policy");
}
update($manager);
end
/*
*
* Policy loaded, identify any preprocessor steps that need to be run first.
*
*/
rule "EVENT.MANAGER.PREPROCESS"
when
$manager : UsecasesEventManager(
isActive(),
getState() == State.POLICY_LOADED,
$step : getSteps().peek(),
$step != null,
!$step.isPreprocessed() )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
try {
/*
* Load any preprocessor steps.
*
* Note: this will not change the state of the manager, but it may change the
* state of the step.
*/
$step.setPreprocessed(true);
$manager.loadPreprocessorSteps();
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception loading preprocessor steps",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to load preprocessing steps");
}
update($manager);
end
/*
*
* Accepts the event, when ready to execute a step of interest. Does not change the
* state of the manager, leaving that to be done by rule "EVENT.MANAGER.EXECUTE.STEP".
*
*/
rule "EVENT.MANAGER.ACCEPT"
salience 200
when
$manager : UsecasesEventManager(
isActive(),
!isAccepted(),
getState() == State.POLICY_LOADED,
$step : getSteps().peek(),
$step != null,
$step.isPreprocessed(),
$step.acceptsEvent() )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
try {
$manager.setAccepted(true);
VirtualControlLoopNotification notification = $manager.makeNotification();
notification.setNotification(ControlLoopNotificationType.ACTIVE);
notification.setPolicyScope(drools.getRule().getName());
notification.setPolicyName($manager.getPolicyName());
notification.setPolicyVersion($manager.getPolicyVersion());
$manager.deliver("policy-cl-mgt", notification, "notification", drools.getRule().getName());
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception processing operation outcome",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to accept the event");
}
update($manager);
end
/*
*
* Ready to execute the step.
*
*/
rule "EVENT.MANAGER.EXECUTE.STEP"
when
$manager : UsecasesEventManager(
isActive(),
getState() == State.POLICY_LOADED,
$step : getSteps().peek(),
$step != null,
$step.isPreprocessed() )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
try {
$step.init();
$step.setProperties();
boolean guardDisabled = "true".equalsIgnoreCase(
PolicyEngineConstants.getManager().getEnvironmentProperty(
ControlLoopEventManager.GUARD_DISABLED_PROPERTY));
if (guardDisabled && XacmlActor.NAME.equals($step.getActorName())) {
// guard is disabled - just enqueue a "SUCCESS" (i.e., "Permit")
OperationOutcome outcome = $step.getParams().makeOutcome();
outcome.setStart(Instant.now());
outcome.setEnd(outcome.getStart());
$manager.getOutcomes().add(outcome);
$manager.setState(State.AWAITING_OUTCOME);
} else if ($manager.executeStep()) {
$manager.setState(State.AWAITING_OUTCOME);
} else {
// this step is no longer necessary - try the next one
$manager.nextStep();
}
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception executing a step",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to execute the next step");
}
update($manager);
end
/*
*
* Generate SDNR notification. Does not discard the outcome from the queue, leaving it to be
* handled by rule "EVENT.MANAGER.PROCESS.OUTCOME".
*
*/
rule "EVENT.MANAGER.GENERATE.SDNR.NOTIFICATION"
// this should fire BEFORE the "EVENT.MANAGER.PROCESS.OUTCOME" rule
salience 100
when
$manager : UsecasesEventManager(
isActive(),
getState() == State.AWAITING_OUTCOME,
$outcome : getOutcomes().peek(),
$outcome != null,
$outcome.getEnd() != null,
!isAbort($outcome),
$step : getSteps().peek(),
"SDNR".equals($step.getActorName()),
$outcome.isFor("SDNR", $step.getOperationName()) )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
try {
ControlLoopResponse clResponse = $manager.makeControlLoopResponse($outcome);
$manager.deliver("dcae_cl_rsp", clResponse, "SDNR notification", drools.getRule().getName());
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception generating SDNR Response notification",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
}
end
/*
*
* Process a GUARD outcome. Does not discard the outcome from the queue, leaving it to be
* handled by rule "EVENT.MANAGER.PROCESS.OUTCOME".
*
*/
rule "EVENT.MANAGER.PROCESS.GUARD.OUTCOME"
salience 100
when
$manager : UsecasesEventManager(
isActive(),
getState() == State.AWAITING_OUTCOME,
$outcome : getOutcomes().peek(),
$outcome != null,
!isAbort($outcome),
$step : getSteps().peek(),
XacmlActor.NAME.equals($step.getActorName()),
$outcome.isFor($step.getActorName(), $step.getOperationName()) )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
try {
VirtualControlLoopNotification notification = $manager.makeNotification();
notification.setNotification(ControlLoopNotificationType.OPERATION);
notification.setPolicyScope(drools.getRule().getName());
notification.setPolicyName($manager.getPolicyName());
notification.setPolicyVersion($manager.getPolicyVersion());
notification.setHistory(Collections.emptyList());
// get actor/operation name from the policy step, not from the guard step
Step step2 = $step.getParentStep();
if ($outcome.getEnd() == null) {
// it's a "start" operation
notification.setMessage("Sending guard query for " + step2.getActorName() + " " + step2.getOperationName());
} else if ($outcome.getResult() == OperationResult.SUCCESS) {
notification.setMessage("Guard result for " + step2.getActorName() + " " + step2.getOperationName()
+ " is Permit");
} else {
// it's a failure
notification.setMessage("Guard result for " + step2.getActorName() + " " + step2.getOperationName()
+ " is Deny");
}
$manager.deliver("policy-cl-mgt", notification, "GUARD notification", drools.getRule().getName());
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception generating GUARD notification",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
}
end
/*
*
* Process an outcome when the policy's operation starts.
*
*/
rule "EVENT.MANAGER.PROCESS.POLICY.STARTED"
when
$manager : UsecasesEventManager(
isActive(),
getState() == State.AWAITING_OUTCOME,
$outcome : getOutcomes().peek(),
$outcome != null,
$outcome.getEnd() == null,
$step : getSteps().peek(),
$step.isPolicyStep(),
$outcome.isFor($step.getActorName(), $step.getOperationName()) )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
try {
$manager.getOutcomes().remove();
// it's a "start" operation for the step
$manager.bumpAttempts();
$manager.addToHistory($outcome);
$manager.storeInDataBase($manager.getPartialHistory().peekLast());
VirtualControlLoopNotification notification = $manager.makeNotification();
notification.setNotification(ControlLoopNotificationType.OPERATION);
notification.setPolicyScope(drools.getRule().getName());
notification.setPolicyName($manager.getPolicyName());
notification.setPolicyVersion($manager.getPolicyVersion());
notification.setHistory(Collections.emptyList());
notification.setMessage($manager.getOperationMessage());
$manager.deliver("policy-cl-mgt", notification, "notification", drools.getRule().getName());
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception processing operation outcome",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to handle policy 'start' outcome");
}
update($manager);
end
/*
*
* Process an outcome when an arbitrary Preprocessor step starts.
*
*/
rule "EVENT.MANAGER.PROCESS.PREPROCESSOR.STARTED"
when
$manager : UsecasesEventManager(
isActive(),
getState() == State.AWAITING_OUTCOME,
$outcome : getOutcomes().peek(),
$outcome != null,
$outcome.getEnd() == null,
$step : getSteps().peek(),
!$step.isPolicyStep(),
$outcome.isFor($step.getActorName(), $step.getOperationName()) )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
try {
$manager.getOutcomes().remove();
// it's a "start" operation for the step
$manager.bumpAttempts();
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception processing operation outcome",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to handle 'start' outcome");
}
update($manager);
end
/*
*
* Process an outcome when the policy's operation succeeds.
*
*/
rule "EVENT.MANAGER.PROCESS.POLICY.SUCCESS"
when
$manager : UsecasesEventManager(
isActive(),
getState() == State.AWAITING_OUTCOME,
$outcome : getOutcomes().peek(),
$outcome != null,
$outcome.getEnd() != null,
$outcome.getResult() == OperationResult.SUCCESS,
$step : getSteps().peek(),
$step.isPolicyStep(),
$outcome.isFor($step.getActorName(), $step.getOperationName()) )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
try {
$manager.getOutcomes().remove();
// let the step record the response that's contained within the outcome
$step.success($outcome);
$manager.addToHistory($outcome);
$manager.storeInDataBase($manager.getPartialHistory().peekLast());
$manager.setResult($outcome.getResult());
VirtualControlLoopNotification notification = $manager.makeNotification();
notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
notification.setPolicyScope(drools.getRule().getName());
notification.setPolicyName($manager.getPolicyName());
notification.setPolicyVersion($manager.getPolicyVersion());
notification.setHistory($manager.getPartialHistory().stream().map(OperationOutcome2::getClOperation)
.collect(Collectors.toList()));
// this step is complete - discard it
$manager.getSteps().remove();
$manager.setState(State.POLICY_LOADED);
$manager.deliver("policy-cl-mgt", notification, "notification", drools.getRule().getName());
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception processing operation outcome",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to handle policy 'success' outcome");
}
update($manager);
end
/*
*
* Process a final failure outcome, when the event has been accepted.
*
*/
rule "EVENT.MANAGER.PROCESS.FINAL.FAILURE.ACCEPTED"
when
$manager : UsecasesEventManager(
isActive(),
isAccepted(),
getState() == State.AWAITING_OUTCOME,
$outcome : getOutcomes().peek(),
$outcome != null,
!isAbort($outcome),
$outcome.getEnd() != null,
$outcome.isFinalOutcome(),
$outcome.getResult() != OperationResult.SUCCESS,
$step : getSteps().peek() )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
try {
$manager.getOutcomes().remove();
if (!$outcome.isFor($step.getActorName(), $step.getOperationName())) {
$outcome.setResult(OperationResult.FAILURE_GUARD);
$outcome.setMessage("Operation denied by " + $outcome.getActor());
}
// final failure for this policy
$manager.addToHistory($outcome);
$manager.storeInDataBase($manager.getPartialHistory().peekLast());
$manager.setResult($outcome.getResult());
VirtualControlLoopNotification notification = $manager.makeNotification();
notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
notification.setPolicyScope(drools.getRule().getName());
notification.setPolicyName($manager.getPolicyName());
notification.setPolicyVersion($manager.getPolicyVersion());
notification.setHistory($manager.getPartialHistory().stream().map(OperationOutcome2::getClOperation)
.collect(Collectors.toList()));
// trigger move to the next policy - clear all steps
$manager.getSteps().clear();
$manager.setState(State.POLICY_LOADED);
$manager.deliver("policy-cl-mgt", notification, "notification", drools.getRule().getName());
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception processing operation outcome",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to handle policy 'failure' outcome");
}
update($manager);
end
/*
*
* Process a final failure outcome, when the event has NOT been accepted. This typically
* occurs when an A&AI query fails BEFORE the first lock has been requested (and thus
* before the first policy's operation has been started).
*
*/
rule "EVENT.MANAGER.PROCESS.FINAL.FAILURE.REJECTED"
when
$manager : UsecasesEventManager(
isActive(),
!isAccepted(),
getState() == State.AWAITING_OUTCOME,
$outcome : getOutcomes().peek(),
$outcome != null,
!isAbort($outcome),
$outcome.getEnd() != null,
$outcome.isFinalOutcome(),
$outcome.getResult() != OperationResult.SUCCESS,
$step : getSteps().peek() )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
retract($manager);
try {
// final failure for this policy
$manager.addToHistory($outcome);
$manager.setResult($outcome.getResult());
VirtualControlLoopNotification notification = $manager.makeNotification();
notification.setNotification(ControlLoopNotificationType.REJECTED);
notification.setPolicyScope(drools.getRule().getName());
notification.setPolicyName($manager.getPolicyName());
notification.setPolicyVersion($manager.getPolicyVersion());
notification.setHistory($manager.getPartialHistory().stream().map(OperationOutcome2::getClOperation)
.collect(Collectors.toList()));
$manager.deliver("policy-cl-mgt", notification, "notification", drools.getRule().getName());
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception processing operation outcome",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to reject event");
}
$manager.destroy();
end
/*
*
* Process an outcome when the policy's operation fails.
*
*/
rule "EVENT.MANAGER.PROCESS.POLICY.FAILURE"
when
$manager : UsecasesEventManager(
isActive(),
getState() == State.AWAITING_OUTCOME,
$outcome : getOutcomes().peek(),
$outcome != null,
!isAbort($outcome),
$outcome.getEnd() != null,
!$outcome.isFinalOutcome(),
$outcome.getResult() != OperationResult.SUCCESS,
$step : getSteps().peek(),
$step.isPolicyStep(),
$outcome.isFor($step.getActorName(), $step.getOperationName()) )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
try {
// not a final failure, thus it will be retried automatically
$manager.getOutcomes().remove();
// do NOT invoke manager.setResult()
$manager.addToHistory($outcome);
$manager.storeInDataBase($manager.getPartialHistory().peekLast());
VirtualControlLoopNotification notification = $manager.makeNotification();
notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
notification.setPolicyScope(drools.getRule().getName());
notification.setPolicyName($manager.getPolicyName());
notification.setPolicyVersion($manager.getPolicyVersion());
$manager.deliver("policy-cl-mgt", notification, "notification", drools.getRule().getName());
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception processing operation outcome",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to handle policy 'failure' outcome");
}
update($manager);
end
/*
*
* Discard an outcome that was not handled by any other rule.
*
*/
rule "EVENT.MANAGER.DISCARD.OUTCOME"
salience -10
when
$manager : UsecasesEventManager(
isActive(),
getState() == State.AWAITING_OUTCOME,
$outcome : getOutcomes().peek(),
$outcome != null,
$step : getSteps().peek() )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $outcome.getResult(), $manager);
try {
$manager.getOutcomes().remove();
if ($outcome.getEnd() != null && $outcome.isFor($step.getActorName(), $step.getOperationName())) {
// it's a completion for the step
// let the step record the response that's contained within the outcome
if ($outcome.getResult() == OperationResult.SUCCESS) {
$step.success($outcome);
}
// this step is complete - discard it
$manager.getSteps().remove();
$manager.setState(State.POLICY_LOADED);
}
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception processing operation outcome",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to handle outcome");
}
update($manager);
end
/*
*
* Abort processing. This can happen in any state (once the manager has been started).
*
*/
rule "EVENT.MANAGER.ABORT"
when
$manager : UsecasesEventManager(
isActive(),
getState() != State.DONE,
$outcome : getOutcomes().peek(),
$outcome != null,
isAbort($outcome),
$step : getSteps().peek() )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: {} manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$step, $manager);
try {
// determine the final message
String msg;
switch ($outcome.getActor()) {
case ActorConstants.CL_TIMEOUT_ACTOR:
msg = "Control Loop timed out";
break;
case ActorConstants.LOCK_ACTOR:
msg = "Target Lock was lost";
break;
default:
msg = "Processing aborted by " + $outcome.getActor();
break;
}
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE, msg);
if ($step != null && "SDNR".equals($step.getActorName())
&& $outcome.isFor($step.getActorName(), $step.getOperationName())) {
// aborted while processing the SDNR step - generate a notification
ControlLoopResponse clResponse = $manager.makeControlLoopResponse($outcome);
$manager.deliver("dcae_cl_rsp", clResponse, "SDNR notification", drools.getRule().getName());
}
if ($step != null) {
$outcome.setActor($step.getActorName());
$outcome.setOperation($step.getOperationName());
$manager.addToHistory($outcome);
$manager.storeInDataBase($manager.getPartialHistory().peekLast());
}
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception handling ABORT outcome",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
$manager.abort(State.DONE, OperationFinalResult.FINAL_FAILURE_EXCEPTION, "failed to handle ABORT");
}
update($manager);
end
/*
*
* Done processing. Arriving here implies that the event has been accepted.
*
*/
rule "EVENT.MANAGER.FINAL"
when
$manager : UsecasesEventManager(
!isActive() || getState() == State.DONE )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}.{}: manager={}",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager);
retract($manager);
try {
VirtualControlLoopNotification notification = $manager.makeNotification();
notification.setPolicyScope(drools.getRule().getName());
notification.setPolicyName($manager.getPolicyName());
notification.setPolicyVersion($manager.getPolicyVersion());
notification.setHistory($manager.getFullHistory().stream().map(OperationOutcome2::getClOperation)
.collect(Collectors.toList()));
OperationFinalResult finalResult = $manager.getFinalResult();
if (finalResult == null) {
finalResult = ($manager.isActive() ? OperationFinalResult.FINAL_SUCCESS : OperationFinalResult.FINAL_FAILURE);
}
switch (finalResult) {
case FINAL_FAILURE_EXCEPTION:
notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
notification.setMessage("Exception in processing closed loop");
break;
case FINAL_SUCCESS:
notification.setNotification(ControlLoopNotificationType.FINAL_SUCCESS);
break;
case FINAL_OPENLOOP:
notification.setNotification(ControlLoopNotificationType.FINAL_OPENLOOP);
break;
case FINAL_FAILURE:
default:
notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
break;
}
if ($manager.getFinalMessage() != null) {
notification.setMessage($manager.getFinalMessage());
}
$manager.deliver("policy-cl-mgt", notification, "notification", drools.getRule().getName());
} catch(RuntimeException e) {
logger.warn("{}: {}.{}: manager={} exception generating final notification",
$manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
$manager, e);
}
$manager.destroy();
end
/*
*
* This rule will clean up any rogue events where there is no
* ControlLoopParams object corresponding to the onset event.
*
*/
rule "EVENT.CLEANUP"
salience -100
when
$event : VirtualControlLoopEvent( $clName: closedLoopControlName )
then
Logger logger = LoggerFactory.getLogger(drools.getRule().getPackageName());
logger.info("{}: {}", $clName, drools.getRule().getName());
logger.debug("{}: {}: orphan event={}",
$clName, drools.getRule().getName(), $event);
//
// Retract the event
//
retract($event);
end
/*
*
* At this point, it appears that if we prevent the rules from getting messages from
* topics, then that will also prevent the actors from getting them. So the following
* rules are here just to discard those messages.
*
* These have a higher salience so the objects are removed before the "FINAL" message
* is processed, so that the junit test can assume things are done once they see the
* "FINAL" message. Otherwise, tests might fail sporadically.
*
*/
rule "APPC.Response.CLEANUP"
salience 1
when
$msg : org.onap.policy.appc.Response( )
then
retract($msg);
end
rule "APPC.Request.CLEANUP"
salience 1
when
$msg : org.onap.policy.appc.Request( )
then
retract($msg);
end
rule "APPC-LCM.Response.CLEANUP"
salience 1
when
$msg : org.onap.policy.appclcm.AppcLcmMessageWrapper( )
then
retract($msg);
end
rule "SDNR.Response.CLEANUP"
salience 1
when
$msg : org.onap.policy.sdnr.PciResponseWrapper( )
then
retract($msg);
end
|