4579b52bfa6aef15faa9163370e11ebc5ad06e1c
[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.dcae.main.rest;
22
23 import java.io.File;
24 import java.time.Instant;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.UUID;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
37 import org.onap.policy.common.utils.coder.Coder;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45
46 public class TestListenerUtils {
47
48     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
49     private static final Coder CODER = new StandardCoder();
50     private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
51
52     /**
53      * Method to create a controlLoop from a yaml file.
54      *
55      * @return ControlLoop controlloop
56      */
57     public static ControlLoop createControlLoop() {
58         ControlLoop controlLoop = new ControlLoop();
59         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
60         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
61         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
62                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
63         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
64             ControlLoopElement clElement = new ControlLoopElement();
65             clElement.setId(UUID.randomUUID());
66
67             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
68             clElementParticipantId.setName(toscaInputEntry.getKey());
69             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
70             clElement.setParticipantId(clElementParticipantId);
71
72             clElement.setDefinition(clElementParticipantId);
73             clElement.setState(ControlLoopState.UNINITIALISED);
74             clElement.setDescription(toscaInputEntry.getValue().getDescription());
75             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
76             elements.put(clElement.getId(), clElement);
77         }
78         controlLoop.setElements(elements);
79         controlLoop.setName("PMSHInstance0");
80         controlLoop.setVersion("1.0.0");
81
82         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
83         definition.setName("PMSHInstance0");
84         definition.setVersion("1.0.0");
85         controlLoop.setDefinition(definition);
86
87         return controlLoop;
88     }
89
90     /**
91      * Method to create ParticipantStateChange message from the arguments passed.
92      *
93      * @param participantState participant State
94      *
95      * @return ParticipantStateChange message
96      */
97     public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
98         final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
99         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
100         participantId.setName("DCAEParticipant0");
101         participantId.setVersion("1.0.0");
102
103         participantStateChangeMsg.setParticipantId(participantId);
104         participantStateChangeMsg.setTimestamp(Instant.now());
105         participantStateChangeMsg.setState(participantState);
106
107         return participantStateChangeMsg;
108     }
109
110     /**
111      * Method to create ControlLoopStateChange message from the arguments passed.
112      *
113      * @param controlLoopOrderedState controlLoopOrderedState
114      *
115      * @return ParticipantControlLoopStateChange message
116      */
117     public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
118             final ControlLoopOrderedState controlLoopOrderedState) {
119         final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
120
121         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
122         controlLoopId.setName("PMSHInstance0");
123         controlLoopId.setVersion("1.0.0");
124
125         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
126         participantId.setName("DCAEParticipant0");
127         participantId.setVersion("1.0.0");
128
129         participantClStateChangeMsg.setControlLoopId(controlLoopId);
130         participantClStateChangeMsg.setParticipantId(participantId);
131         participantClStateChangeMsg.setTimestamp(Instant.now());
132         participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
133
134         return participantClStateChangeMsg;
135     }
136
137     /**
138      * Method to create ControlLoopUpdateMsg.
139      *
140      * @return ParticipantControlLoopUpdate message
141      */
142     public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() {
143         final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
144         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
145         controlLoopId.setName("PMSHInstance0");
146         controlLoopId.setVersion("1.0.0");
147
148         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
149         participantId.setName("DCAEParticipant0");
150         participantId.setVersion("1.0.0");
151
152         clUpdateMsg.setControlLoopId(controlLoopId);
153         clUpdateMsg.setParticipantId(participantId);
154
155         ControlLoop controlLoop = new ControlLoop();
156         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
157         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
158         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
159                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
160         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
161             ControlLoopElement clElement = new ControlLoopElement();
162             clElement.setId(UUID.randomUUID());
163
164             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
165             clElementParticipantId.setName(toscaInputEntry.getKey());
166             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
167             clElement.setParticipantId(clElementParticipantId);
168
169             clElement.setDefinition(clElementParticipantId);
170             clElement.setState(ControlLoopState.UNINITIALISED);
171             clElement.setDescription(toscaInputEntry.getValue().getDescription());
172             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
173             elements.put(clElement.getId(), clElement);
174         }
175         controlLoop.setElements(elements);
176         controlLoop.setName("PMSHInstance0");
177         controlLoop.setVersion("1.0.0");
178         controlLoop.setDefinition(controlLoopId);
179         clUpdateMsg.setControlLoop(controlLoop);
180         clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
181
182         return clUpdateMsg;
183     }
184
185     /**
186      * Method to create a deep copy of ControlLoopUpdateMsg.
187      *
188      * @return ParticipantControlLoopUpdate message
189      */
190     public static ParticipantControlLoopUpdate createCopyControlLoopUpdateMsg(ParticipantControlLoopUpdate cpy) {
191         return new ParticipantControlLoopUpdate(cpy);
192     }
193
194     /**
195      * Method to create ParticipantHealthCheck message.
196      *
197      * @return ParticipantHealthCheck message
198      */
199     public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
200         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
201         participantId.setName("DCAEParticipant0");
202         participantId.setVersion("1.0.0");
203
204         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
205         controlLoopId.setName("PMSHInstance0");
206         controlLoopId.setVersion("1.0.0");
207
208         final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
209         participantHealthCheckMsg.setParticipantId(participantId);
210         participantHealthCheckMsg.setControlLoopId(controlLoopId);
211         participantHealthCheckMsg.setTimestamp(Instant.now());
212         participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
213
214         return participantHealthCheckMsg;
215     }
216
217     /**
218      * Method to create ParticipantControlLoopUpdate using the arguments passed.
219      *
220      * @param jsonFilePath the path of the controlloop content
221      *
222      * @return ParticipantControlLoopUpdate message
223      * @throws CoderException exception while reading the file to object
224      */
225     public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
226             throws CoderException {
227         ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
228                 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
229         return participantControlLoopUpdateMsg;
230     }
231
232     private static ToscaServiceTemplate testControlLoopRead() {
233         return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
234     }
235
236     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
237         String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
238         ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
239         return serviceTemplate;
240     }
241 }