aboutsummaryrefslogtreecommitdiffstats
path: root/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java
blob: 28558cc0220c345b7ac9b1274dd2ca0018220bb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2021 Nordix Foundation.
 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.controlloop.runtime.instantiation;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import lombok.AllArgsConstructor;
import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
import org.onap.policy.clamp.controlloop.models.messages.rest.GenericNameVersion;
import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopOrderStateResponse;
import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopPrimed;
import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopPrimedResponse;
import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstancePropertiesResponse;
import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider;
import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler;
import org.onap.policy.common.parameters.BeanValidationResult;
import org.onap.policy.common.parameters.ObjectValidationResult;
import org.onap.policy.common.parameters.ValidationResult;
import org.onap.policy.common.parameters.ValidationStatus;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNameVersion;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.springframework.stereotype.Component;

/**
 * This class is dedicated to the Instantiation of Commissioned control loop.
 */
@Component
@AllArgsConstructor
public class ControlLoopInstantiationProvider {
    private static final String CONTROL_LOOP_NODE_TYPE = "org.onap.policy.clamp.controlloop.ControlLoop";
    private static final String CONTROL_LOOP_NODE_ELEMENT_TYPE = "ControlLoopElement";
    private static final String PARTICIPANT_ID_PROPERTY_KEY = "participant_id";
    private static final String CL_ELEMENT_NAME = "name";
    private static final String CL_ELEMENT_VERSION = "version";
    private static final String INSTANCE_TEXT = "_Instance";

    private static final Gson GSON = new Gson();

    private final ControlLoopProvider controlLoopProvider;
    private final CommissioningProvider commissioningProvider;
    private final SupervisionHandler supervisionHandler;

    private static final Object lockit = new Object();

    /**
     * Creates Instance Properties and Control Loop.
     *
     * @param serviceTemplate the service template
     * @return the result of the instantiation operation
     * @throws PfModelException on creation errors
     */
    public InstancePropertiesResponse createInstanceProperties(ToscaServiceTemplate serviceTemplate)
        throws PfModelException {

        String instanceName = generateSequentialInstanceName();
        ControlLoop controlLoop = new ControlLoop();
        Map<UUID, ControlLoopElement> controlLoopElements = new HashMap<>();

        ToscaServiceTemplate toscaServiceTemplate = commissioningProvider
            .getToscaServiceTemplate(null, null);

        Map<String, ToscaNodeTemplate> persistedNodeTemplateMap = toscaServiceTemplate
            .getToscaTopologyTemplate().getNodeTemplates();

        Map<String, ToscaNodeTemplate> nodeTemplates =
            deepCloneNodeTemplate(serviceTemplate);

        nodeTemplates.forEach((key, template) -> {
            ToscaNodeTemplate newNodeTemplate = new ToscaNodeTemplate();
            String name = key + instanceName;
            String version = template.getVersion();
            String description = template.getDescription() + instanceName;
            newNodeTemplate.setName(name);
            newNodeTemplate.setVersion(version);
            newNodeTemplate.setDescription(description);
            newNodeTemplate.setProperties(new HashMap<>(template.getProperties()));
            newNodeTemplate.setType(template.getType());
            newNodeTemplate.setTypeVersion(template.getTypeVersion());
            newNodeTemplate.setMetadata(template.getMetadata());

            crateNewControlLoopInstance(instanceName, controlLoop, controlLoopElements, template, newNodeTemplate);

            persistedNodeTemplateMap.put(name, newNodeTemplate);
        });

        ControlLoops controlLoops = new ControlLoops();

        serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().putAll(persistedNodeTemplateMap);

        controlLoop.setElements(controlLoopElements);
        controlLoops.getControlLoopList().add(controlLoop);

        return saveInstancePropertiesAndControlLoop(serviceTemplate, controlLoops);
    }

