8f37b47c95915fcdbe5ae227a8d58ee20d2cba3d
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.domain.models.operational;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.List;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.policy.common.utils.coder.CoderException;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31 import org.onap.policy.common.utils.resources.ResourceUtils;
32 import org.onap.policy.drools.domain.models.Metadata;
33 import org.onap.policy.drools.policies.DomainMaker;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
37
38 public class OperationalPolicyTest {
39     // Policy Types
40     private static final String OPERATIONAL_DROOLS_POLICY_TYPE = "onap.policies.controlloop.operational.common.Drools";
41
42     // Operational vCPE Policies
43     private static final String OP_POLICY_NAME_VCPE = "operational.restart";
44     private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON =
45                                 "policies/vCPE.policy.operational.input.tosca.json";
46
47     private DomainMaker domainMaker;
48     private StandardCoder nonValCoder;
49
50     @Before
51     public void setUp() {
52         domainMaker = new DomainMaker();
53         nonValCoder = new StandardCoder();
54     }
55
56     @Test
57     public void testToscaCompliantOperationalPolicyType() throws CoderException {
58         String rawVcpeToscaPolicy = getExamplesPolicyString(VCPE_OPERATIONAL_DROOLS_POLICY_JSON, OP_POLICY_NAME_VCPE);
59
60         // valid "known" policy type with implicit schema
61         ToscaPolicyTypeIdentifier operationalCompliantType =
62                 new ToscaPolicyTypeIdentifier(OPERATIONAL_DROOLS_POLICY_TYPE, "1.0.0");
63         assertTrue(domainMaker.isConformant(operationalCompliantType, rawVcpeToscaPolicy));
64         assertNotNull(domainMaker.convertTo(operationalCompliantType, rawVcpeToscaPolicy, OperationalPolicy.class));
65     }
66
67     @Test
68     public void testOperationalCompliantModel() {
69         // @formatter:off
70         OperationalPolicy policy =
71                 OperationalPolicy.builder()
72                     .metadata(Metadata.builder().policyId(OP_POLICY_NAME_VCPE).build())
73                     .name(OP_POLICY_NAME_VCPE)
74                     .type(OPERATIONAL_DROOLS_POLICY_TYPE)
75                     .typeVersion("1.0.0")
76                     .version("1.0.0")
77                     .properties(
78                             OperationalProperties.builder()
79                                     .id("ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e")
80                                     .abatement(true)
81                                     .trigger("unique-policy-id-1-restart")
82                                     .operations(
83                                             List.of(Operation.builder()
84                                                     .id("unique-policy-id-1-restart")
85                                                     .description("Restart the VM")
86                                                     .timeout(60)
87                                                     .retries(3)
88                                                     .actorOperation(ActorOperation.builder()
89                                                         .operation("Restart")
90                                                         .actor("APPC")
91                                                         .target(OperationalTarget.builder().targetType("VNF").build())
92                                                         .build())
93                                                     .build()))
94                                     .controllerName("usecases")
95                                     .build())
96                     .build();
97         // @formatter:on
98
99         assertNotNull(policy);
100     }
101
102     private String getJsonFromResource(String resourcePath) {
103         return ResourceUtils.getResourceAsString(resourcePath);
104     }
105
106     private ToscaPolicy getExamplesPolicy(String resourcePath, String policyName) throws CoderException {
107         String policyJson = getJsonFromResource(resourcePath);
108         ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
109         return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
110     }
111
112     private String getExamplesPolicyString(String resourcePath, String policyName) throws CoderException {
113         return nonValCoder.encode(getExamplesPolicy(resourcePath, policyName));
114     }
115 }