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