956b5e9116f04d73dd7d942a029de47e730b783e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.commissioning;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
33 import org.onap.policy.common.utils.coder.Coder;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43
44 public class CommissioningProviderTest {
45     private static final String TOSCA_SERVICE_TEMPLATE_YAML =
46             "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml";
47     private static final String TEMPLATE_IS_NULL = ".*serviceTemplate is marked non-null but is null";
48     private static final Coder CODER = new StandardCoder();
49     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
50     private static int dbNum = 0;
51     private static final Object lockit = new Object();
52     private PolicyModelsProviderParameters databaseProviderParameters;
53
54     private static String getParameterGroupAsString() {
55         dbNum++;
56         return ResourceUtils.getResourceAsString("src/test/resources/parameters/TestParameters.json")
57                 .replace("jdbc:h2:mem:testdb", "jdbc:h2:mem:commissioningdb" + dbNum);
58     }
59
60     /**
61      * Sets up db provider parameters before each test.
62      *
63      * @throws CoderException .
64      */
65     @Before
66     public void setupDbProviderParameters() throws CoderException {
67         synchronized (lockit) {
68             databaseProviderParameters = CODER.decode(getParameterGroupAsString(), ClRuntimeParameterGroup.class)
69                     .getDatabaseProviderParameters();
70         }
71     }
72
73     /**
74      * Test the fetching of control loop definitions (ToscaServiceTemplates).
75      *
76      * @throws Exception .
77      */
78     @Test
79     public void testGetControlLoopDefinitions() throws Exception {
80         List<ToscaNodeTemplate> listOfTemplates;
81
82         try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) {
83             ToscaServiceTemplate serviceTemplate = yamlTranslator
84                     .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
85                             ToscaServiceTemplate.class);
86
87
88             listOfTemplates = provider.getControlLoopDefinitions(null, null);
89             assertThat(listOfTemplates).isEmpty();
90
91             provider.createControlLoopDefinitions(serviceTemplate);
92             listOfTemplates = provider.getControlLoopDefinitions(null, null);
93             assertThat(listOfTemplates).hasSize(2);
94
95             //Test Filtering
96             listOfTemplates = provider.getControlLoopDefinitions("org.onap.domain.pmsh.PMSHControlLoopDefinition",
97                     "1.2.3");
98             assertThat(listOfTemplates).hasSize(1);
99             for (ToscaNodeTemplate template : listOfTemplates) {
100                 //Other CL elements contain PMSD instead of PMSH in their name
101                 assertFalse(template.getName().contains("PMSD"));
102             }
103
104             //Test Wrong Name
105             listOfTemplates = provider.getControlLoopDefinitions("WrongControlLoopName", "0.0.0");
106             assertThat(listOfTemplates).isEmpty();
107         }
108     }
109
110     /**
111      * Test the creation of control loop definitions (ToscaServiceTemplates).
112      *
113      * @throws Exception .
114      */
115     @Test
116     public void testCreateControlLoopDefinitions() throws Exception {
117         List<ToscaNodeTemplate> listOfTemplates;
118
119         try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) {
120             //Test Service template is null
121             assertThatThrownBy(() -> provider.createControlLoopDefinitions(null)).hasMessageMatching(TEMPLATE_IS_NULL);
122             listOfTemplates = provider.getControlLoopDefinitions(null, null);
123             assertThat(listOfTemplates).isEmpty();
124
125             ToscaServiceTemplate serviceTemplate = yamlTranslator
126                     .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
127                             ToscaServiceTemplate.class);
128
129             // Response should return the number of node templates present in the service template
130             List<ToscaConceptIdentifier> affectedDefinitions =
131                     provider.createControlLoopDefinitions(serviceTemplate).getAffectedControlLoopDefinitions();
132             assertThat(affectedDefinitions).hasSize(13);
133             listOfTemplates = provider.getControlLoopDefinitions(null, null);
134             assertThat(listOfTemplates).hasSize(2);
135         }
136     }
137
138     /**
139      * Test the deletion of control loop definitions (ToscaServiceTemplate).
140      *
141      * @throws Exception .
142      */
143     @Test
144     public void testDeleteControlLoopDefinitions() throws Exception {
145         List<ToscaNodeTemplate> listOfTemplates;
146
147         try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) {
148             ToscaServiceTemplate serviceTemplate = yamlTranslator
149                     .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
150                             ToscaServiceTemplate.class);
151
152             listOfTemplates = provider.getControlLoopDefinitions(null, null);
153             assertThat(listOfTemplates).isEmpty();
154
155             provider.createControlLoopDefinitions(serviceTemplate);
156             listOfTemplates = provider.getControlLoopDefinitions(null, null);
157             assertThat(listOfTemplates).hasSize(2);
158
159             provider.deleteControlLoopDefinition(serviceTemplate.getName(), serviceTemplate.getVersion());
160             listOfTemplates = provider.getControlLoopDefinitions(null, null);
161             assertThat(listOfTemplates).isEmpty();
162         }
163     }
164
165     /**
166      * Test the fetching of control loop element definitions.
167      *
168      * @throws Exception .
169      */
170     @Test
171     public void testGetControlLoopElementDefinitions() throws Exception {
172         try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) {
173             ToscaServiceTemplate serviceTemplate = yamlTranslator
174                     .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML),
175                             ToscaServiceTemplate.class);
176
177             provider.getControlLoopDefinitions(null, null);
178
179             provider.createControlLoopDefinitions(serviceTemplate);
180             List<ToscaNodeTemplate> controlLoopDefinitionList = provider.getControlLoopDefinitions(
181                     "org.onap.domain.pmsh.PMSHControlLoopDefinition", "1.2.3");
182
183             List<ToscaNodeTemplate> controlLoopElementNodeTemplates =
184                     provider.getControlLoopElementDefinitions(controlLoopDefinitionList.get(0));
185
186             // 4 PMSH control loop elements definitions.
187             assertThat(controlLoopElementNodeTemplates).hasSize(4);
188
189             List<ToscaNodeType> derivedTypes = getDerivedNodeTypes(serviceTemplate);
190             for (ToscaNodeTemplate template : controlLoopElementNodeTemplates) {
191                 assertTrue(checkNodeType(template, derivedTypes));
192             }
193         }
194     }
195
196     private boolean checkNodeType(ToscaNodeTemplate template, List<ToscaNodeType> derivedNodeTypes) {
197         String controlLoopElementType = "org.onap.policy.clamp.controlloop.ControlLoopElement";
198         for (ToscaNodeType derivedType : derivedNodeTypes) {
199             if (template.getType().equals(derivedType.getName()) || template.getType().equals(controlLoopElementType)) {
200                 return true;
201             }
202         }
203         return false;
204     }
205
206     private List<ToscaNodeType> getDerivedNodeTypes(ToscaServiceTemplate serviceTemplate) {
207         String type = "org.onap.policy.clamp.controlloop.ControlLoopElement";
208         List<ToscaNodeType> nodeTypes = new ArrayList<>();
209         for (ToscaNodeType nodeType : serviceTemplate.getNodeTypes().values()) {
210             if (nodeType.getDerivedFrom().equals(type)) {
211                 nodeTypes.add(nodeType);
212             }
213         }
214         return nodeTypes;
215     }
216 }