    /**
     * Deletes Instance Properties.
     *
     * @param name the name of the control loop to delete
     * @param version the version of the control loop to delete
     * @return the result of the deletion
     * @throws PfModelException on deletion errors
     */
    public InstantiationResponse deleteInstanceProperties(String name, String version) throws PfModelException {

        String instanceName = getInstancePropertyName(name, version);

        Map<String, ToscaNodeTemplate> filteredToscaNodeTemplateMap = new HashMap<>();

        ToscaServiceTemplate toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(name, version);

        toscaServiceTemplate.getToscaTopologyTemplate()
            .getNodeTemplates().forEach((key, nodeTemplate) -> {
                if (!nodeTemplate.getName().contains(instanceName)) {
                    filteredToscaNodeTemplateMap.put(key, nodeTemplate);
                }
            });

        List<ToscaNodeTemplate> filteredToscaNodeTemplateList =
            toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().values().stream()
                .filter(nodeTemplate -> nodeTemplate.getName().contains(instanceName)).collect(Collectors.toList());

        InstantiationResponse response = this.deleteControlLoop(name, version);

        controlLoopProvider.deleteInstanceProperties(filteredToscaNodeTemplateMap, filteredToscaNodeTemplateList);

        return response;
    }

    /**
     * Create control loops.
     *
     * @param controlLoops the control loop
     * @return the result of the instantiation operation
     * @throws PfModelException on creation errors
     */
    public InstantiationResponse createControlLoops(ControlLoops controlLoops) throws PfModelException {

        synchronized (lockit) {
            for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
                var checkControlLoop = controlLoopProvider
                    .getControlLoop(controlLoop.getKey().asIdentifier());
                if (checkControlLoop != null) {
                    throw new PfModelException(Response.Status.BAD_REQUEST,
                            controlLoop.getKey().asIdentifier() + " already defined");
                }
            }
            BeanValidationResult validationResult = validateControlLoops(controlLoops);
            if (!validationResult.isValid()) {
                throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
            }
            controlLoopProvider.createControlLoops(controlLoops.getControlLoopList());
        }

