Merge "Remove bad dependeny in apex POM"
diff --git a/packages/apex-pdp-docker/src/main/docker/Dockerfile b/packages/apex-pdp-docker/src/main/docker/Dockerfile
index b5c6bc9..1c47682 100644
--- a/packages/apex-pdp-docker/src/main/docker/Dockerfile
+++ b/packages/apex-pdp-docker/src/main/docker/Dockerfile
@@ -23,7 +23,7 @@
 #
 # Docker file to build an image that runs APEX on Java 11 or better in alpine
 #
-FROM onap/policy-jdk-alpine:2.4.0
+FROM onap/policy-jdk-alpine:2.4.1
 
 LABEL maintainer="Policy Team"
 
diff --git a/pom.xml b/pom.xml
index 02dd807..1a4fc5e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -27,7 +27,7 @@
     <parent>
         <groupId>org.onap.policy.parent</groupId>
         <artifactId>integration</artifactId>
-        <version>3.5.1-SNAPSHOT</version>
+        <version>3.5.1</version>
         <relativePath />
     </parent>
 
@@ -45,8 +45,8 @@
         <version.commons-cli>1.4</version.commons-cli>
         <version.kafka>3.0.0</version.kafka>
         <version.hibernate>5.3.7.Final</version.hibernate>
-        <version.policy.common>1.10.1-SNAPSHOT</version.policy.common>
-        <version.policy.models>2.6.1-SNAPSHOT</version.policy.models>
+        <version.policy.common>1.10.1</version.policy.common>
+        <version.policy.models>2.6.1</version.policy.models>
         <version.jgroups>4.1.5.Final</version.jgroups>
         <version.caffeine>2.8.0</version.caffeine>
         <sonar.javascript.lcov.reportPaths>${project.basedir}/target/code-coverage/lcov.info</sonar.javascript.lcov.reportPaths>
diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java
index 2e97c54..72cbd28 100644
--- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java
+++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java
@@ -29,7 +29,9 @@
 import com.google.gson.JsonObject;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 import org.onap.policy.apex.core.engine.EngineParameters;
 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
@@ -63,6 +65,7 @@
 
     private static final String POLICY_TYPE_IMPL = "policy_type_impl";
     private static final String APEX_POLICY_MODEL = "apexPolicyModel";
+    private static final String METADATA_SET = "metadataSet";
 
     private String policyModel;
     private String apexConfig;
@@ -171,14 +174,28 @@
                 apexConfigJsonObject.add(property.getKey(), body);
             }
             apexConfig = standardCoder.encode(apexConfigJsonObject);
+
+            // populate policyModel from metadata if present
+            Optional<Map<String, Object>> metadata =
+                Optional.ofNullable(toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0)
+                    .entrySet().iterator().next().getValue().getMetadata());
+            if (metadata.isPresent() && metadata.get().containsKey(METADATA_SET)) {
+                JsonElement body = standardCoder.convert(metadata.get(), JsonObject.class);
+                policyModel = extractPolicyModel(standardCoder, body);
+            }
         } catch (Exception e) {
             throw new ApexException("Parsing config and model from the tosca policy failed.", e);
         }
     }
 
     private String extractPolicyModel(StandardCoder standardCoder, JsonElement body) throws CoderException {
-        // Check for "policy_type_impl"
-        JsonElement policyTypeImplObject = ((JsonObject) body).get(POLICY_TYPE_IMPL);
+        JsonElement policyTypeImplObject = null;
+        // Check for "policy_type_impl, if not present check for "metadataSet"
+        if (body.getAsJsonObject().has(POLICY_TYPE_IMPL)) {
+            policyTypeImplObject = ((JsonObject) body).get(POLICY_TYPE_IMPL);
+        } else if (body.getAsJsonObject().has(METADATA_SET))  {
+            policyTypeImplObject = ((JsonObject) body).get(METADATA_SET);
+        }
         if (null == policyTypeImplObject) {
             return null;
         }
diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ApexParametersTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ApexParametersTest.java
index a0169d7..dd76374 100644
--- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ApexParametersTest.java
+++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ApexParametersTest.java
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2020,2022 Nordix Foundation.
  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
@@ -23,6 +23,7 @@
 
 package org.onap.policy.apex.service.engine.parameters;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -79,6 +80,51 @@
     }
 
     @Test
