cfd0ab6104f2ba5b78fbb33e5e99e42f29f2b85b
[policy/parent.git] / docs / development / devtools / devtools.rst
1 .. This work is licensed under a
2 .. Creative Commons Attribution 4.0 International License.
3 .. http://creativecommons.org/licenses/by/4.0
4
5 .. _policy-development-tools-label:
6
7 Policy Platform Development Tools
8 #################################
9
10 .. contents::
11     :depth: 3
12
13
14 This article explains how to build the ONAP Policy Framework for development purposes and how to run stability/performance tests for a variety of components. To start, the developer should consult the latest ONAP Wiki to familiarize themselves with developer best practices and how-tos to setup their environment, see `https://wiki.onap.org/display/DW/Developer+Best+Practices`.
15
16
17 This article assumes that:
18
19 * You are using a *\*nix* operating system such as linux or macOS.
20 * You are using a directory called *git* off your home directory *(~/git)* for your git repositories
21 * Your local maven repository is in the location *~/.m2/repository*
22 * You have copied the settings.xml from oparent to *~/.m2/* directory
23 * You have added settings to access the ONAP Nexus to your M2 configuration, see `Maven Settings Example <https://wiki.onap.org/display/DW/Setting+Up+Your+Development+Environment>`_ (bottom of the linked page)
24
25 The procedure documented in this article has been verified to work on a MacBook laptop running macOS Mojave Version 10.14.6 and an Ubuntu 18.06 VM.
26
27 Cloning All The Policy Repositories
28 ***********************************
29
30 Run a script such as the script below to clone the required modules from the `ONAP git repository <https://gerrit.onap.org/r/admin/repos/q/filter:policy>`_. This script clones all the ONAP Policy Framework repositories.
31
32 ONAP Policy Framework has dependencies to the ONAP Parent *oparent* module, the ONAP ECOMP SDK *ecompsdkos* module, and the A&AI Schema module.
33
34
35 .. code-block:: bash
36    :caption: Typical ONAP Policy Framework Clone Script
37    :linenos:
38
39     #!/usr/bin/env bash
40
41     ## script name for output
42     MOD_SCRIPT_NAME=`basename $0`
43
44     ## the ONAP clone directory, defaults to "onap"
45     clone_dir="onap"
46
47     ## the ONAP repos to clone
48     onap_repos="\
49     policy/parent \
50     policy/common \
51     policy/models \
52     policy/docker \
53     policy/api \
54     policy/pap \
55     policy/apex-pdp \
56     policy/drools-pdp \
57     policy/drools-applications \
58     policy/xacml-pdp \
59     policy/distribution \
60     policy/gui \
61     policy/clamp "
62
63     ##
64     ## Help screen and exit condition (i.e. too few arguments)
65     ##
66     Help()
67     {
68         echo ""
69         echo "$MOD_SCRIPT_NAME - clones all required ONAP git repositories"
70         echo ""
71         echo "       Usage:  $MOD_SCRIPT_NAME [-options]"
72         echo ""
73         echo "       Options"
74         echo "         -d          - the ONAP clone directory, defaults to '.'"
75         echo "         -h          - this help screen"
76         echo ""
77         exit 255;
78     }
79
80     ##
81     ## read command line
82     ##
83     while [ $# -gt 0 ]
84     do
85         case $1 in
86             #-d ONAP clone directory
87             -d)
88                 shift
89                 if [ -z "$1" ]; then
90                     echo "$MOD_SCRIPT_NAME: no clone directory"
91                     exit 1
92                 fi
93                 clone_dir=$1
94                 shift
95             ;;
96
97             #-h prints help and exists
98             -h)
99                 Help;exit 0;;
100
101             *)    echo "$MOD_SCRIPT_NAME: undefined CLI option - $1"; exit 255;;
102         esac
103     done
104
105     if [ -f "$clone_dir" ]; then
106         echo "$MOD_SCRIPT_NAME: requested clone directory '$clone_dir' exists as file"
107         exit 2
108     fi
109     if [ -d "$clone_dir" ]; then
110         echo "$MOD_SCRIPT_NAME: requested clone directory '$clone_dir' exists as directory"
111         exit 2
112     fi
113
114     mkdir $clone_dir
115     if [ $? != 0 ]
116     then
117         echo cannot clone ONAP repositories, could not create directory '"'$clone_dir'"'
118         exit 3
119     fi
120
121     for repo in $onap_repos
122     do
123         repoDir=`dirname "$repo"`
124         repoName=`basename "$repo"`
125
126         if [ ! -z $dirName ]
127         then
128             mkdir "$clone_dir/$repoDir"
129             if [ $? != 0 ]
130             then
131                 echo cannot clone ONAP repositories, could not create directory '"'$clone_dir/repoDir'"'
132                 exit 4
133             fi
134         fi
135
136         git clone https://gerrit.onap.org/r/${repo} $clone_dir/$repo
137     done
138
139     echo ONAP has been cloned into '"'$clone_dir'"'
140
141
142 Execution of the script above results in the following directory hierarchy in your *~/git* directory:
143
144     *  ~/git/onap
145     *  ~/git/onap/policy
146     *  ~/git/onap/policy/parent
147     *  ~/git/onap/policy/common
148     *  ~/git/onap/policy/models
149     *  ~/git/onap/policy/api
150     *  ~/git/onap/policy/pap
151     *  ~/git/onap/policy/gui
152     *  ~/git/onap/policy/docker
153     *  ~/git/onap/policy/drools-applications
154     *  ~/git/onap/policy/drools-pdp
155     *  ~/git/onap/policy/clamp
156     *  ~/git/onap/policy/apex-pdp
157     *  ~/git/onap/policy/xacml-pdp
158     *  ~/git/onap/policy/distribution
159
160
161 Building ONAP Policy Framework Components
162 *****************************************
163
164 **Step 1:** Optionally, for a completely clean build, remove the ONAP built modules from your local repository.
165
166     .. code-block:: bash
167
168         rm -fr ~/.m2/repository/org/onap
169
170
171 **Step 2:**  A pom such as the one below can be used to build the ONAP Policy Framework modules. Create the *pom.xml* file in the directory *~/git/onap/policy*.
172
173 .. code-block:: xml
174    :caption: Typical pom.xml to build the ONAP Policy Framework
175    :linenos:
176
177     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
178         <modelVersion>4.0.0</modelVersion>
179         <groupId>org.onap</groupId>
180         <artifactId>onap-policy</artifactId>
181         <version>1.0.0-SNAPSHOT</version>
182         <packaging>pom</packaging>
183         <name>${project.artifactId}</name>
184         <inceptionYear>2017</inceptionYear>
185         <organization>
186             <name>ONAP</name>
187         </organization>
188
189         <modules>
190             <module>parent</module>
191             <module>common</module>
192             <module>models</module>
193             <module>api</module>
194             <module>pap</module>
195             <module>apex-pdp</module>
196             <module>xacml-pdp</module>
197             <module>drools-pdp</module>
198             <module>drools-applications</module>
199             <module>distribution</module>
200             <module>gui</module>
201             <module>clamp</module>
202         </modules>
203     </project>
204
205 **Policy Architecture/API Transition**
206
207 In Dublin, a new Policy Architecture was introduced. The legacy architecture runs in parallel with the new architecture. It will be deprecated after Frankfurt release.
208 If the developer is only interested in working with the new architecture components, the engine sub-module can be ommitted.
209
210
211 **Step 3:** You can now build the Policy framework.
212
213 Java artifacts only:
214
215     .. code-block:: bash
216
217        cd ~/git/onap
218        mvn clean install
219
220 With docker images:
221
222     .. code-block:: bash
223
224        cd ~/git/onap
225        mvn clean install -P docker
226
227 Developing and Debugging each Policy Component
228 **********************************************
229
230 Running a MariaDb Instance
231 ++++++++++++++++++++++++++
232
233 The Policy Framework requires a MariaDb instance running. The easiest way to do this is to run a docker image locally.
234
235 One example on how to do this is to use the scripts used by the policy/api S3P tests.
236
237 `Simulator Setup Script Example <https://gerrit.onap.org/r/gitweb?p=policy/api.git;a=tree;f=testsuites/stability/src/main/resources/simulatorsetup;h=9038413f67cff2e2a79d6345f198f96ee0c57de1;hb=refs/heads/master>`_
238
239     .. code-block:: bash
240
241        cd ~/git/onap/api/testsuites/stability/src/main/resources/simulatorsetup
242        ./setup_components.sh
243
244 Another example on how to run the MariaDb is using the docker compose file used by the Policy API CSITs:
245
246 `Example Compose Script to run MariaDB <https://gerrit.onap.org/r/gitweb?p=integration/csit.git;a=blob;f=scripts/policy/docker-compose-api.yml;h=e32190f1e6cb6d9b64ddf53a2db2c746723a0c6a;hb=refs/heads/master>`_
247
248 Running the API component standalone
249 ++++++++++++++++++++++++++++++++++++
250
251 Assuming you have successfully built the codebase using the instructions above. The only requirement for the API component to run is a
252 running MariaDb database instance. The easiest way to do this is to run the docker image, please see the mariadb documentation for the latest
253 information on doing so. Once the mariadb is up and running, a configuration file must be provided to the api in order for it to know how to
254 connect to the mariadb. You can locate the default configuration file in the packaging of the api component:
255
256 `Default Policy API Configuration <https://gerrit.onap.org/r/gitweb?p=policy/api.git;a=blob;f=packages/policy-api-tarball/src/main/resources/etc/apiParameters.yaml;h=2c19199a8a889cb0ab203334182662fe15e1635e;hb=refs/heads/master>`_
257
258 You will want to change the fields pertaining to "host", "port" and "databaseUrl" to your local environment settings and start the
259 policy-api springboot application either using your IDE of choice or using the run goal from Spring Boot Maven plugin: *mvn spring-boot:run*.
260
261 Running the API component using Docker Compose
262 ++++++++++++++++++++++++++++++++++++++++++++++
263
264 An example of running the api using a docker compose script is located in the Policy Integration CSIT test repository.
265
266 `Policy CSIT API Docker Compose <https://gerrit.onap.org/r/gitweb?p=integration/csit.git;a=blob;f=scripts/policy/docker-compose-api.yml;h=e32190f1e6cb6d9b64ddf53a2db2c746723a0c6a;hb=refs/heads/master>`_
267
268 Running the PAP component standalone
269 ++++++++++++++++++++++++++++++++++++
270
271 Once you have successfully built the PAP codebase, a running MariaDb database and DMaaP instance will also be required to start up the application.
272 For MariaDb instance, the easiest way is to run the docker image, please see the mariadb documentation for the latest
273 information on doing so. For DMaaP, the easiest way during development is to run the DMaaP simulator which is explained in the below sections.
274 Once the mariadb and DMaaP are running, a configuration file must be provided to the PAP component in order for it to know how to
275 connect to the mariadb and DMaaP along with other relevant configuration details. You can locate the default configuration file in the packaging of the PAP component:
276
277 `Default PAP Configuration <https://gerrit.onap.org/r/gitweb?p=policy/pap.git;a=blob;f=packages/policy-pap-tarball/src/main/resources/etc/papParameters.yaml;h=06dd45f4946fd0a11ed8ef859f8fc5bcf409a3f0;hb=HEAD>`_
278
279 Update the fields related to MariaDB, DMaaP and the RestServer for the application as per your local environment settings.
280 Then to start the application, just run the Spring Boot application using IDE or command line.
281
282 Running the Smoke Tests
283 ***********************
284
285 The following links contain instructions on how to run the smoke tests. These may be helpful to developers to become
286 familiar with the Policy Framework components and test any local changes.
287
288 .. toctree::
289    :maxdepth: 1
290
291    policy-gui-acm-smoke.rst
292    db-migrator-smoke.rst
293    acm-participants-smoke.rst
294    clamp-smoke.rst
295    clamp-cl-participant-protocol-smoke.rst
296    policy-participant-smoke.rst
297    api-smoke.rst
298    pap-smoke.rst
299    apex-smoke.rst
300    drools-smoke.rst
301    xacml-smoke.rst
302    distribution-smoke.rst
303
304
305 Running the Stability/Performance Tests
306 ***************************************
307
308 The following links contain instructions on how to run the S3P Stability and Performance tests. These may be helpful to developers to become
309 familiar with the Policy Framework components and test any local changes.
310
311 .. toctree::
312    :maxdepth: 1
313
314    api-s3p.rst
315    pap-s3p.rst
316    apex-s3p.rst
317    drools-s3p.rst
318    xacml-s3p.rst
319    distribution-s3p.rst
320    clamp-s3p.rst
321
322 Running the Pairwise Tests
323 **************************
324
325 The following links contain instructions on how to run the pairwise tests. These may be helpful to developers check that
326 the Policy Framework works in a full ONAP deployment.
327
328 .. toctree::
329    :maxdepth: 1
330
331    clamp-policy.rst
332
333    clamp-dcae.rst
334
335    policy-cds.rst
336
337    clamp-sdc.rst
338
339 ..
340    api-pairwise.rst
341
342 ..
343    pap-pairwise.rst
344
345 ..
346    apex-pairwise.rst
347
348 ..
349    drools-pairwise.rst
350
351 ..
352    xacml-pairwise.rst
353
354 ..
355    distribution-pairwise.rst
356
357
358 Testing OpenSuse docker images
359 ******************************
360
361 Policy Framework offers docker images in two flavors: Alpine and OpenSuse.
362 Alpine images are used in OOM for ONAP deployments.
363 The OpenSuse images are built manually if needed, by running Maven with the -Pdockersuse profile.
364 To test these images, CSITs will be run.
365
366 1. Build the OpenSuse image you want by running Maven with -Pdockersuse:
367
368     .. code-block:: bash
369
370         cd policy/apex-pdp
371         mvn clean install -Pdockersuse
372
373     The image onap/policy-apex-pdp:latest will be produced.
374
375 2. To avoid ambiguity, tag the image as opensuse:
376
377     .. code-block:: bash
378
379         docker tag onap/policy-apex-pdp:latest onap/policy-apex-pdp:opensuse
380
381 3. Clone policy/docker repo.
382
383 4. Modify docker/csit/docker-compose-all.yml to use the tagged OpenSuse image.
384
385     Replace:
386
387     .. code-block:: yaml
388
389         apex-pdp:
390           image: nexus3.onap.org:10001/onap/policy-apex-pdp:${POLICY_APEX_PDP_VERSION}
391
392     with:
393
394     .. code-block:: yaml
395
396         apex-pdp:
397           image: onap/policy-apex-pdp:opensuse
398
399 5. Run the project CSIT. For apex-pdp:
400
401     .. code-block:: bash
402
403         cd docker/csit
404         ./run-project-csit.sh apex-pdp
405
406     Automated tests will be run, and log files displayed.
407
408
409 Generating Swagger Documentation
410 ********************************
411
412 1. Using Swagger2Markup maven plugin from Policy Parent Integration POM
413 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
414
415 The `Policy Parent Integration POM <https://github.com/onap/policy-parent/blob/master/integration/pom.xml>`_ contains a *generateSwaggerDocs* profile. This
416 profile can be activated on any module that has a Swagger endpoint. When active, this profile creates a tarball in Nexus with the name
417 *<project-artifactId>-swagger-docs.tar.gz*. The tarball contains the following files:
418
419 .. code-block:: bash
420
421     swagger/swagger.html
422     swagger/swagger.json
423     swagger/swagger.pdf
424
425 The profile is activated when:
426
427 1. The following property is defined at the top of the *pom.xml* file for a module
428
429     .. code-block:: bash
430
431         <!--  This property triggers generation of the Swagger documents -->
432         <swagger.generation.phase>post-integration-test</swagger.generation.phase>
433
434     See the `CLAMP runtime POM <https://github.com/onap/policy-clamp/blob/master/runtime/pom.xml>`_ for an example of the usage of this property.
435
436 2. Unit tests are being executed in the build, in other words when the *skipTests* flag is *false*.
437
438 You **must** create a unit test in your module that generates the following file:
439
440 .. code-block:: bash
441
442     src/test/resources/swagger/swagger.json
443
444 Typically, you do this by starting your REST endpoint in a unit test, issuing a REST call to get the Swagger API documentation. The test case below is an example
445 of such a test case.
446
447 .. code-block:: java
448
449    @Test
450    public void testSwaggerJson() throws Exception {
451        ResponseEntity<String> httpsEntity = getRestTemplate()
452                .getForEntity("https://localhost:" + this.httpsPort + "/restservices/clds/api-doc", String.class);
453        assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
454        assertThat(httpsEntity.getBody()).contains("swagger");
455        FileUtils.writeStringToFile(new File("target/swagger/swagger.json"), httpsEntity.getBody(),
456                Charset.defaultCharset());
457    }
458
459 2. Accessing Swagger documentation for springboot based policy applications
460 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
461
462 Springfox Swagger2 maven dependency aids with auto-generation of Swagger documentation.
463
464 Using the Swagger-UI maven dependency Swagger HTML documentation can be accessed at the root url.
465
466 - The generated swagger.json can be accessed at: *https://service_IP:service_port/v2/api-docs*
467 - Swagger UI can be accessed at: *https://service_IP:service_port/swagger-ui/index.html*
468
469 Running the DMaaP Simulator during Development
470 **********************************************
471 It is sometimes convenient to run the DMaaP simulator during development. You can run it from the command line using Maven or from within your IDE.
472
473 Running on the Command Line
474 +++++++++++++++++++++++++++
475 1. Check out the policy models repository
476 2. Go to the *models-sim/policy-models-simulators* subdirectory in the policy-models repo
477 3. Run the following Maven command:
478
479    .. code-block:: bash
480
481       mvn exec:java  -Dexec.mainClass=org.onap.policy.models.simulators.Main -Dexec.args="src/test/resources/simParameters.json"
482
483 Running in Eclipse
484 ++++++++++++++++++
485 1. Check out the policy models repository
486 2. Go to the *models-sim/policy-models-simulators* module in the policy-models repo
487 3. Specify a run configuration using the class *org.onap.policy.models.simulators.Main* as the main class
488 4. Specify an argument of *src/test/resources/simParameters.json* to the run configuration
489 5. Run the configuration
490
491 Specifying a local configuration file
492 +++++++++++++++++++++++++++++++++++++
493
494 You may specify a local configuration file instead of *src/test/resources/simParameters.json* on the command line or as an argument in the run configuration in eclipse:
495
496 .. code-block:: json
497
498    {
499      "dmaapProvider": {
500        "name": "DMaaP simulator",
501        "topicSweepSec": 900
502      },
503      "restServers": [
504        {
505          "name": "DMaaP simulator",
506          "providerClass": "org.onap.policy.models.sim.dmaap.rest.DmaapSimRestControllerV1",
507          "host": "localhost",
508          "port": 3904,
509          "https": false
510        }
511      ]
512    }
513
514 Bringing up Strimzi-Kafka Deploment with Policy Framework
515 *********************************************************
516
517 This page will explain how to setup a local Kubernetes cluster and minimal helm setup to run and deploy Policy Framework on a single host.
518
519 This is meant for a development purpose only as we are going to use microk8s in this page
520
521 .. toctree::
522    :maxdepth: 1
523
524    strimzi-policy.rst