136d0e5005eeaf83e354415bfadaa79fa685d158
[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.ControlLoopStateChange;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
35 import org.onap.policy.common.utils.coder.Coder;
36 import org.onap.policy.common.utils.coder.CoderException;
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
44 public class TestListenerUtils {
45
46     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
47     private static final Coder CODER = new StandardCoder();
48     private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
49
50     /**
51      * Method to create a controlLoop from a yaml file.
52      *
53      * @return ControlLoop controlloop
54      */
55     public static ControlLoop createControlLoop() {
56         ControlLoop controlLoop = new ControlLoop();
57         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
58         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
59         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
60                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
61         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
62             ControlLoopElement clElement = new ControlLoopElement();
63             clElement.setId(UUID.randomUUID());
64
65             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
66             clElementParticipantId.setName(toscaInputEntry.getKey());
67             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
68             clElement.setParticipantId(clElementParticipantId);
69
70             clElement.setDefinition(clElementParticipantId);
71             clElement.setState(ControlLoopState.UNINITIALISED);
72             clElement.setDescription(toscaInputEntry.getValue().getDescription());
73             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
74             elements.put(clElement.getId(), clElement);
75         }
76         controlLoop.setElements(elements);
77         controlLoop.setName("PMSHInstance0");
78         controlLoop.setVersion("1.0.0");
79
80         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
81         definition.setName("PMSHInstance0");
82         definition.setVersion("1.0.0");
83         controlLoop.setDefinition(definition);
84
85         return controlLoop;
86     }
87
88     /**
89      * Method to create ControlLoopStateChange message from the arguments passed.
90      *
91      * @param controlLoopOrderedState controlLoopOrderedState
92      *
93      * @return ControlLoopStateChange message
94      */
95     public static ControlLoopStateChange createControlLoopStateChangeMsg(
96             final ControlLoopOrderedState controlLoopOrderedState) {
97         final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
98
99         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
100         controlLoopId.setName("PMSHInstance0");
101         controlLoopId.setVersion("1.0.0");
102
103         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
104         participantId.setName("DCAEParticipant0");
105         participantId.setVersion("1.0.0");
106
107         clStateChangeMsg.setControlLoopId(controlLoopId);
108         clStateChangeMsg.setParticipantId(participantId);
109         clStateChangeMsg.setTimestamp(Instant.now());
110         clStateChangeMsg.setOrderedState(controlLoopOrderedState);
111
112         return clStateChangeMsg;
113     }
114
115     /**
116      * Method to create ControlLoopUpdateMsg.
117      *
118      * @return ControlLoopUpdate message
119      */
120     public static ControlLoopUpdate createControlLoopUpdateMsg() {
121         final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
122         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
123         controlLoopId.setName("PMSHInstance0");
124         controlLoopId.setVersion("1.0.0");
125
126         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
127         participantId.setName("DCAEParticipant0");
128         participantId.setVersion("1.0.0");
129
130         clUpdateMsg.setControlLoopId(controlLoopId);
131         clUpdateMsg.setParticipantId(participantId);
132
133         ControlLoop controlLoop = new ControlLoop();
134         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
135         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
136         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
137                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
138         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
139             ControlLoopElement clElement = new ControlLoopElement();
140             clElement.setId(UUID.randomUUID());
141
142             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
143             clElementParticipantId.setName(toscaInputEntry.getKey());
144             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
145             clElement.setParticipantId(clElementParticipantId);
146
147             clElement.setDefinition(clElementParticipantId);
148             clElement.setState(ControlLoopState.UNINITIALISED);
149             clElement.setDescription(toscaInputEntry.getValue().getDescription());
150             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
151             elements.put(clElement.getId(), clElement);
152         }
153         controlLoop.setElements(elements);
154         controlLoop.setName("PMSHInstance0");
155         controlLoop.setVersion("1.0.0");
156         controlLoop.setDefinition(controlLoopId);
157         clUpdateMsg.setControlLoop(controlLoop);
158
159         return clUpdateMsg;
160     }
161
162     /**
163      * Method to create a deep copy of ControlLoopUpdateMsg.
164      *
165      * @return ControlLoopUpdate message
166      */
167     public static ControlLoopUpdate createCopyControlLoopUpdateMsg(ControlLoopUpdate cpy) {
168         return new ControlLoopUpdate(cpy);
169     }
170
171     /**
172      * Method to create ControlLoopUpdate using the arguments passed.
173      *
174      * @param jsonFilePath the path of the controlloop content
175      *
176      * @return ControlLoopUpdate message
177      * @throws CoderException exception while reading the file to object
178      */
179     public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
180             throws CoderException {
181         ControlLoopUpdate controlLoopUpdateMsg =
182                 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
183         return controlLoopUpdateMsg;
184     }
185
186     private static ToscaServiceTemplate testControlLoopRead() {
187         return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
188     }
189
190     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
191         String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
192         ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
193         return serviceTemplate;
194     }
195 }