+    public void testPolicyModelFromMetadata() throws ParameterException {
+        // Policy Models provided only in metadata.
+        final String[] args = {"-p", "src/test/resources/parameters/policyModelFromMetadata.json"};
+        final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
+
+        ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
+
+        assertThat(parameters.getEngineServiceParameters().getPolicyModel()).isNotEmpty();
+        assertThat(parameters.getEngineServiceParameters().getPolicyModel())
+            .contains("{\"key\":{\"name\":\"dummy key1 provided in metadata\",\"version\":\"0.0.1\"},\"keyInformation\""
+                + ":{\"key\":{\"name\":\"dummy key2 provided in metadata\",\"version\":\"0.0.1\"}},"
+                + "\"threshold\":3.15,\"state\":\"passive\"}");
+    }
+
+    @Test
+    public void testPolicyModelFromProperties() throws ParameterException {
+        // Policy models provided in properties under EngineServiceParameters for backward compatibility
+        final String[] args = {"-p", "src/test/resources/parameters/policyModelFromProperties.json"};
+        final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
+
+        ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
+
+        assertThat(parameters.getEngineServiceParameters().getPolicyModel()).isNotEmpty();
+        assertThat(parameters.getEngineServiceParameters().getPolicyModel())
+            .contains("{\"key\":{\"name\":\"dummy key1 provided in properties\",\"version\":\"0.0.1\"},"
+                + "\"keyInformation\":{\"key\":{\"name\":\"dummy key2 provided in properties\","
+                + "\"version\":\"0.0.1\"}},\"threshold\":3.15,\"state\":\"passive\"}");
+    }
+
+    @Test
+    public void testPolicyModelFromPropertiesAndMetadata() throws ParameterException {
+        // Policy models provided in both properties and in metadata. policyModels in metadata takes precedence
+        final String[] args = {"-p", "src/test/resources/parameters/policyModelMultiple.json"};
+        final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args);
+
+        ApexParameters parameters = new ApexParameterHandler().getParameters(arguments);
+
+        assertThat(parameters.getEngineServiceParameters().getPolicyModel()).isNotEmpty();
+        assertThat(parameters.getEngineServiceParameters().getPolicyModel())
+            .contains("{\"key\":{\"name\":\"dummy key1 provided in metadata\",\"version\":\"0.0.1\"},"
+                + "\"keyInformation\":{\"key\":{\"name\":\"dummy key2 provided in metadata\","
+                + "\"version\":\"0.0.1\"}},\"threshold\":3.15,\"state\":\"passive\"}");
+    }
+
+    @Test
     public void testGettersSetters() {
         ApexParameters pars = new ApexParameters();
         assertNotNull(pars);
diff --git a/services/services-engine/src/test/resources/parameters/policyModelFromMetadata.json b/services/services-engine/src/test/resources/parameters/policyModelFromMetadata.json
new file mode 100644
index 0000000..d7423bc
--- /dev/null
+++ b/services/services-engine/src/test/resources/parameters/policyModelFromMetadata.json
@@ -0,0 +1,89 @@
+{
+  "tosca_definitions_version": "tosca_simple_yaml_1_1_0",
+  "topology_template": {
+    "policies": [
+      {
+        "onap.policies.apex.Simplecontrolloop": {
+          "type": "onap.policies.native.Apex",
+          "type_version": "1.0.0",
+          "name": "onap.policies.apex.Simplecontrolloop",
+          "version": "1.0.0",
+          "metadata": {
+            "metadataSetName": "dummyMetadataSet",
+            "metadataSetVersion": "1.0.0",
+            "metadataSet": {
+              "apexPolicyModel": {
+                "key": {
+                  "name": "dummy key1 provided in metadata",
+                  "version": "0.0.1"
+                },
+                "keyInformation": {
+                  "key": {
+                    "name": "dummy key2 provided in metadata",
+                    "version": "0.0.1"
+                  }
+                },
+                "threshold": 3.15,
+                "state": "passive"
+              }
+            }
+          },
+          "properties": {
+            "javaProperties": [
+              [
+                "property0",
+                "property0Value"
+              ],
+              [
+                "property1",
+                "property1Value"
+              ]
+            ],
+            "engineServiceParameters": {
+              "name": "MyApexEngine",
+              "version": "0.0.1",
+              "id": 45,
+              "instanceCount": 345,
+              "deploymentPort": 65522,
+              "engineParameters": {
+                "contextParameters": {
+                  "parameterClassName": "org.onap.policy.apex.context.parameters.ContextParameters"
+                },
+                "executorParameters": {
+                  "JAVASCRIPT": {
+                    "parameterClassName": "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperExecutorParameters"
+                  }
+                }
+              }
+            },
+            "eventOutputParameters": {
+              "FirstProducer": {
+                "carrierTechnologyParameters": {
+                  "carrierTechnology": "FILE",
+                  "parameters": {
+                    "fileName": "target/aaa.json"
+                  }
+                },
+                "eventProtocolParameters": {
+                  "eventProtocol": "JSON"
+                }
+              }
+            },
+            "eventInputParameters": {
+              "MySuperDooperConsumer1": {
+                "carrierTechnologyParameters": {
+                  "carrierTechnology": "SUPER_DOOPER",
+                  "parameterClassName": "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters"
+                },
+                "eventProtocolParameters": {
+                  "eventProtocol": "SUPER_TOK_DEL",
+                  "parameterClassName": "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters"
+                }
+              }
+            }
+          }
+        }
+      }
+    ]
+  }
+}
\ No newline at end of file
diff --git a/services/services-engine/src/test/resources/parameters/policyModelFromProperties.json b/services/services-engine/src/test/resources/parameters/policyModelFromProperties.json
new file mode 100644
index 0000000..77f00ac
--- /dev/null
+++ b/services/services-engine/src/test/resources/parameters/policyModelFromProperties.json
@@ -0,0 +1,85 @@
+{
+  "tosca_definitions_version": "tosca_simple_yaml_1_1_0",
+  "topology_template": {
+    "policies": [
+      {
+        "onap.policies.apex.Simplecontrolloop": {
+          "type": "onap.policies.native.Apex",
+          "type_version": "1.0.0",
+          "name": "onap.policies.apex.Simplecontrolloop",
+          "version": "1.0.0",
+          "properties": {
+            "javaProperties": [
+              [
+                "property0",
+                "property0Value"
+              ],
+              [
+                "property1",
+                "property1Value"
+              ]
+            ],
+            "engineServiceParameters": {
+              "name": "MyApexEngine",
+              "version": "0.0.1",
+              "id": 45,
+              "instanceCount": 345,
+              "deploymentPort": 65522,
+              "policy_type_impl": {
+                "apexPolicyModel": {
+                  "key": {
+                    "name": "dummy key1 provided in properties",
+                    "version": "0.0.1"
+                  },
+                  "keyInformation": {
+                    "key": {
+                      "name": "dummy key2 provided in properties",
+                      "version": "0.0.1"
+                    }
+                  },
+                  "threshold": 3.15,
+                  "state": "passive"
+                }
+              },
+              "engineParameters": {
+                "contextParameters": {
+                  "parameterClassName": "org.onap.policy.apex.context.parameters.ContextParameters"
+                },
+                "executorParameters": {
+                  "JAVASCRIPT": {
+                    "parameterClassName": "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperExecutorParameters"
+                  }
+                }
+              }
+            },
+            "eventOutputParameters": {
+              "FirstProducer": {
+                "carrierTechnologyParameters": {
+                  "carrierTechnology": "FILE",
+                  "parameters": {
+                    "fileName": "target/aaa.json"
+                  }
+                },
+                "eventProtocolParameters": {
+                  "eventProtocol": "JSON"
+                }
+              }
+            },
+            "eventInputParameters": {
+              "MySuperDooperConsumer1": {
+                "carrierTechnologyParameters": {
+                  "carrierTechnology": "SUPER_DOOPER",
+                  "parameterClassName": "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters"
+                },
+                "eventProtocolParameters": {
+                  "eventProtocol": "SUPER_TOK_DEL",
+                  "parameterClassName": "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters"
+                }
+              }
+            }
+          }
+        }
+      }
+    ]
+  }
+}
\ No newline at end of file
diff --git a/services/services-engine/src/test/resources/parameters/policyModelMultiple.json b/services/services-engine/src/test/resources/parameters/policyModelMultiple.json
new file mode 100644
index 0000000..dc1e2bc
--- /dev/null
+++ b/services/services-engine/src/test/resources/parameters/policyModelMultiple.json
@@ -0,0 +1,105 @@
+{
+  "tosca_definitions_version": "tosca_simple_yaml_1_1_0",
+  "topology_template": {
+    "policies": [
+      {
+        "onap.policies.apex.Simplecontrolloop": {
+          "type": "onap.policies.native.Apex",
+          "type_version": "1.0.0",
+          "name": "onap.policies.apex.Simplecontrolloop",
+          "version": "1.0.0",
+          "metadata": {
+            "metadataSetName": "dummyMetadataSet",
+            "metadataSetVersion": "1.0.0",
+            "metadataSet": {
+              "apexPolicyModel": {
+                "key": {
+                  "name": "dummy key1 provided in metadata",
+                  "version": "0.0.1"
+                },
+                "keyInformation": {
+                  "key": {
+                    "name": "dummy key2 provided in metadata",
+                    "version": "0.0.1"
+                  }
+                },
+                "threshold": 3.15,
+                "state": "passive"
+              }
+            }
+          },
+          "properties": {
+            "javaProperties": [
+              [
+                "property0",
+                "property0Value"
+              ],
+              [
+                "property1",
+                "property1Value"
+              ]
+            ],
+            "engineServiceParameters": {
+              "name": "MyApexEngine",
+              "version": "0.0.1",
+              "id": 45,
+              "instanceCount": 345,
+              "deploymentPort": 65522,
+              "policy_type_impl": {
+                "apexPolicyModel": {
+                  "key": {
+                    "name": "dummy key1 provided in properties",
+                    "version": "0.0.1"
+                  },
+                  "keyInformation": {
+                    "key": {
+                      "name": "dummy key2 provided in properties",
+                      "version": "0.0.1"
+                    }
+                  },
+                  "threshold": 3.15,
+                  "state": "passive"
+                }
+              },
+              "engineParameters": {
+                "contextParameters": {
+                  "parameterClassName": "org.onap.policy.apex.context.parameters.ContextParameters"
+                },
+                "executorParameters": {
+                  "JAVASCRIPT": {
+                    "parameterClassName": "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperExecutorParameters"
+                  }
+                }
+              }
+            },
+            "eventOutputParameters": {
+              "FirstProducer": {
+                "carrierTechnologyParameters": {
+                  "carrierTechnology": "FILE",
+                  "parameters": {
+                    "fileName": "target/aaa.json"
+                  }
+                },
+                "eventProtocolParameters": {
+                  "eventProtocol": "JSON"
+                }
+              }
+            },
+            "eventInputParameters": {
+              "MySuperDooperConsumer1": {
+                "carrierTechnologyParameters": {
+                  "carrierTechnology": "SUPER_DOOPER",
+                  "parameterClassName": "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters"
+                },
+                "eventProtocolParameters": {
+                  "eventProtocol": "SUPER_TOK_DEL",
+                  "parameterClassName": "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters"
+                }
+              }
+            }
+          }
+        }
+      }
+    ]
+  }
+}
\ No newline at end of file