7b7dd2d848e9bf7dc55fe4488a3ef91948e156c3
[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.participant.simulator.main.rest;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.FileNotFoundException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.UUID;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
35 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
36 import org.onap.policy.common.utils.coder.Coder;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class TestListenerUtils {
47
48     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
49     private static final Coder CODER = new StandardCoder();
50     static CommonTestData commonTestData = new CommonTestData();
51     private static RestController restController;
52     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
53
54     private TestListenerUtils() {}
55
56     /**
57      * Method to create a controlLoop from a yaml file.
58      *
59      * @return ControlLoop controlloop
60      */
61     public static ControlLoop createControlLoop() {
62         ControlLoop controlLoop = new ControlLoop();
63         List<ControlLoopElement> elements =  new ArrayList<>();
64         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
65         Map<String, ToscaNodeTemplate> nodeTemplatesMap = toscaServiceTemplate
66                 .getToscaTopologyTemplate().getNodeTemplates();
67         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
68             ControlLoopElement clElement = new ControlLoopElement();
69             clElement.setId(UUID.randomUUID());
70
71             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
72             clElementParticipantId.setName(toscaInputEntry.getKey());
73             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
74             clElement.setParticipantId(clElementParticipantId);
75
76             clElement.setDefinition(clElementParticipantId);
77             clElement.setState(ControlLoopState.UNINITIALISED);
78             clElement.setDescription(toscaInputEntry.getValue().getDescription());
79             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
80             elements.add(clElement);
81         }
82         controlLoop.setElements(elements);
83         controlLoop.setName("PMSHInstance0");
84         controlLoop.setVersion("1.0.0");
85
86         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
87         definition.setName("PMSHInstance0");
88         definition.setVersion("1.0.0");
89         controlLoop.setDefinition(definition);
90
91         return controlLoop;
92     }
93
94     private static ToscaServiceTemplate testControlLoopRead() {
95         Set<String> controlLoopDirectoryContents = ResourceUtils
96                 .getDirectoryContents("src/test/resources/rest/servicetemplates");
97
98         boolean atLeastOneControlLoopTested = false;
99         ToscaServiceTemplate toscaServiceTemplate = null;
100
101         for (String controlLoopFilePath : controlLoopDirectoryContents) {
102             if (!controlLoopFilePath.endsWith(".yaml")) {
103                 continue;
104             }
105             atLeastOneControlLoopTested = true;
106             toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
107         }
108
109         assertTrue(atLeastOneControlLoopTested);
110         return toscaServiceTemplate;
111     }
112
113     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
114         try {
115             String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
116             if (controlLoopString == null) {
117                 throw new FileNotFoundException(controlLoopFilePath);
118             }
119
120             ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
121                             controlLoopString, ToscaServiceTemplate.class);
122             return serviceTemplate;
123         } catch (FileNotFoundException e) {
124             LOGGER.error("cannot find YAML file", controlLoopFilePath);
125             throw new IllegalArgumentException(e);
126         }
127     }
128 }