2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.models.domain.operational;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
26 import com.openpojo.reflection.PojoClass;
27 import com.openpojo.reflection.filters.FilterChain;
28 import com.openpojo.reflection.filters.FilterClassName;
29 import com.openpojo.reflection.filters.FilterNonConcrete;
30 import com.openpojo.reflection.impl.PojoClassFactory;
31 import com.openpojo.validation.Validator;
32 import com.openpojo.validation.ValidatorBuilder;
33 import com.openpojo.validation.test.impl.GetterTester;
34 import com.openpojo.validation.test.impl.SetterTester;
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.nio.file.Paths;
38 import java.util.List;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.resources.ResourceUtils;
44 import org.onap.policy.drools.domain.models.DroolsPolicy;
45 import org.onap.policy.drools.domain.models.Metadata;
46 import org.onap.policy.drools.policies.DomainMaker;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
51 public class OperationalPolicyTest {
53 private static final String OPERATIONAL_DROOLS_POLICY_TYPE = "onap.policies.controlloop.operational.common.Drools";
54 private static final String OPERATIONAL_LEGACY_POLICY_TYPE = "onap.policies.controlloop.Operational";
56 // Operational vCPE Policies
57 private static final String OP_POLICY_NAME_VCPE = "operational.restart";
58 private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON =
59 "policies/vCPE.policy.operational.input.tosca.json";
60 public static final String VCPE_OPERATIONAL_LEGACY_POLICY_JSON = "src/test/resources/tosca-legacy-vcpe.json";
62 private DomainMaker domainMaker;
63 private StandardCoder nonValCoder;
67 domainMaker = new DomainMaker();
68 nonValCoder = new StandardCoder();
73 public void testToscaCompliantOperationalPolicyType() throws CoderException {
74 String rawVcpeToscaPolicy = getExamplesPolicyString(VCPE_OPERATIONAL_DROOLS_POLICY_JSON, OP_POLICY_NAME_VCPE);
76 // valid "known" policy type with implicit schema
77 assertTrue(domainMaker
79 new ToscaPolicyTypeIdentifier(OPERATIONAL_DROOLS_POLICY_TYPE, "1.0.0"), rawVcpeToscaPolicy));
81 OperationalPolicy policy = domainMaker.convertTo(
82 new ToscaPolicyTypeIdentifier("OPERATIONAL_LEGACY_POLICY_TYPE", "1.0.0"),
83 rawVcpeToscaPolicy, OperationalPolicy.class);
85 assertNotNull(policy);
89 public void testToscaLegacyOperationalPolicyType() throws CoderException, IOException {
90 String rawVcpeToscaPolicy = getJsonFromFile(VCPE_OPERATIONAL_LEGACY_POLICY_JSON);
92 // valid "known" policy type with implicit schema
93 assertTrue(domainMaker
95 new ToscaPolicyTypeIdentifier(OPERATIONAL_LEGACY_POLICY_TYPE, "1.0.0"), rawVcpeToscaPolicy));
99 public void testOperationalCompliantModel() {
101 OperationalPolicy policy =
102 OperationalPolicy.builder()
103 .metadata(Metadata.builder().policyId(OP_POLICY_NAME_VCPE).build())
104 .name(OP_POLICY_NAME_VCPE)
105 .type(OPERATIONAL_DROOLS_POLICY_TYPE)
106 .typeVersion("1.0.0")
109 OperationalProperties.builder()
110 .id("ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e")
112 .trigger("unique-policy-id-1-restart")
114 List.of(Operation.builder()
115 .id("unique-policy-id-1-restart")
116 .description("Restart the VM")
119 .actorOperation(ActorOperation.builder()
120 .operation("Restart")
122 .target(OperationalTarget.builder().type("VNF").build())
125 .controllerName("usecases")
130 assertNotNull(policy);
134 public void testPackage() {
135 /* validate model pojos */
136 List<PojoClass> pojoClasses =
138 .getPojoClassesRecursively("org.onap.policy.drools.models.domain.operational",
139 new FilterChain(new FilterNonConcrete(),
140 new FilterClassName(DroolsPolicy.class.getName())));
142 Validator validator = ValidatorBuilder.create()
143 .with(new SetterTester(), new GetterTester()).build();
144 validator.validate(pojoClasses);
147 private String getJsonFromFile(String filePath) throws IOException {
148 return new String(Files.readAllBytes(Paths.get(filePath)));
151 private String getJsonFromResource(String resourcePath) {
152 return ResourceUtils.getResourceAsString(resourcePath);
155 private String getPolicyFromFileString(String filePath, String policyName) throws CoderException, IOException {
156 String policyJson = getJsonFromFile(filePath);
157 ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
158 return nonValCoder.encode(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName));
161 private ToscaPolicy getExamplesPolicy(String resourcePath, String policyName) throws CoderException {
162 String policyJson = getJsonFromResource(resourcePath);
163 ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
164 return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
167 private String getExamplesPolicyString(String resourcePath, String policyName) throws CoderException {
168 return nonValCoder.encode(getExamplesPolicy(resourcePath, policyName));