999feb1e1673d30d52dcd2e4490f788d84a83e71
[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.File;
26 import java.io.FileNotFoundException;
27 import java.time.Instant;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.UUID;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
39 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
40 import org.onap.policy.common.utils.coder.Coder;
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.coder.YamlJsonTranslator;
44 import org.onap.policy.common.utils.resources.ResourceUtils;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class TestListenerUtils {
52
53     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
54     private static final Coder CODER = new StandardCoder();
55     static CommonTestData commonTestData = new CommonTestData();
56     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
57
58     private TestListenerUtils() {}
59
60     /**
61      * Method to create a controlLoop from a yaml file.
62      *
63      * @return ControlLoop controlloop
64      */
65     public static ControlLoop createControlLoop() {
66         ControlLoop controlLoop = new ControlLoop();
67         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
68         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
69         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
70                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
71         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
72             ControlLoopElement clElement = new ControlLoopElement();
73             clElement.setId(UUID.randomUUID());
74
75             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
76             clElementParticipantId.setName(toscaInputEntry.getKey());
77             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
78             clElement.setParticipantId(clElementParticipantId);
79             clElement.setParticipantType(clElementParticipantId);
80
81             clElement.setDefinition(clElementParticipantId);
82             clElement.setState(ControlLoopState.UNINITIALISED);
83             clElement.setDescription(toscaInputEntry.getValue().getDescription());
84             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
85             elements.put(clElement.getId(), clElement);
86         }
87         controlLoop.setElements(elements);
88         controlLoop.setName("PMSHInstance0");
89         controlLoop.setVersion("1.0.0");
90
91         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
92         definition.setName("PMSHInstance0");
93         definition.setVersion("1.0.0");
94         controlLoop.setDefinition(definition);
95
96         return controlLoop;
97     }
98
99     /**
100      * Method to create ControlLoopStateChange message from the arguments passed.
101      *
102      * @param controlLoopOrderedState controlLoopOrderedState
103      *
104      * @return ControlLoopStateChange message
105      */
106     public static ControlLoopStateChange createControlLoopStateChangeMsg(
107             final ControlLoopOrderedState controlLoopOrderedState) {
108         final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
109
110         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
111         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
112
113         clStateChangeMsg.setControlLoopId(controlLoopId);
114         clStateChangeMsg.setParticipantId(participantId);
115         clStateChangeMsg.setTimestamp(Instant.now());
116         clStateChangeMsg.setOrderedState(controlLoopOrderedState);
117
118         return clStateChangeMsg;
119     }
120
121     /**
122      * Method to create ControlLoopUpdateMsg.
123      *
124      * @return ControlLoopUpdate message
125      */
126     public static ControlLoopUpdate createControlLoopUpdateMsg() {
127         final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
128         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
129         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
130
131         clUpdateMsg.setControlLoopId(controlLoopId);
132         clUpdateMsg.setParticipantId(participantId);
133         clUpdateMsg.setParticipantType(participantId);
134
135         ControlLoop controlLoop = new ControlLoop();
136         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
137         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
138         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
139                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
140         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
141             ControlLoopElement clElement = new ControlLoopElement();
142             clElement.setId(UUID.randomUUID());
143
144             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
145             clElementParticipantId.setName(toscaInputEntry.getKey());
146             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
147             clElement.setParticipantId(clElementParticipantId);
148             clElement.setParticipantType(clElementParticipantId);
149
150             clElement.setDefinition(clElementParticipantId);
151             clElement.setState(ControlLoopState.UNINITIALISED);
152             clElement.setDescription(toscaInputEntry.getValue().getDescription());
153             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
154             elements.put(clElement.getId(), clElement);
155         }
156         controlLoop.setElements(elements);
157         controlLoop.setName("PMSHInstance0");
158         controlLoop.setVersion("1.0.0");
159         controlLoop.setDefinition(controlLoopId);
160         clUpdateMsg.setControlLoop(controlLoop);
161
162         return clUpdateMsg;
163     }
164
165     /**
166      * Method to create ControlLoopUpdate using the arguments passed.
167      *
168      * @param jsonFilePath the path of the controlloop content
169      *
170      * @return ControlLoopUpdate message
171      * @throws CoderException exception while reading the file to object
172      */
173     public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
174             throws CoderException {
175         ControlLoopUpdate controlLoopUpdateMsg =
176                 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
177         return controlLoopUpdateMsg;
178     }
179
180     private static ToscaServiceTemplate testControlLoopRead() {
181         Set<String> controlLoopDirectoryContents =
182                 ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
183
184         boolean atLeastOneControlLoopTested = false;
185         ToscaServiceTemplate toscaServiceTemplate = null;
186
187         for (String controlLoopFilePath : controlLoopDirectoryContents) {
188             if (!controlLoopFilePath.endsWith(".yaml")) {
189                 continue;
190             }
191             atLeastOneControlLoopTested = true;
192             toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
193         }
194
195         assertTrue(atLeastOneControlLoopTested);
196         return toscaServiceTemplate;
197     }
198
199     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
200         try {
201             String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
202             if (controlLoopString == null) {
203                 throw new FileNotFoundException(controlLoopFilePath);
204             }
205
206             ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
207                             controlLoopString, ToscaServiceTemplate.class);
208             return serviceTemplate;
209         } catch (FileNotFoundException e) {
210             LOGGER.error("cannot find YAML file", controlLoopFilePath);
211             throw new IllegalArgumentException(e);
212         }
213     }
214 }