aboutsummaryrefslogtreecommitdiffstats
path: root/docs/drools/pdpdEngine.rst
blob: 3456cc6d45e18bb3e4a6be88529287f684292c27 (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
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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
.. This work is licensed under a Creative Commons Attribution 4.0 International License.
.. http://creativecommons.org/licenses/by/4.0

.. _pdpd-engine-label:

PDP-D Engine
############

.. contents::
    :depth: 2

Overview
========

The PDP-D Core Engine provides an infrastructure and services for `drools <https://www.drools.org/>`__ based applications
in the context of Policies and ONAP.

A PDP-D supports applications by means of *controllers*.   A *controller* is a named
grouping of resources.   These typically include references to communication endpoints,
maven artifact coordinates, and *coders* for message mapping.

*Controllers* use *communication endpoints* to interact
with remote networked entities typically using messaging (dmaap or ueb),
or http.

PDP-D Engine capabilities can be extended via *features*.   Integration with other
Policy Framework components (API, PAP, and PDP-X) is through one of them (*feature-lifecycle*).

The PDP-D Engine infrastructure provides mechanisms for data migration, diagnostics, and application management.

Software
========

Source Code repositories
~~~~~~~~~~~~~~~~~~~~~~~~

The PDP-D software is mainly located in the `policy/drools repository <https://git.onap.org/policy/drools-pdp>`__ with the *communication endpoints* software residing in the `policy/common repository <https://git.onap.org/policy/common>`__ and Tosca policy models in the `policy/models repository <https://git.onap.org/policy/models>`__.

Docker Image
~~~~~~~~~~~~

Check the *drools-pdp* `released versions <https://wiki.onap.org/display/DW/Policy+Framework+Project%3A+Component+Versions>`__ page for the latest versions.
At the time of this writing *1.6.3* is the latest version.

.. code-block:: bash

    docker pull onap/policy-drools:1.6.3

A container instantiated from this image will run under the non-priviledged *policy* account.

The PDP-D root directory is located at the */opt/app/policy* directory (or *$POLICY_HOME*), with the
exception of the *$HOME/.m2* which contains the local maven repository.
The PDP-D configuration resides in the following directories:

- **/opt/app/policy/config**: (*$POLICY_HOME/config* or *$POLICY_CONFIG*) contains *engine*, *controllers*, and *endpoint* configuration.
- **/home/policy/.m2**: (*$HOME/.m2*) maven repository configuration.
- **/opt/app/policy/etc/**: (*$POLICY_HOME/etc*) miscellaneous configuration such as certificate stores.

The following command can be used to explore the directory layout.

.. code-block:: bash

    docker run --rm -it nexus3.onap.org:10001/onap/policy-drools:1.6.3 -- bash

Communication Endpoints
=======================

PDP-D supports the following networked infrastructures.   This is also referred to as
*communication infrastructures* in the source code.

- DMaaP
- UEB
- NOOP
- Http Servers
- Http Clients

The source code is located at
`the policy-endpoints module <https://git.onap.org/policy/common/tree/policy-endpoints>`__
in the *policy/commons* repository.

These network resources are *named* and typically have a *global* scope, therefore typically visible to
the PDP-D engine (for administration purposes), application *controllers*,
and *features*.

DMaaP, UEB, and NOOP are message-based communication infrastructures, hence the terminology of
source and sinks, to denote their directionality into or out of the *controller*, respectively.

An endpoint can either be *managed* or *unmanaged*.  The default for an endpoint is to be *managed*,
meaning that they are globally accessible by name, and managed by the PDP-D engine.
*Unmanaged* topics are used when neither global visibility, or centralized PDP-D management is desired.
The software that uses *unmanaged* topics is responsible for their lifecycle management.

DMaaP Endpoints
~~~~~~~~~~~~~~~

These are messaging enpoints that use DMaaP as the communication infrastructure.

Typically, a *managed* endpoint configuration is stored in the *<topic-name>-topic.properties* files.

For example, the
`DCAE_TOPIC-topic.properties <https://git.onap.org/policy/drools-applications/tree/controlloop/common/feature-controlloop-management/src/main/feature/config/DCAE_TOPIC-topic.properties>`__ is defined as

.. code-block:: bash

    dmaap.source.topics=DCAE_TOPIC

    dmaap.source.topics.DCAE_TOPIC.effectiveTopic=${env:DCAE_TOPIC}
    dmaap.source.topics.DCAE_TOPIC.servers=${env:DMAAP_SERVERS}
    dmaap.source.topics.DCAE_TOPIC.consumerGroup=${env:DCAE_CONSUMER_GROUP}
    dmaap.source.topics.DCAE_TOPIC.https=true

In this example, the generic name of the *source* endpoint
is *DCAE_TOPIC*.  This is known as the *canonical* name.
The actual *topic* used in communication exchanges in a physical lab is contained
in the *$DCAE_TOPIC* environment variable.   This environment variable is usually
set up by *devops* on a per installation basis to meet the needs of each
lab spec.

In the previous example, *DCAE_TOPIC* is a source-only topic.

Sink topics are similarly specified but indicating that are sink endpoints
from the perspective of the *controller*.  For example, the *APPC-CL* topic
is configured as

.. code-block:: bash

    dmaap.source.topics=APPC-CL
    dmaap.sink.topics=APPC-CL

    dmaap.source.topics.APPC-CL.servers=${env:DMAAP_SERVERS}
    dmaap.source.topics.APPC-CL.https=true

    dmaap.sink.topics.APPC-CL.servers=${env:DMAAP_SERVERS}
    dmaap.sink.topics.APPC-CL.https=true

Although not shown in these examples, additional configuration options are available such as *user name*,
*password*, *security keys*, *consumer group* and *consumer instance*.

UEB Endpoints
~~~~~~~~~~~~~

Similary, UEB endpoints are messaging endpoints, similar to the DMaaP ones.

For example, the
`DCAE_TOPIC-topic.properties <https://git.onap.org/policy/drools-applications/tree/controlloop/common/feature-controlloop-management/src/main/feature/config/DCAE_TOPIC-topic.properties>`__ can be converted to an *UEB* one, by replacing the
*dmaap* prefix with *ueb*.  For example:

.. code-block:: bash

    ueb.source.topics=DCAE_TOPIC

    ueb.source.topics.DCAE_TOPIC.effectiveTopic=${env:DCAE_TOPIC}
    ueb.source.topics.DCAE_TOPIC.servers=${env:DMAAP_SERVERS}
    ueb.source.topics.DCAE_TOPIC.consumerGroup=${env:DCAE_CONSUMER_GROUP}
    ueb.source.topics.DCAE_TOPIC.https=true

NOOP Endpoints
~~~~~~~~~~~~~~

NOOP (no-operation) endpoints are messaging endpoints that don't have any network attachments.
They are used for testing convenience.
To convert the
`DCAE_TOPIC-topic.properties <https://git.onap.org/policy/drools-applications/tree/controlloop/common/feature-controlloop-management/src/main/feature/config/DCAE_TOPIC-topic.properties>`__ to a *NOOP* endpoint, simply replace the *dmaap* prefix with *noop*:

.. code-block:: bash

    noop.source.topics=DCAE_TOPIC
    noop.source.topics.DCAE_TOPIC.effectiveTopic=${env:DCAE_TOPIC}

HTTP Clients
~~~~~~~~~~~~

HTTP Clients are typically stored in files following the naming convention: *<name>-http-client.properties* convention.
One such example is
the `AAI HTTP Client <https://git.onap.org/policy/drools-applications/tree/controlloop/common/feature-controlloop-management/src/main/feature/config/AAI-http-client.properties>`__:

.. code-block:: bash

    http.client.services=AAI

    http.client.services.AAI.managed=true
    http.client.services.AAI.https=true
    http.client.services.AAI.host=${envd:AAI_HOST}
    http.client.services.AAI.port=${envd:AAI_PORT}
    http.client.services.AAI.userName=${envd:AAI_USERNAME}
    http.client.services.AAI.password=${envd:AAI_PASSWORD}
    http.client.services.AAI.contextUriPath=${envd:AAI_CONTEXT_URI}

HTTP Servers
~~~~~~~~~~~~

HTTP Servers are stored in files that follow a similar naming convention *<name>-http-server.properties*.
The following is an example of a server named *CONFIG*, getting most of its configuration from
environment variables.

.. code-block:: bash

    http.server.services=CONFIG

    http.server.services.CONFIG.host=${envd:TELEMETRY_HOST}
    http.server.services.CONFIG.port=7777
    http.server.services.CONFIG.userName=${envd:TELEMETRY_USER}
    http.server.services.CONFIG.password=${envd:TELEMETRY_PASSWORD}
    http.server.services.CONFIG.restPackages=org.onap.policy.drools.server.restful
    http.server.services.CONFIG.managed=false
    http.server.services.CONFIG.swagger=true
    http.server.services.CONFIG.https=true
    http.server.services.CONFIG.aaf=${envd:AAF:false}

*Endpoints* configuration resides in the *$POLICY_HOME/config* (or *$POLICY_CONFIG*) directory in a container.

Controllers
===========

*Controllers* are the means for the PDP-D to run *applications*.   Controllers are
defined in *<name>-controller.properties* files.

For example, see the
`frankfurt controller configuration <https://git.onap.org/policy/drools-applications/tree/controlloop/common/feature-controlloop-frankfurt/src/main/feature/config/frankfurt-controller.properties>`__.

This configuration file has two sections: *a)* application maven coordinates, and *b)* endpoint references and coders.

Maven Coordinates
~~~~~~~~~~~~~~~~~

The coordinates section (*rules*) points to the *controller-frankfurt* *kjar* artifact.
It is the *brain* of the control loop application.

.. code-block:: bash

    controller.name=frankfurt

    rules.groupId=${project.groupId}
    rules.artifactId=controller-frankfurt
    rules.version=${project.version}
    .....

This *kjar* contains the
`frankfurt DRL <https://git.onap.org/policy/drools-applications/tree/controlloop/common/controller-frankfurt/src/main/resources/frankfurt.drl>`__ file (there may be more than one DRL file included).

.. code-block:: bash

    ...
    rule "NEW.TOSCA.POLICY"
        when
            $policy : ToscaPolicy()
        then

        ...

        ControlLoopParams params = ControlLoopUtils.toControlLoopParams($policy);
        if (params != null) {
            insert(params);
        }
    end
    ...

The DRL in conjuction with the dependent java libraries in the kjar
`pom <https://git.onap.org/policy/drools-applications/tree/controlloop/common/controller-frankfurt/pom.xml>`__
realizes the application's function.  For intance, it realizes the
vFirewall, vCPE, and vDNS use cases in ONAP.

.. code-block:: bash

    ..
    <dependency>
        <groupId>org.onap.policy.models.policy-models-interactions.model-actors</groupId>
        <artifactId>actor.appclcm</artifactId>
        <version>${policy.models.version}</version>
        <scope>provided</scope>
    </dependency>
    ...

Endpoints References and Coders
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The *frankfurt-controller.properties* configuration also contains a mix of
source (of incoming controller traffic) and sink (of outgoing controller traffic)
configuration.  This configuration also contains specific
filtering and mapping rules for incoming and outgoing dmaap messages
known as *coders*.

.. code-block:: bash

    ...
    dmaap.source.topics=DCAE_TOPIC,APPC-CL,APPC-LCM-WRITE,SDNR-CL-RSP
    dmaap.sink.topics=APPC-CL,APPC-LCM-READ,POLICY-CL-MGT,SDNR-CL,DCAE_CL_RSP


    dmaap.source.topics.APPC-LCM-WRITE.events=org.onap.policy.appclcm.AppcLcmDmaapWrapper
    dmaap.source.topics.APPC-LCM-WRITE.events.org.onap.policy.appclcm.AppcLcmDmaapWrapper.filter=[?($.type == 'response')]
    dmaap.source.topics.APPC-LCM-WRITE.events.custom.gson=org.onap.policy.appclcm.util.Serialization,gson

    dmaap.sink.topics.APPC-CL.events=org.onap.policy.appc.Request
    dmaap.sink.topics.APPC-CL.events.custom.gson=org.onap.policy.appc.util.Serialization,gsonPretty
    ...

In this example, the *coders* specify that incoming messages over the DMaaP endpoint
reference *APPC-LCM-WRITE*, that have a field called *type* under the root JSON object with
value *response* are allowed into the *controller* application.  In this case, the incoming
message is converted into an object (fact) of type *org.onap.policy.appclcm.AppcLcmDmaapWrapper*.
The *coder* has attached a custom implementation provided by the *application* with class
*org.onap.policy.appclcm.util.Serialization*.  Note that the *coder* filter is expressed in JSONPath notation.

Note that not all the communication endpoint references need to be explicitly referenced within the
*controller* configuration file.  For example, *Http clients* do not.
The reasons are historical, as the PDP-D was initially intended to only communicate
through messaging-based protocols such as UEB or DMaaP in asynchronous unidirectional mode.
The introduction of *Http* with synchronous bi-directional communication with remote endpoints made
it more convenient for the application to manage each network exchange.

*Controllers* configuration resides in the *$POLICY_HOME/config* (or *$POLICY_CONFIG*) directory in a container.

Other Configuration Files
~~~~~~~~~~~~~~~~~~~~~~~~~

There are other types of configuration files that *controllers* can use, for example *.environment* files
that provides a means to share data across applications.   The
`controlloop.properties.environment <https://git.onap.org/policy/drools-applications/tree/controlloop/common/feature-controlloop-management/src/main/feature/config/controlloop.properties.environment>`__ is one such example.


Tosca Policies
==============

PDP-D supports Tosca Policies through the *feature-lifecycle*.    The *PDP-D* receives its policy set
from the *PAP*.    A policy conforms to its Policy Type specification.
Policy Types and policy creation is done by the *API* component.
Policy deployments are orchestrated by the *PAP*.

All communication between *PAP* and PDP-D is over the DMaaP *POLICY-PDP-PAP* topic.

Native Policy Types
~~~~~~~~~~~~~~~~~~~

The PDP-D Engine supports two (native) Tosca policy types by means of the *lifecycle*
feature:

- *onap.policies.native.drools.Controller*
- *onap.policies.native.drools.Artifact*

These types can be used to dynamically deploy or undeploy application *controllers*,
assign policy types, and upgrade or downgrade their attached maven artifact versions.

For instance, an
`example native controller <https://git.onap.org/policy/drools-pdp/tree/feature-lifecycle/src/test/resources/tosca-policy-native-controller-example.json>`__ policy is shown below.

.. code-block:: bash

    {
        "tosca_definitions_version": "tosca_simple_yaml_1_0_0",
        "topology_template": {
            "policies": [
                {
                    "example.controller": {
                        "type": "onap.policies.native.drools.Controller",
                        "type_version": "1.0.0",
                        "version": "1.0.0",
                        "name": "example.controller",
                        "metadata": {
                            "policy-id": "example.controller"
                        },
                        "properties": {
                            "controllerName": "lifecycle",
                            "sourceTopics": [
                                {
                                    "topicName": "DCAE_TOPIC",
                                    "events": [
                                        {
                                            "eventClass": "java.util.HashMap",
                                            "eventFilter": "[?($.closedLoopEventStatus == 'ONSET')]"
                                        },
                                        {
                                            "eventClass": "java.util.HashMap",
                                            "eventFilter": "[?($.closedLoopEventStatus == 'ABATED')]"
                                        }
                                    ]
                                }
                            ],
                            "sinkTopics": [
                                {
                                    "topicName": "APPC-CL",
                                    "events": [
                                        {
                                            "eventClass": "java.util.HashMap",
                                            "eventFilter": "[?($.CommonHeader && $.Status)]"
                                        }
                                    ]
                                }
                            ],
                            "customConfig": {
                                "field1" : "value1"
                            }
                        }
                    }
                }
            ]
        }
    }

The actual application coordinates are provided with a policy of type onap.policies.native.drools.Artifact,
see the `example native artifact <https://git.onap.org/policy/drools-pdp/tree/feature-lifecycle/src/test/resources/tosca-policy-native-artifact-example.json>`__

.. code-block:: bash

    {
        "tosca_definitions_version": "tosca_simple_yaml_1_0_0",
        "topology_template": {
            "policies": [
                {
                    "example.artifact": {
                        "type": "onap.policies.native.drools.Artifact",
                        "type_version": "1.0.0",
                        "version": "1.0.0",
                        "name": "example.artifact",
                        "metadata": {
                            "policy-id": "example.artifact"
                        },
                        "properties": {
                            "rulesArtifact": {
                                "groupId": "org.onap.policy.drools.test",
                                "artifactId": "lifecycle",
                                "version": "1.0.0"
                            },
                            "controller": {
                                "name": "lifecycle"
                            }
                        }
                    }
                }
            ]
        }
    }

Operational Policy Types
~~~~~~~~~~~~~~~~~~~~~~~~

The PDP-D also recognizes Tosca Operational Policies, although it needs an
application *controller* that understands them to execute them.   These are:

- *onap.policies.controlloop.operational.common.Drools*
- *onap.policies.controlloop.Operational*

A minimum of one application *controller* that supports these capabilities
must be installed in order to honor the *operational policy types*.
One such controller is the *frankfurt* controller residing in the
`policy/drools-applications <https://git.onap.org/policy/drools-applications>`__
repository.

Controller Policy Type Support
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Note that a *controller* may support other policy types. A controller may declare them
explicitly in a native *onap.policies.native.drools.Controller* policy.

.. code-block:: bash

    "customConfig": {
        "controller.policy.types" : "policy.type.A"
    }

The *controller* application could declare its supported policy types in the *kjar*.
For example, the *frankfurt controller* packages this information in the
`kmodule.xml <https://git.onap.org/policy/drools-applications/tree/controlloop/common/controller-frankfurt/src/main/resources/META-INF/kmodule.xml>`__.    One advantage of this approach is that the PDP-D would only
commit to execute policies against these policy types if a supporting controller is up and running.

.. code-block:: bash

    <kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
        <kbase name="onap.policies.controlloop.operational.common.Drools" default="false" equalsBehavior="equality"/>
        <kbase name="onap.policies.controlloop.Operational" equalsBehavior="equality"
               packages="org.onap.policy.controlloop" includes="onap.policies.controlloop.operational.common.Drools">
            <ksession name="frankfurt"/>
        </kbase>
    </kmodule>

Software Architecture
======================

PDP-D is divided into 2 layers:

- core (`policy-core <https://git.onap.org/policy/drools-pdp/tree/policy-core>`__)
- management (`policy-management <https://git.onap.org/policy/drools-pdp/tree/policy-management>`__)

Core Layer
~~~~~~~~~~

The core layer directly interfaces with the *drools* libraries with 2 main abstractions:

* `PolicyContainer <https://git.onap.org/policy/drools-pdp/tree/policy-core/src/main/java/org/onap/policy/drools/core/PolicyContainer.java>`__, and
* `PolicySession <https://git.onap.org/policy/drools-pdp/tree/policy-core/src/main/java/org/onap/policy/drools/core/PolicySession.java>`__.

Policy Container and Sessions
"""""""""""""""""""""""""""""

The *PolicyContainer* abstracts the drools *KieContainer*, while a *PolicySession* abstracts a drools *KieSession*.
PDP-D uses stateful sessions in active mode (*fireUntilHalt*) (please visit the `drools <https://www.drools.org/>`__
website for additional documentation).

Management Layer
~~~~~~~~~~~~~~~~

The management layer manages the PDP-D and builds on top of the *core* capabilities.

PolicyEngine
""""""""""""

The PDP-D `PolicyEngine <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java>`__ is the top abstraction and abstracts away the PDP-D and all the
resources it holds.   The reader looking at the source code can start looking at this component
in a top-down fashion.   Note that the *PolicyEngine* abstraction should not be confused with the
sofware in the *policy/engine* repository, there is no relationship whatsoever other than in the naming.

The *PolicyEngine* represents the PDP-D, holds all PDP-D resources, and orchestrates activities among those.

The *PolicyEngine* manages applications via the `PolicyController <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/java/org/onap/policy/drools/system/PolicyController.java>`__ abstractions in the base code.  The
relationship between the *PolicyEngine* and *PolicyController* is one to many.

The *PolicyEngine* holds other global resources such as a *thread pool*, *policies validator*, *telemetry* server,
and *unmanaged* topics for administration purposes.

The *PolicyEngine* has interception points that allow
`*features* <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/java/org/onap/policy/drools/features/PolicyEngineFeatureApi.java>`__
to observe and alter the default *PolicyEngine* behavior.

The *PolicyEngine* implements the `*Startable* <https://git.onap.org/policy/common/tree/capabilities/src/main/java/org/onap/policy/common/capabilities/Startable.java>`__ and `*Lockable* <https://git.onap.org/policy/common/tree/capabilities/src/main/java/org/onap/policy/common/capabilities/Lockable.java>`__ interfaces.   These operations
have a cascading effect on the resources the *PolicyEngine* holds, as it is the top level entity, thus
affecting *controllers* and *endpoints*.   These capabilities are intended to be used for extensions,
for example active/standby multi-node capabilities.   This programmability is
exposed via the *telemetry* API, and *feature* hooks.

Configuration
^^^^^^^^^^^^^

*PolicyEngine* related configuration is located in the
`engine.properties <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/server/config/engine.properties>`__,
and `engine-system.properties <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/server/config/engine.properties>`__.

The *engine* configuration files reside in the *$POLICY_CONFIG* directory.

PolicyController
""""""""""""""""

A *PolicyController* represents an application.   Each *PolicyController* has an instance of a
`DroolsController <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/java/org/onap/policy/drools/system/PolicyController.java>`__.   The *PolicyController* provides the means to group application specific resources
into a single unit.  Such resources include the application's *maven coordinates*, *endpoint references*, and *coders*.

A *PolicyController* uses a
`DroolsController <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java>`__ to interface with the *core* layer (*PolicyContainer* and *PolicySession*).

The relationship between the *PolicyController* and the *DroolsController* is one-to-one.
The *DroolsController* currently supports 2 implementations, the
`MavenDroolsController <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java>`__, and the
`NullDroolsController <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java>`__.
The *DroolsController*'s polymorphic behavior depends on whether a maven artifact is attached to the controller or not.

Configuration
^^^^^^^^^^^^^

The *controllers* configuration resides in the *$POLICY_CONFIG* directory.

Programmability
~~~~~~~~~~~~~~~

PDP-D is programmable through:

- Features and Event Listeners.
- Maven-Drools applications.

Using Features and Listeners
""""""""""""""""""""""""""""

Features hook into the interception points provided by the the *PDP-D* main entities.

*Endpoint Listeners*, see `here <https://git.onap.org/policy/common/tree/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/TopicListener.java>`__
and `here <https://git.onap.org/policy/common/tree/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners>`__, can be used in conjuction with features for additional capabilities.

Using Maven-Drools applications
"""""""""""""""""""""""""""""""

Maven-based drools applications can run any arbitrary functionality structured with rules and java logic.

Recommended Flow
""""""""""""""""

Whenever possible it is suggested that PDP-D related operations flow through the
*PolicyEngine* downwards in a top-down manner.  This imposed order implies that
all the feature hooks are always invoked in a deterministic fashion.   It is also
a good mechanism to safeguard against deadlocks.

Telemetry Extensions
""""""""""""""""""""

It is recommended to *features* (extensions) to offer a diagnostics REST API
to integrate with the telemetry API.   This is done by placing JAX-RS files under
the package *org.onap.policy.drools.server.restful*.   The root context path
for all the telemetry services is */policy/pdp/engine*.

Features
========

*Features* is an extension mechanism for the PDP-D functionality.
Features can be toggled on and off.
A feature is composed of:

- Java libraries.
- Scripts and configuration files.

Java Extensions
~~~~~~~~~~~~~~~

Additional functionality can be provided in the form of java libraries that hook into the
*PolicyEngine*, *PolicyController*, *DroolsController*, and *PolicySession* interception
points to observe or alter the PDP-D logic.

See the Feature APIs available in the
`management <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/java/org/onap/policy/drools/features>`__
and
`core  <https://git.onap.org/policy/drools-pdp/tree/policy-core/src/main/java/org/onap/policy/drools/core/PolicySessionFeatureApi.java>`__ layers.

The convention used for naming these extension modules are *api-<name>* for interfaces,
and *feature-<name>* for the actual java extensions.

Configuration Items
~~~~~~~~~~~~~~~~~~~

Installation items such as scripts, SQL, maven artifacts, and configuration files.

The reader can refer to the `policy/drools-pdp repository <https://git.onap.org/policy/drools-pdp>`__
and the <https://git.onap.org/policy/drools-applications>`__ repository for miscellaneous feature
implementations.

Layout
""""""

A feature is packaged in a *feature-<name>.zip* and has this internal layout:

.. code-block:: bash

    # #######################################################################################
    # Features Directory Layout:
    #
    # $POLICY_HOME/
    #   L─ features/
    #        L─ <feature-name>*/
    #            L─ [config]/
    #            |   L─ <config-file>+
    #            L─ [bin]/
    #            |   L─ <bin-file>+
    #            L─ lib/
    #            |   L─ [dependencies]/
    #            |   |   L─ <dependent-jar>+
    #            │   L─ feature/
    #            │       L─ <feature-jar>
    #            L─ [db]/
    #            │   L─ <db-name>/+
    #            │       L─ sql/
    #            │           L─ <sql-scripts>*
    #            L─ [artifacts]/
    #                L─ <artifact>+
    #            L─ [install]
    #                L─ [enable]
    #                L─ [disable]
    #                L─ [other-directories-or-files]
    #
    # notes:  [] = optional , * = 0 or more , + = 1 or more
    #   <feature-name> directory without "feature-" prefix.
    #   [config]       feature configuration directory that contains all configuration
    #                  needed for this features
    #   [config]/<config-file>  preferably named with "feature-<feature-name>" prefix to
    #                  precisely match it against the exact features, source code, and
    #                  associated wiki page for configuration details.
    #   [bin]       feature bin directory that contains helper scripts for this feature
    #   [bin]/<executable-file>  preferably named with "feature-<feature-name>" prefix.
    #   lib            jar libraries needed by this features
    #   lib/[dependencies]  3rd party jar dependencies not provided by base installation
    #                  of pdp-d that are necessary for <feature-name> to operate
    #                  correctly.
    #   lib/feature    the single feature jar that implements the feature.
    #   [db]           database directory, if the feature contains sql.
    #   [db]/<db-name> database to which underlying sql scripts should be applied.
    #                  ideally, <db-name> = <feature-name> so it is easily to associate
    #                  the db data with a feature itself.   In addition, since a feature is
    #                  a somewhat independent isolated unit of functionality,the <db-name>
    #                  database ideally isolates all its data.
    #   [db]/<db-name>/sql  directory with all the sql scripts.
    #   [db]/<db-name>/sql/<sql-scripts>  for this feature, sql
    #                  upgrade scripts should be suffixed with ".upgrade.sql"
    #                  and downgrade scripts should be suffixed with ".downgrade.sql"
    #   [artifacts]    maven artifacts to be deployed in a maven repository.
    #   [artifacts]/<artifact>  maven artifact with identifiable maven coordinates embedded
    #                  in the artifact.
    #   [install]      custom installation directory where custom enable or disable scripts
    #                  and other free form data is included to be used for the enable and
    #                  and disable scripts.
    #   [install]/[enable] enable script executed when the enable operation is invoked in
    #                  the feature.
    #   [install]/[disable] disable script executed when the disable operation is invoked in
    #                  the feature.
    #   [install]/[other-directories-or-files] other executables, or data that can be used
    #                  by the feature for any of its operations.   The content is determined
    #                  by the feature designer.
    # ########################################################################################

The `features <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/server-gen/bin/features>`__
is the tool used for administration purposes:

.. code-block:: bash

		Usage:  features status
		            Get enabled/disabled status on all features
		        features enable <feature> ...
		            Enable the specified feature
		        features disable <feature> ...
		            Disable the specified feature
		        features install [ <feature> | <file-name> ] ...
		            Install the specified feature
		        features uninstall <feature> ...
		            Uninstall the specified feature

Features available in the Docker image
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The only enabled feature in the *onap/policy-drools* image is:

- **lifecycle**: enables the lifecycle capability to integrate with the Policy Framework components.

The following features are included in the image but disabled.

- **distributed locking**: distributed resource locking.
- **healthcheck**: basic PDP-D Engine healthcheck.

Healthcheck
"""""""""""

The Healthcheck feature provides reports used to verify the health of *PolicyEngine.manager* in addition to the construction, operation, and deconstruction of HTTP server/client objects.

When enabled, the feature takes as input a properties file named "*feature-healtcheck.properties*.
This file should contain configuration properties necessary for the construction of HTTP client and server objects.

Upon initialization, the feature first constructs HTTP server and client objects using the properties
from its properties file. A healthCheck operation is then triggered. The logic of the healthCheck verifies
that *PolicyEngine.manager* is alive, and iteratively tests each HTTP server object by sending HTTP GET
requests using its respective client object. If a server returns a "200 OK" message, it is marked as "healthy"
in its individual report. Any other return code results in an "unhealthy" report.

After the testing of the server objects has completed, the feature returns a single consolidated report.

Lifecycle
"""""""""

The "lifecycle" feature enables a PDP-D to work with the architectural framework introduced in the
Dublin release.

The lifecycle feature maintains three states: TERMINATED, PASSIVE, and ACTIVE.
The PAP interacts with the lifecycle feature to put a PDP-D in PASSIVE or ACTIVE states.
The PASSIVE state allows for Tosca Operational policies to be deployed.
Policy execution is enabled when the PDP-D transitions to the ACTIVE state.

This feature can coexist side by side with the legacy mode of operation that pre-dates the Dublin release.

Distributed Locking
"""""""""""""""""""

The Distributed Locking Feature provides locking of resources across a pool of PDP-D hosts.
The list of locks is maintained in a database, where each record includes a resource identifier,
an owner identifier, and an expiration time.  Typically, a drools application will unlock the resource
when it's operation completes.  However, if it fails to do so, then the resource will be automatically
released when the lock expires, thus preventing a resource from becoming permanently locked.

Other features
~~~~~~~~~~~~~~

The following features have been contributed to the *policy/drools-pdp* but are either
unnecessary or have not been thoroughly tested:

.. toctree::
   :maxdepth: 1

   feature_activestdbymgmt.rst
   feature_controllerlogging.rst
   feature_eelf.rst
   feature_mdcfilters.rst
   feature_pooling.rst
   feature_sesspersist.rst
   feature_statemgmt.rst
   feature_testtransaction.rst

Data Migration
==============

PDP-D data is migrated across releases with the
`db-migrator <https://git.onap.org/policy/drools-pdp/tree/policy-management/src/main/server-gen/bin/db-migrator>`__.

The migration occurs when different release data is detected. *db-migrator* will look under the
*$POLICY_HOME/etc/db/migration* for databases and SQL scripts to migrate.

.. code-block:: bash

    $POLICY_HOME/etc/db/migration/<schema-name>/sql/<sql-file>

where *<sql-file>* is of the form:

.. code-block:: bash

    <VERSION>-<pdp|feature-name>[-description](.upgrade|.downgrade).sql

The db-migrator tool syntax is

.. code-block:: bash

    syntax: db-migrator
    	 -s <schema-name>
    	 [-b <migration-dir>]
    	 [-f <from-version>]
    	 [-t <target-version>]
    	 -o <operations>

    	 where <operations>=upgrade|downgrade|auto|version|erase|report

    Configuration Options:
    	 -s|--schema|--database:  schema to operate on ('ALL' to apply on all)
    	 -b|--basedir: overrides base DB migration directory
    	 -f|--from: overrides current release version for operations
    	 -t|--target: overrides target release to upgrade/downgrade

    Operations:
    	 upgrade: upgrade operation
    	 downgrade: performs a downgrade operation
    	 auto: autonomous operation, determines upgrade or downgrade
    	 version: returns current version, and in conjunction if '-f' sets the current version
    	 erase: erase all data related <schema> (use with care)
    	 report: migration detailed report on an schema
    	 ok: is the migration status valid

See the
`feature-distributed-locking sql directory <https://git.onap.org/policy/drools-pdp/tree/feature-distributed-locking/src/main/feature/db/pooling/sql>`__
for an example of upgrade/downgrade scripts.

The following command will provide a report on the upgrade or downgrade activies:

.. code-block:: bash

    db-migrator -s ALL -o report

For example in the official frankfurt delivery:

.. code-block:: bash

    policy@dev-drools-0:/tmp/policy-install$ db-migrator -s ALL -o report
    +---------+---------+
    | name    | version |
    +---------+---------+
    | pooling | 1811    |
    +---------+---------+
    +-------------------------------------+-----------+---------+---------------------+
    | script                              | operation | success | atTime              |
    +-------------------------------------+-----------+---------+---------------------+
    | 1804-distributedlocking.upgrade.sql | upgrade   | 1       | 2020-05-22 19:33:09 |
    | 1811-distributedlocking.upgrade.sql | upgrade   | 1       | 2020-05-22 19:33:09 |
    +-------------------------------------+-----------+---------+---------------------+

In order to use the *db-migrator* tool, the system must be configured with a database.

.. code-block:: bash

    SQL_HOST=mariadb

Maven Repositories
==================

The drools libraries in the PDP-D uses maven to fetch rules artifacts and software dependencies.

The default *settings.xml* file specifies the repositories to search.   This configuration
can be overriden with a custom copy that would sit in a mounted configuration
directory.  See an example of the OOM override
`settings.xml <https://git.onap.org/oom/tree/kubernetes/policy/charts/drools/resources/configmaps/settings.xml>`__.

The default ONAP installation of the *control loop* child image *onap/policy-pdpd-cl:1.6.4* is *OFFLINE*.
In this configuration, the *rules* artifact and the *dependencies* retrieves all the artifacts from the local
maven repository.   Of course, this requires that the maven dependencies are preloaded in the local
repository for it to work.

An offline configuration requires two items:

- *OFFLINE* environment variable set to true.
- override *settings.xml* customization, see
  `settings.xml <https://git.onap.org/oom/tree/kubernetes/policy/charts/drools/resources/configmaps/settings.xml>`__.

The default mode in the *onap/policy-drools:1.6.3* is ONLINE instead.

In *ONLINE* mode, the *controller* initialization can take a significant amount of time.

The Policy ONAP installation includes a *nexus* repository component that can be used to host any arbitrary
artifacts that an PDP-D application may require.
The following environment variables configure its location:

.. code-block:: bash

    SNAPSHOT_REPOSITORY_ID=policy-nexus-snapshots
    SNAPSHOT_REPOSITORY_URL=http://nexus:8080/nexus/content/repositories/snapshots/
    RELEASE_REPOSITORY_ID=policy-nexus-releases
    RELEASE_REPOSITORY_URL=http://nexus:8080/nexus/content/repositories/releases/
    REPOSITORY_OFFLINE=false

The *deploy-artifact* tool is used to deploy artifacts to the local or remote maven repositories.
It also allows for dependencies to be installed locally.  The *features* tool invokes it when artifacts are
to be deployed as part of a feature.   The tool can be useful for developers to test a new application
in a container.

.. code-block:: bash

    syntax: deploy-artifact
    	 [-f|-l|-d]
    	 -s <custom-settings>
    	 -a <artifact>

    Options:
    	 -f|--file-repo: deploy in the file repository
    	 -l|--local-repo: install in the local repository
    	 -d|--dependencies: install dependencies in the local repository
    	 -s|--settings: custom settings.xml
    	 -a|--artifact: file artifact (jar or pom) to deploy and/or install

AAF
===

Policy can talk to AAF for authorization requests.   To enable AAF set
the following environment variables:

.. code-block:: bash

    AAF=true
    AAF_NAMESPACE=org.onap.policy
    AAF_HOST=aaf-locate.onap

By default AAF is disabled.

Policy Tool
===========

The *policy* tool can be used to stop, start, and provide status on the PDP-D.

.. code-block:: bash

    syntax: policy [--debug] status|start|stop

The *status* option provides generic status of the system.

.. code-block:: bash

    [drools-pdp-controllers]
     L []: Policy Management (pid 408) is running
    	0 cron jobs installed.

    [features]
    name                   version         status
    ----                   -------         ------
    healthcheck            1.6.3           enabled
    distributed-locking    1.6.3           enabled
    lifecycle              1.6.3           enabled
    controlloop-management 1.6.4           enabled
    controlloop-utils      1.6.4           enabled
    controlloop-trans      1.6.4           enabled
    controlloop-frankfurt  1.6.4           enabled
    controlloop-usecases   1.6.4           disabled

    [migration]
    pooling: OK @ 1811

It contains 3 sections:

- *PDP-D* running status
- *features* applied
- Data migration status on a per database basis.

The *start* and *stop* commands are useful for developers testing functionality on a docker container instance.

Telemetry Shell
===============

*PDP-D* offers an ample set of REST APIs to debug, introspect, and change state on a running PDP-D.   This is known as the
*telemetry* API.   The *telemetry* shell wraps these APIs for shell-like access using
`http-prompt <http://http-prompt.com/>`__.

.. code-block:: bash

    policy@dev-drools-0:~$ telemetry
    Version: 1.0.0
    https://localhost:9696/policy/pdp/engine> get controllers
    HTTP/1.1 200 OK
    Content-Length: 13
    Content-Type: application/json
    Date: Thu, 04 Jun 2020 01:07:38 GMT
    Server: Jetty(9.4.24.v20191120)

    [
        "frankfurt"
    ]

    https://localhost:9696/policy/pdp/engine> exit
    Goodbye!
    policy@dev-drools-0:~$


Other tools
===========

Refer to the *$POLICY_HOME/bin/* directory for additional tooling.

PDP-D Docker Container Configuration
====================================

Both the PDP-D *onap/policy-drools* and *onap/policy-pdpd-cl* images can be used without other components.

There are 2 types of configuration data provided to the container:

1. environment variables.
2. configuration files and shell scripts.

Environment variables
~~~~~~~~~~~~~~~~~~~~~

As it was shown in the *controller* and *endpoint* sections, PDP-D configuration can rely
on environment variables.   In a container environment, these variables are set up by the user
in the host environment.

Configuration Files and Shell Scripts
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

PDP-D is very flexible in its configuration.

The following file types are recognized when mounted under */tmp/policy-install/config*.

These are the configuration items that can reside externally and override the default configuration:

- **settings.xml** if working with external nexus repositories.
- **standalone-settings.xml** if an external *policy* nexus repository is not available.
- ***.conf** files containing environment variables.   This is an alternative to use environment variables,
  as these files will be sourced in before the PDP-D starts.
- **features*.zip** to load any arbitrary feature not present in the image.
- ***.pre.sh** scripts that will be executed before the PDP-D starts.
- ***.post.sh** scripts that will be executed after the PDP-D starts.
- **policy-keystore** to override the default PDP-D java keystore.
- **policy-truststore** to override the default PDP-D java truststore.
- **aaf-cadi.keyfile** to override the default AAF CADI Key generated by AAF.
- ***.properties** to override or add any properties file for the PDP-D, this includes *controller*, *endpoint*,
  *engine* or *system* configurations.
- **logback*.xml** to override the default logging configuration.
- ***.xml** to override other .xml configuration that may be used for example by an *application*.
- ***.json** *json* configuration that may be used by an *application*.


Running PDP-D with a single container
=====================================

Environment File
~~~~~~~~~~~~~~~~

First create an environment file (in this example *env.conf*) to configure the PDP-D.

.. code-block:: bash

    # SYSTEM software configuration

    POLICY_HOME=/opt/app/policy
    POLICY_LOGS=/var/log/onap/policy/pdpd
    KEYSTORE_PASSWD=Pol1cy_0nap
    TRUSTSTORE_PASSWD=Pol1cy_0nap

    # Telemetry credentials

    TELEMETRY_PORT=9696
    TELEMETRY_HOST=0.0.0.0
    TELEMETRY_USER=demo@people.osaaf.org
    TELEMETRY_PASSWORD=demo123456!

    # nexus repository

    SNAPSHOT_REPOSITORY_ID=
    SNAPSHOT_REPOSITORY_URL=
    RELEASE_REPOSITORY_ID=
    RELEASE_REPOSITORY_URL=
    REPOSITORY_USERNAME=
    REPOSITORY_PASSWORD=
    REPOSITORY_OFFLINE=true

    # Relational (SQL) DB access

    SQL_HOST=
    SQL_USER=
    SQL_PASSWORD=

    # AAF

    AAF=false
    AAF_NAMESPACE=org.onap.policy
    AAF_HOST=aaf.api.simpledemo.onap.org

    # PDP-D DMaaP configuration channel

    PDPD_CONFIGURATION_TOPIC=PDPD-CONFIGURATION
    PDPD_CONFIGURATION_API_KEY=
    PDPD_CONFIGURATION_API_SECRET=
    PDPD_CONFIGURATION_CONSUMER_GROUP=
    PDPD_CONFIGURATION_CONSUMER_INSTANCE=
    PDPD_CONFIGURATION_PARTITION_KEY=

    # PAP-PDP configuration channel

    POLICY_PDP_PAP_TOPIC=POLICY-PDP-PAP
    POLICY_PDP_PAP_API_KEY=
    POLICY_PDP_PAP_API_SECRET=

    # DMaaP

    DMAAP_SERVERS=localhost

Note that *SQL_HOST*, and *REPOSITORY* are empty, so the PDP-D does not attempt
to integrate with those components.

Configuration
~~~~~~~~~~~~~

In order to avoid the noise in the logs that relate to dmaap configuration, a startup script (*noop.pre.sh*) is added
to convert *dmaap* endpoints to *noop* in the host directory to be mounted.

noop.pre.sh
"""""""""""

.. code-block:: bash

    #!/bin/bash -x

    sed -i "s/^dmaap/noop/g" $POLICY_HOME/config/*.properties


active.post.sh
""""""""""""""

To put the controller directly in active mode at initialization, place an *active.post.sh* script under the
mounted host directory:

.. code-block:: bash

    #!/bin/bash -x

    bash -c "http --verify=no -a ${TELEMETRY_USER}:${TELEMETRY_PASSWORD} PUT https://localhost:9696/policy/pdp/engine/lifecycle/state/ACTIVE"

Bring up the PDP-D
~~~~~~~~~~~~~~~~~~

.. code-block:: bash

    docker run --rm -p 9696:9696 -v ${PWD}/config:/tmp/policy-install/config --env-file ${PWD}/env/env.conf -it --name PDPD -h pdpd nexus3.onap.org:10001/onap/policy-drools:1.6.3

To run the container in detached mode, add the *-d* flag.

Note that in this command, we are opening the *9696* telemetry API port to the outside world, the config directory
(where the *noop.pre.sh* customization script resides) is mounted as /tmp/policy-install/config,
and the customization environment variables (*env/env.conf*) are passed into the container.

To open a shell into the PDP-D:

.. code-block:: bash

    docker exec -it pdp-d bash

Once in the container, run tools such as *telemetry*, *db-migrator*, *policy* to look at the system state:

To run the *telemetry shell* and other tools from the host:

.. code-block:: bash

    docker exec -it PDPD bash -c "/opt/app/policy/bin/telemetry"
    docker exec -it PDPD bash -c "/opt/app/policy/bin/policy status"
    docker exec -it PDPD bash -c "/opt/app/policy/bin/db-migrator -s ALL -o report"

Controlled instantiation of the PDP-D
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sometimes a developer may want to start and stop the PDP-D manually:

.. code-block:: bash

   # start a bash

   docker run --rm -p 9696:9696 -v ${PWD}/config:/tmp/policy-install/config --env-file ${PWD}/env/env.conf -it --name PDPD -h pdpd nexus3.onap.org:10001/onap/policy-drools:1.6.3 bash

   # use this command to start policy applying host customizations from /tmp/policy-install/config

   pdpd-entrypoint.sh vmboot

   # or use this command to start policy without host customization

   policy start

   # at any time use the following command to stop the PDP-D

   policy stop

   # and this command to start the PDP-D back again

   policy start

Running PDP-D with nexus and mariadb
====================================

*docker-compose* can be used to test the PDP-D with other components.   This is an example configuration
that brings up *nexus*, *mariadb* and the PDP-D (*docker-compose-pdp.yml*)

docker-compose-pdp.yml
~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: bash

    version: '3'
    services:
       mariadb:
          image: mariadb:10.2.25
          container_name: mariadb
          hostname: mariadb
          command: ['--lower-case-table-names=1', '--wait_timeout=28800']
          env_file:
             - ${PWD}/db/db.conf
          volumes:
             - ${PWD}/db:/docker-entrypoint-initdb.d
          ports:
             - "3306:3306"
       nexus:
          image: sonatype/nexus:2.14.8-01
          container_name: nexus
          hostname: nexus
          ports:
             - "8081:8081"
       drools:
          image: nexus3.onap.org:10001/onap/policy-drools:1.6.3
          container_name: drools
          depends_on:
             - mariadb
             - nexus
          hostname: drools
          ports:
             - "9696:9696"
          volumes:
             - ${PWD}/config:/tmp/policy-install/config
          env_file:
             - ${PWD}/env/env.conf

with *${PWD}/db/db.conf*:

db.conf
~~~~~~~

.. code-block:: bash

    MYSQL_ROOT_PASSWORD=secret
    MYSQL_USER=policy_user
    MYSQL_PASSWORD=policy_user

and *${PWD}/db/db.sh*:

db.sh
~~~~~

.. code-block:: bash

    for db in support onap_sdk log migration operationshistory10 pooling policyadmin operationshistory
    do
    	mysql -uroot -p"${MYSQL_ROOT_PASSWORD}" --execute "CREATE DATABASE IF NOT EXISTS ${db};"
    	mysql -uroot -p"${MYSQL_ROOT_PASSWORD}" --execute "GRANT ALL PRIVILEGES ON \`${db}\`.* TO '${MYSQL_USER}'@'%' ;"
    done

    mysql -uroot -p"${MYSQL_ROOT_PASSWORD}" --execute "FLUSH PRIVILEGES;"

env.conf
~~~~~~~~

The environment file *env/env.conf* for *PDP-D* can be set up with appropriate variables to point to the *nexus* instance
and the *mariadb* database:

.. code-block:: bash

    # SYSTEM software configuration

    POLICY_HOME=/opt/app/policy
    POLICY_LOGS=/var/log/onap/policy/pdpd
    KEYSTORE_PASSWD=Pol1cy_0nap
    TRUSTSTORE_PASSWD=Pol1cy_0nap

    # Telemetry credentials

    TELEMETRY_PORT=9696
    TELEMETRY_HOST=0.0.0.0
    TELEMETRY_USER=demo@people.osaaf.org
    TELEMETRY_PASSWORD=demo123456!

    # nexus repository

    SNAPSHOT_REPOSITORY_ID=policy-nexus-snapshots
    SNAPSHOT_REPOSITORY_URL=http://nexus:8081/nexus/content/repositories/snapshots/
    RELEASE_REPOSITORY_ID=policy-nexus-releases
    RELEASE_REPOSITORY_URL=http://nexus:8081/nexus/content/repositories/releases/
    REPOSITORY_USERNAME=admin
    REPOSITORY_PASSWORD=admin123
    REPOSITORY_OFFLINE=false

    MVN_SNAPSHOT_REPO_URL=https://nexus.onap.org/content/repositories/snapshots/
    MVN_RELEASE_REPO_URL=https://nexus.onap.org/content/repositories/releases/

    # Relational (SQL) DB access

    SQL_HOST=mariadb
    SQL_USER=policy_user
    SQL_PASSWORD=policy_user

    # AAF

    AAF=false
    AAF_NAMESPACE=org.onap.policy
    AAF_HOST=aaf.api.simpledemo.onap.org

    # PDP-D DMaaP configuration channel

    PDPD_CONFIGURATION_TOPIC=PDPD-CONFIGURATION
    PDPD_CONFIGURATION_API_KEY=
    PDPD_CONFIGURATION_API_SECRET=
    PDPD_CONFIGURATION_CONSUMER_GROUP=
    PDPD_CONFIGURATION_CONSUMER_INSTANCE=
    PDPD_CONFIGURATION_PARTITION_KEY=

    # PAP-PDP configuration channel

    POLICY_PDP_PAP_TOPIC=POLICY-PDP-PAP
    POLICY_PDP_PAP_API_KEY=
    POLICY_PDP_PAP_API_SECRET=

    # DMaaP

    DMAAP_SERVERS=localhost

prepare.pre.sh
~~~~~~~~~~~~~~

A pre-start script *config/prepare.pres.sh"can be added the custom config directory
to prepare the PDP-D to activate the distributed-locking feature (using the database)
and to use "noop" topics instead of *dmaap* topics:

.. code-block:: bash

    #!/bin/bash

    bash -c "/opt/app/policy/bin/features enable distributed-locking"
    sed -i "s/^dmaap/noop/g" $POLICY_HOME/config/*.properties

active.post.sh
~~~~~~~~~~~~~~

A post-start script *config/active.post.sh* can place PDP-D in *active* mode at initialization:

    .. code-block:: bash

    bash -c "http --verify=no -a ${TELEMETRY_USER}:${TELEMETRY_PASSWORD} PUT https://localhost:9696/policy/pdp/engine/lifecycle/state/ACTIVE"

Bring up the PDP-D, nexus, and mariadb
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To bring up the containers:

.. code-block:: bash

    docker-compose -f docker-compose-pdpd.yaml up -d

To take it down:

.. code-block:: bash

    docker-compose -f docker-compose-pdpd.yaml down -v

Other examples
~~~~~~~~~~~~~~

The reader can also look at the `integration/csit repository <https://git.onap.org/integration/csit>`__.
More specifically, these directories have examples of other PDP-D configurations:

* `plans <https://git.onap.org/integration/csit/tree/plans/policy/drools-pdp>`__: startup scripts.
* `scripts <https://git.onap.org/integration/csit/tree/scripts/policy/docker-compose-drools.yml>`__: docker-compose related files.
* `plans <https://git.onap.org/integration/csit/tree/tests/policy/drools-pdp>`__: test plan.

Configuring the PDP-D in an OOM Kubernetes installation
=======================================================

The `PDP-D OOM chart <https://git.onap.org/oom/tree/kubernetes/policy/charts/drools>`__ can be
customized at the following locations:

* `values.yaml <https://git.onap.org/oom/tree/kubernetes/policy/charts/drools/values.yaml>`__: custom values for your installation.
* `configmaps <https://git.onap.org/oom/tree/kubernetes/policy/charts/drools/resources/configmaps>`__: place in this directory any configuration extensions or overrides to customize the PDP-D that does not contain sensitive information.
* `secrets <https://git.onap.org/oom/tree/kubernetes/policy/charts/drools/resources/secrets>`__: place in this directory any configuration extensions or overrides to customize the PDP-D that does contain sensitive information.

The same customization techniques described in the docker sections for PDP-D, fully apply here, by placing the corresponding
files or scripts in these two directories.

Additional information
======================

For additional information, please see the
`Drools PDP Development and Testing (In Depth) <https://wiki.onap.org/display/DW/2020+Frankfurt+Tutorials>`__ page.