        var response = new InstantiationResponse();
        response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
                .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));

        return response;
    }

    /**
     * Update control loops.
     *
     * @param controlLoops the control loop
     * @return the result of the instantiation operation
     * @throws PfModelException on update errors
     */
    public InstantiationResponse updateControlLoops(ControlLoops controlLoops) throws PfModelException {
        synchronized (lockit) {
            BeanValidationResult validationResult = validateControlLoops(controlLoops);
            if (!validationResult.isValid()) {
                throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
            }
            controlLoopProvider.updateControlLoops(controlLoops.getControlLoopList());
        }

        var response = new InstantiationResponse();
        response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
                .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));

        return response;
    }

    /**
     * Validate ControlLoops.
     *
     * @param controlLoops ControlLoops to validate
     * @return the result of validation
     * @throws PfModelException if controlLoops is not valid
     */
    private BeanValidationResult validateControlLoops(ControlLoops controlLoops) throws PfModelException {

        var result = new BeanValidationResult("ControlLoops", controlLoops);

        for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
            var subResult = new BeanValidationResult("entry " + controlLoop.getDefinition().getName(), controlLoop);

            List<ToscaNodeTemplate> toscaNodeTemplates = commissioningProvider.getControlLoopDefinitions(
                    controlLoop.getDefinition().getName(), controlLoop.getDefinition().getVersion());

            if (toscaNodeTemplates.isEmpty()) {
                subResult.addResult(new ObjectValidationResult("ControlLoop", controlLoop.getDefinition().getName(),
                        ValidationStatus.INVALID, "Commissioned control loop definition not FOUND"));
            } else if (toscaNodeTemplates.size() > 1) {
                subResult.addResult(new ObjectValidationResult("ControlLoop", controlLoop.getDefinition().getName(),
                        ValidationStatus.INVALID, "Commissioned control loop definition not VALID"));
            } else {

                List<ToscaNodeTemplate> clElementDefinitions =
                        commissioningProvider.getControlLoopElementDefinitions(toscaNodeTemplates.get(0));

                // @formatter:off
                Map<String, ToscaConceptIdentifier> definitions = clElementDefinitions
                        .stream()
                        .map(nodeTemplate -> nodeTemplate.getKey().asIdentifier())
                        .collect(Collectors.toMap(ToscaConceptIdentifier::getName, UnaryOperator.identity()));
                // @formatter:on

                for (ControlLoopElement element : controlLoop.getElements().values()) {
                    subResult.addResult(validateDefinition(definitions, element.getDefinition()));
                }
            }
            result.addResult(subResult);
        }
        return result;
    }

    /**
     * Validate ToscaConceptIdentifier, checking if exist in ToscaConceptIdentifiers map.
     *
     * @param definitions map of all ToscaConceptIdentifiers
     * @param definition ToscaConceptIdentifier to validate
     * @return the validation result
     */
    private ValidationResult validateDefinition(Map<String, ToscaConceptIdentifier> definitions,
            ToscaConceptIdentifier definition) {
        var result = new BeanValidationResult("entry " + definition.getName(), definition);
        ToscaConceptIdentifier identifier = definitions.get(definition.getName());
        if (identifier == null) {
            result.setResult(ValidationStatus.INVALID, "Not FOUND");
        } else if (!identifier.equals(definition)) {
            result.setResult(ValidationStatus.INVALID, "Version not matching");
        }
        return (result.isClean() ? null : result);
    }

    /**
     * Delete the control loop with the given name and version.
     *
     * @param name the name of the control loop to delete
     * @param version the version of the control loop to delete
     * @return the result of the deletion
     * @throws PfModelException on deletion errors
     */
    public InstantiationResponse deleteControlLoop(String name, String version) throws PfModelException {
        var response = new InstantiationResponse();
        synchronized (lockit) {
            List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version);
            if (controlLoops.isEmpty()) {
                throw new PfModelException(Response.Status.NOT_FOUND, "Control Loop not found");
            }
            for (ControlLoop controlLoop : controlLoops) {
                if (!ControlLoopState.UNINITIALISED.equals(controlLoop.getState())) {
                    throw new PfModelException(Response.Status.BAD_REQUEST,
                            "Control Loop State is still " + controlLoop.getState());
                }
            }

            response.setAffectedControlLoops(Collections
                    .singletonList(controlLoopProvider.deleteControlLoop(name, version).getKey().asIdentifier()));
        }
        return response;
    }

    /**
     * Get the requested control loops.
     *
     * @param name the name of the control loop to get, null for all control loops
     * @param version the version of the control loop to get, null for all control loops
     * @return the control loops
     * @throws PfModelException on errors getting control loops
     */
    public ControlLoops getControlLoops(String name, String version) throws PfModelException {
        var controlLoops = new ControlLoops();
        controlLoops.setControlLoopList(controlLoopProvider.getControlLoops(name, version));

        return controlLoops;
    }

    /**
     * Issue a command to control loops, setting their ordered state.
     *
     * @param command the command to issue to control loops
     * @return the result of the initiation command
     * @throws PfModelException on errors setting the ordered state on the control loops
     * @throws ControlLoopException on ordered state invalid
     */
    public InstantiationResponse issueControlLoopCommand(InstantiationCommand command)
            throws ControlLoopException, PfModelException {

        if (command.getOrderedState() == null) {
            throw new ControlLoopException(Status.BAD_REQUEST, "ordered state invalid or not specified on command");
        }

        synchronized (lockit) {
            List<ControlLoop> controlLoops = new ArrayList<>(command.getControlLoopIdentifierList().size());
            for (ToscaConceptIdentifier id : command.getControlLoopIdentifierList()) {
                var controlLoop = controlLoopProvider.getControlLoop(id);
                controlLoop.setCascadedOrderedState(command.getOrderedState());
                controlLoops.add(controlLoop);
            }
            controlLoopProvider.updateControlLoops(controlLoops);
        }

        supervisionHandler.triggerControlLoopSupervision(command.getControlLoopIdentifierList());
        var response = new InstantiationResponse();
        response.setAffectedControlLoops(command.getControlLoopIdentifierList());

        return response;
    }

    /**
     * Gets a list of control loops with it's ordered state.
     *
     * @param name the name of the control loop to get, null for all control loops
     * @param version the version of the control loop to get, null for all control loops
     * @return a list of Instantiation Command
     * @throws PfModelException on errors getting control loops
     */
    public ControlLoopOrderStateResponse getInstantiationOrderState(String name, String version)
        throws PfModelException {

        List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version);

        var response = new ControlLoopOrderStateResponse();

        controlLoops.forEach(controlLoop -> {
            var genericNameVersion = new GenericNameVersion();
            genericNameVersion.setName(controlLoop.getName());
            genericNameVersion.setVersion(controlLoop.getVersion());
            response.getControlLoopIdentifierList().add(genericNameVersion);
        });

        return response;
    }

    /**
     * Saves Instance Properties and Control Loop.
     * Gets a list of control loops which are primed or de-primed.
     *
     * @param name the name of the control loop to get, null for all control loops
     * @param version the version of the control loop to get, null for all control loops
     * @return a list of Instantiation Command
     * @throws PfModelException on errors getting control loops
     */
    public ControlLoopPrimedResponse getControlLoopPriming(String name, String version)
        throws PfModelException {

        List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version);

        var response = new ControlLoopPrimedResponse();

        controlLoops.forEach(controlLoop -> {
            var primed = new ControlLoopPrimed();
            primed.setName(controlLoop.getName());
            primed.setVersion(controlLoop.getVersion());
            primed.setPrimed(controlLoop.getPrimed());
            response.getPrimedControlLoopsList().add(primed);
        });

        return response;
    }

    /**
     * Creates instance element name.
     *
     * @param serviceTemplate the service template
     * @param controlLoops a list of control loops
     * @return the result of the instance properties and instantiation operation
     * @throws PfModelException on creation errors
     */
    private InstancePropertiesResponse saveInstancePropertiesAndControlLoop(
        ToscaServiceTemplate serviceTemplate, ControlLoops controlLoops) throws PfModelException {

        var response = new InstancePropertiesResponse();

        Map<String, ToscaNodeTemplate> toscaSavedNodeTemplate;

        synchronized (lockit) {
            for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
                var checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier());
                if (checkControlLoop != null) {
                    throw new PfModelException(Response.Status.BAD_REQUEST,
                        controlLoop.getKey().asIdentifier() + " already defined");
                }
            }

            toscaSavedNodeTemplate = controlLoopProvider.saveInstanceProperties(serviceTemplate);

            controlLoopProvider.createControlLoops(controlLoops.getControlLoopList());

        }

        List<ToscaConceptIdentifier> affectedControlLoops = controlLoops.getControlLoopList().stream()
            .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList());

        List<ToscaConceptIdentifier> toscaAffectedProperties = toscaSavedNodeTemplate.values().stream()
            .map(template -> template.getKey().asIdentifier()).collect(Collectors.toList());

        response.setAffectedInstanceProperties(Stream.of(affectedControlLoops, toscaAffectedProperties)
            .flatMap(Collection::stream).collect(Collectors.toList()));

        return response;
    }

    /**
     * Crates a new Control Loop instance.
     * @param instanceName Control Loop Instance name
     * @param controlLoop empty Control Loop
     * @param controlLoopElements new Control Loop Element map
     * @param template original Cloned Tosca Node Template
     * @param newNodeTemplate new Tosca Node Template
     */
    private void crateNewControlLoopInstance(String instanceName, ControlLoop controlLoop,
                                             Map<UUID, ControlLoopElement> controlLoopElements,
                                             ToscaNodeTemplate template,
                                             ToscaNodeTemplate newNodeTemplate) {
        if (template.getType().equals(CONTROL_LOOP_NODE_TYPE)) {
            controlLoop.setDefinition(getControlLoopDefinition(newNodeTemplate));
        }

        if (template.getType().contains(CONTROL_LOOP_NODE_ELEMENT_TYPE)) {
            ControlLoopElement controlLoopElement = getControlLoopElement(instanceName, newNodeTemplate);
            controlLoopElements.put(controlLoopElement.getId(), controlLoopElement);
        }

        controlLoop.setName("PMSH" + instanceName);
        controlLoop.setVersion(template.getVersion());
        controlLoop.setDescription("PMSH control loop " + instanceName);
        controlLoop.setState(ControlLoopState.UNINITIALISED);
        controlLoop.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
    }


    /**
     * Get's the instance property name of the control loop.
     *
     * @param name the name of the control loop to get, null for all control loops
     * @param version the version of the control loop to get, null for all control loops
     * @return the instance name of the control loop instance properties
     * @throws PfModelException on errors getting control loops
     */
    private String getInstancePropertyName(String name, String version) throws PfModelException {
        List<String> toscaDefinitionsNames =
            controlLoopProvider.getControlLoops(name, version).stream().map(ControlLoop::getDefinition)
                .map(ToscaNameVersion::getName).collect(Collectors.toList());

        return toscaDefinitionsNames.stream().reduce("", (s1, s2) -> {

            if (s2.contains(INSTANCE_TEXT)) {
                String[] instances = s2.split(INSTANCE_TEXT);

                return INSTANCE_TEXT + instances[1];
            }

            return s1;
        });
    }

    /**
     * Generates Instance Name in sequential order and return it to append to the Node Template Name.
     *
     * @return instanceName
     */
    private String generateSequentialInstanceName() {
        List<ToscaNodeTemplate> nodeTemplates = controlLoopProvider.getNodeTemplates(null, null);

        int instanceNumber =
            nodeTemplates.stream().map(ToscaNodeTemplate::getName)
                .filter(name -> name.contains(INSTANCE_TEXT)).map(n -> {
                    String[] defNameArr = n.split(INSTANCE_TEXT);

                    return Integer.parseInt(defNameArr[1]);
                }).reduce(0, Math::max);

        return INSTANCE_TEXT + (instanceNumber + 1);
    }

    /**
     * Retrieves Control Loop Definition.
     *
     * @param template tosca node template
     * @return control loop definition
     */
    private ToscaConceptIdentifier getControlLoopDefinition(ToscaNodeTemplate template) {
        ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
        definition.setName(template.getName());
        definition.setVersion(template.getVersion());

        return definition;
    }

    /**
     * Retrieves Control Loop Element.
     *
     * @param instanceName instance name to be appended to participant name
     * @param template tosca node template
     * @return a control loop element
     */
    @SuppressWarnings("unchecked")
    private ControlLoopElement getControlLoopElement(String instanceName, ToscaNodeTemplate template) {
        ControlLoopElement controlLoopElement = new ControlLoopElement();
        ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
        definition.setName(template.getName());
        definition.setVersion(template.getVersion());
        controlLoopElement.setDefinition(definition);

        LinkedTreeMap<String, Object> participantId = (LinkedTreeMap<String, Object>) template.getProperties()
            .get(PARTICIPANT_ID_PROPERTY_KEY);

        ToscaConceptIdentifier participantIdAndType = new ToscaConceptIdentifier();
        participantIdAndType.setName(participantId.get(CL_ELEMENT_NAME) + instanceName);
        participantIdAndType.setVersion(String.valueOf(participantId.get(CL_ELEMENT_VERSION)));

        controlLoopElement.setParticipantType(participantIdAndType);
        controlLoopElement.setParticipantId(participantIdAndType);

        return controlLoopElement;
    }

    /**
     * Deep clones ToscaNodeTemplate.
     *
     * @param serviceTemplate ToscaServiceTemplate
     * @return a cloned Hash Map of ToscaNodeTemplate
     */
    private Map<String, ToscaNodeTemplate> deepCloneNodeTemplate(ToscaServiceTemplate serviceTemplate) {
        String jsonString = GSON.toJson(serviceTemplate.getToscaTopologyTemplate().getNodeTemplates());

        Type type = new TypeToken<HashMap<String, ToscaNodeTemplate>>() {}.getType();

        return GSON.fromJson(jsonString, type);
    }

}