2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.runtime.commissioning;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.PropertyNamingStrategies;
27 import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.stream.Collectors;
34 import javax.ws.rs.core.Response.Status;
35 import org.apache.commons.collections4.CollectionUtils;
36 import org.apache.commons.collections4.MapUtils;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
38 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
39 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
41 import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse;
42 import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler;
43 import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher;
44 import org.onap.policy.models.base.PfModelException;
45 import org.onap.policy.models.provider.PolicyModelsProvider;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
58 import org.springframework.stereotype.Component;
61 * This class provides the create, read and delete actions on Commissioning of Control Loop concepts in the database to
65 public class CommissioningProvider {
66 public static final String CONTROL_LOOP_NODE_TYPE = "org.onap.policy.clamp.controlloop.ControlLoop";
67 private static final String INSTANCE_TEXT = "_Instance";
69 private final PolicyModelsProvider modelsProvider;
70 private final ControlLoopProvider clProvider;
71 private final ObjectMapper mapper = new ObjectMapper();
72 private final ParticipantProvider participantProvider;
73 private final SupervisionHandler supervisionHandler;
75 private static final Object lockit = new Object();
78 * Create a commissioning provider.
80 * @param modelsProvider the PolicyModelsProvider
81 * @param clProvider the ControlLoopProvider
83 public CommissioningProvider(PolicyModelsProvider modelsProvider,
84 ControlLoopProvider clProvider,
85 SupervisionHandler supervisionHandler,
86 ParticipantProvider participantProvider) {
87 this.modelsProvider = modelsProvider;
88 this.clProvider = clProvider;
89 this.supervisionHandler = supervisionHandler;
90 this.participantProvider = participantProvider;
91 mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
95 * Create control loops from a service template.
97 * @param serviceTemplate the service template
98 * @return the result of the commissioning operation
99 * @throws PfModelException on creation errors
101 public CommissioningResponse createControlLoopDefinitions(ToscaServiceTemplate serviceTemplate)
102 throws PfModelException {
104 if (verifyIfInstancePropertiesExists()) {
105 throw new PfModelException(Status.BAD_REQUEST,
106 "Delete instances, to commission control loop definitions");
109 synchronized (lockit) {
110 modelsProvider.createServiceTemplate(serviceTemplate);
111 List<Participant> participantList =
112 participantProvider.getParticipants(null, null);
113 if (!participantList.isEmpty()) {
114 this.supervisionHandler.handleSendCommissionMessage(
115 getCommonOrInstancePropertiesFromNodeTypes(true,
116 serviceTemplate.getName(),
117 serviceTemplate.getVersion()));
121 var response = new CommissioningResponse();
123 response.setAffectedControlLoopDefinitions(serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
126 .map(template -> template.getKey().asIdentifier())
127 .collect(Collectors.toList()));
134 * Delete the control loop definition with the given name and version.
136 * @param name the name of the control loop definition to delete
137 * @param version the version of the control loop to delete
138 * @return the result of the deletion
139 * @throws PfModelException on deletion errors
141 public CommissioningResponse deleteControlLoopDefinition(String name, String version)
142 throws PfModelException {
144 if (verifyIfInstancePropertiesExists()) {
145 throw new PfModelException(Status.BAD_REQUEST,
146 "Delete instances, to commission control loop definitions");
149 synchronized (lockit) {
150 List<Participant> participantList =
151 participantProvider.getParticipants(null,
153 if (!participantList.isEmpty()) {
154 this.supervisionHandler.handleSendDeCommissionMessage();
156 modelsProvider.deleteServiceTemplate(name, version);
159 var response = new CommissioningResponse();
160 response.setAffectedControlLoopDefinitions(
161 Collections.singletonList(new ToscaConceptIdentifier(name, version)));
167 * Get control loop node templates.
169 * @param clName the name of the control loop, null for all
170 * @param clVersion the version of the control loop, null for all
171 * @return list of control loop node templates
172 * @throws PfModelException on errors getting control loop definitions
174 public List<ToscaNodeTemplate> getControlLoopDefinitions(String clName, String clVersion) throws PfModelException {
177 ToscaTypedEntityFilter<ToscaNodeTemplate> nodeTemplateFilter = ToscaTypedEntityFilter
178 .<ToscaNodeTemplate>builder()
181 .type(CONTROL_LOOP_NODE_TYPE)
185 return clProvider.getFilteredNodeTemplates(nodeTemplateFilter);
189 * Get the control loop elements from a control loop node template.
191 * @param controlLoopNodeTemplate the control loop node template
192 * @return a list of the control loop element node templates in a control loop node template
193 * @throws PfModelException on errors get control loop element node templates
195 public List<ToscaNodeTemplate> getControlLoopElementDefinitions(ToscaNodeTemplate controlLoopNodeTemplate)
196 throws PfModelException {
197 if (!CONTROL_LOOP_NODE_TYPE.equals(controlLoopNodeTemplate.getType())) {
198 return Collections.emptyList();
201 if (MapUtils.isEmpty(controlLoopNodeTemplate.getProperties())) {
202 return Collections.emptyList();
205 @SuppressWarnings("unchecked")
206 List<Map<String, String>> controlLoopElements =
207 (List<Map<String, String>>) controlLoopNodeTemplate.getProperties().get("elements");
209 if (CollectionUtils.isEmpty(controlLoopElements)) {
210 return Collections.emptyList();
213 List<ToscaNodeTemplate> controlLoopElementList = new ArrayList<>();
215 controlLoopElementList.addAll(
218 .map(elementMap -> clProvider.getNodeTemplates(elementMap.get("name"),
219 elementMap.get("version")))
220 .flatMap(List::stream)
221 .collect(Collectors.toList())
225 return controlLoopElementList;
229 * Get the initial node types with common or instance properties.
231 * @param fullNodeTypes map of all the node types in the specified template
232 * @param common boolean to indicate whether common or instance properties are required
233 * @return node types map that only has common properties
234 * @throws PfModelException on errors getting node type with common properties
236 private Map<String, ToscaNodeType> getInitialNodeTypesMap(Map<String, ToscaNodeType> fullNodeTypes,
239 var tempNodeTypesMap = new HashMap<String, ToscaNodeType>();
241 fullNodeTypes.forEach((key, nodeType) -> {
242 var tempToscaNodeType = new ToscaNodeType();
243 tempToscaNodeType.setName(key);
245 var resultantPropertyMap = findCommonOrInstancePropsInNodeTypes(nodeType, common);
247 if (!resultantPropertyMap.isEmpty()) {
248 tempToscaNodeType.setProperties(resultantPropertyMap);
249 tempNodeTypesMap.put(key, tempToscaNodeType);
252 return tempNodeTypesMap;
255 private Map<String, ToscaProperty> findCommonOrInstancePropsInNodeTypes(ToscaNodeType nodeType, boolean common) {
257 var tempCommonPropertyMap = new HashMap<String, ToscaProperty>();
258 var tempInstancePropertyMap = new HashMap<String, ToscaProperty>();
260 nodeType.getProperties().forEach((propKey, prop) -> {
262 if (prop.getMetadata() != null) {
263 prop.getMetadata().forEach((k, v) -> {
264 if (k.equals("common") && v.equals("true") && common) {
265 tempCommonPropertyMap.put(propKey, prop);
266 } else if (k.equals("common") && v.equals("false") && !common) {
267 tempInstancePropertyMap.put(propKey, prop);
272 tempInstancePropertyMap.put(propKey, prop);
276 if (tempCommonPropertyMap.isEmpty() && !common) {
277 return tempInstancePropertyMap;
279 return tempCommonPropertyMap;
284 * Get the node types derived from those that have common properties.
286 * @param initialNodeTypes map of all the node types in the specified template
287 * @param filteredNodeTypes map of all the node types that have common or instance properties
288 * @return all node types that have common properties including their children
289 * @throws PfModelException on errors getting node type with common properties
291 private Map<String, ToscaNodeType> getFinalNodeTypesMap(Map<String, ToscaNodeType> initialNodeTypes,
292 Map<String, ToscaNodeType> filteredNodeTypes) {
293 for (var i = 0; i < initialNodeTypes.size(); i++) {
294 initialNodeTypes.forEach((key, nodeType) -> {
295 var tempToscaNodeType = new ToscaNodeType();
296 tempToscaNodeType.setName(key);
298 if (filteredNodeTypes.get(nodeType.getDerivedFrom()) != null) {
299 tempToscaNodeType.setName(key);
301 var finalProps = new HashMap<String, ToscaProperty>(
302 filteredNodeTypes.get(nodeType.getDerivedFrom()).getProperties());
304 tempToscaNodeType.setProperties(finalProps);
308 filteredNodeTypes.putIfAbsent(key, tempToscaNodeType);
312 return filteredNodeTypes;
316 * Get the requested node types with common or instance properties.
318 * @param common boolean indicating common or instance properties
319 * @param name the name of the definition to get, null for all definitions
320 * @param version the version of the definition to get, null for all definitions
321 * @return the node types with common or instance properties
322 * @throws PfModelException on errors getting node type properties
324 public Map<String, ToscaNodeType> getCommonOrInstancePropertiesFromNodeTypes(boolean common, String name,
325 String version) throws PfModelException {
326 var serviceTemplates = new ToscaServiceTemplates();
327 serviceTemplates.setServiceTemplates(modelsProvider.getServiceTemplateList(name, version));
328 var tempNodeTypesMap =
329 this.getInitialNodeTypesMap(serviceTemplates.getServiceTemplates().get(0).getNodeTypes(), common);
331 return this.getFinalNodeTypesMap(serviceTemplates.getServiceTemplates().get(0).getNodeTypes(),
337 * Get node templates with appropriate common or instance properties added.
339 * @param initialNodeTemplates map of all the node templates in the specified template
340 * @param nodeTypeProps map of all the node types that have common or instance properties including children
341 * @return all node templates with appropriate common or instance properties added
342 * @throws PfModelException on errors getting map of node templates with common or instance properties added
344 private Map<String, ToscaNodeTemplate> getDerivedCommonOrInstanceNodeTemplates(
345 Map<String, ToscaNodeTemplate> initialNodeTemplates, Map<String, ToscaNodeType> nodeTypeProps) {
347 var finalNodeTemplatesMap = new HashMap<String, ToscaNodeTemplate>();
349 initialNodeTemplates.forEach((templateKey, template) -> {
350 if (nodeTypeProps.containsKey(template.getType())) {
351 var finalMergedProps = new HashMap<String, Object>();
353 nodeTypeProps.get(template.getType()).getProperties().forEach(finalMergedProps::putIfAbsent);
355 template.setProperties(finalMergedProps);
357 finalNodeTemplatesMap.put(templateKey, template);
362 return finalNodeTemplatesMap;
366 * Get node templates with common properties added.
368 * @param common boolean indicating common or instance properties to be used
369 * @param name the name of the definition to use, null for all definitions
370 * @param version the version of the definition to use, null for all definitions
371 * @return the nodes templates with common or instance properties
372 * @throws PfModelException on errors getting common or instance properties from node_templates
374 public Map<String, ToscaNodeTemplate> getNodeTemplatesWithCommonOrInstanceProperties(boolean common, String name,
375 String version) throws PfModelException {
377 if (common && verifyIfInstancePropertiesExists()) {
378 throw new PfModelException(Status.BAD_REQUEST,
379 "Cannot create or edit common properties, delete all the instantiations first");
382 var commonOrInstanceNodeTypeProps =
383 this.getCommonOrInstancePropertiesFromNodeTypes(common, name, version);
385 var serviceTemplates = new ToscaServiceTemplates();
386 serviceTemplates.setServiceTemplates(filterToscaNodeTemplateInstance(
387 modelsProvider.getServiceTemplateList(name, version)));
389 return this.getDerivedCommonOrInstanceNodeTemplates(
390 serviceTemplates.getServiceTemplates().get(0).getToscaTopologyTemplate().getNodeTemplates(),
391 commonOrInstanceNodeTypeProps);
395 * Get the requested control loop definitions.
397 * @param name the name of the definition to get, null for all definitions
398 * @param version the version of the definition to get, null for all definitions
399 * @return the control loop definitions
400 * @throws PfModelException on errors getting control loop definitions
402 public ToscaServiceTemplate getToscaServiceTemplate(String name, String version) throws PfModelException {
403 var serviceTemplates = new ToscaServiceTemplates();
404 serviceTemplates.setServiceTemplates(modelsProvider.getServiceTemplateList(name, version));
405 return serviceTemplates.getServiceTemplates().get(0);
409 * Get the tosca service template with only required sections.
411 * @param name the name of the template to get, null for all definitions
412 * @param version the version of the template to get, null for all definitions
413 * @return the tosca service template
414 * @throws PfModelException on errors getting tosca service template
416 public String getToscaServiceTemplateReduced(String name, String version)
417 throws PfModelException {
419 var serviceTemplates = new ToscaServiceTemplates();
420 serviceTemplates.setServiceTemplates(modelsProvider.getServiceTemplateList(name, version));
422 ToscaServiceTemplate fullTemplate = filterToscaNodeTemplateInstance(
423 serviceTemplates.getServiceTemplates()).get(0);
425 var template = new HashMap<String, Object>();
426 template.put("tosca_definitions_version", fullTemplate.getToscaDefinitionsVersion());
427 template.put("data_types", fullTemplate.getDataTypes());
428 template.put("policy_types", fullTemplate.getPolicyTypes());
429 template.put("node_types", fullTemplate.getNodeTypes());
430 template.put("topology_template", fullTemplate.getToscaTopologyTemplate());
433 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(template);
435 } catch (JsonProcessingException e) {
436 throw new PfModelException(Status.BAD_REQUEST, "Converion to Json Schema failed", e);
441 * Get the requested json schema.
443 * @param section section of the tosca service template to get schema for
444 * @return the specified tosca service template or section Json Schema
445 * @throws PfModelException on errors with retrieving the classes
447 public String getToscaServiceTemplateSchema(String section) throws PfModelException {
448 var visitor = new SchemaFactoryWrapper();
453 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaDataType.class), visitor);
455 case "capability_types":
456 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaCapabilityType.class), visitor);
459 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaNodeType.class), visitor);
461 case "relationship_types":
462 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaRelationshipType.class), visitor);
465 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaPolicyType.class), visitor);
467 case "topology_template":
468 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaTopologyTemplate.class), visitor);
470 case "node_templates":
471 mapper.acceptJsonFormatVisitor(
472 mapper.getTypeFactory().constructCollectionType(List.class, ToscaNodeTemplate.class),
476 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaServiceTemplate.class), visitor);
479 var jsonSchema = visitor.finalSchema();
480 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
481 } catch (JsonProcessingException e) {
482 throw new PfModelException(Status.BAD_REQUEST, "Converion to Json Schema failed", e);
486 private List<ToscaServiceTemplate> filterToscaNodeTemplateInstance(List<ToscaServiceTemplate> serviceTemplates) {
488 List<ToscaServiceTemplate> toscaServiceTemplates = new ArrayList<>();
490 serviceTemplates.stream().forEach(serviceTemplate -> {
492 Map<String, ToscaNodeTemplate> toscaNodeTemplates = new HashMap<>();
494 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach((key, nodeTemplate) -> {
495 if (!nodeTemplate.getName().contains(INSTANCE_TEXT)) {
496 toscaNodeTemplates.put(key, nodeTemplate);
500 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().clear();
501 serviceTemplate.getToscaTopologyTemplate().setNodeTemplates(toscaNodeTemplates);
503 toscaServiceTemplates.add(serviceTemplate);
506 return toscaServiceTemplates;
510 * Validates to see if there is any instance properties saved.
512 * @return true if exists instance properties
514 private boolean verifyIfInstancePropertiesExists() {
515 return clProvider.getNodeTemplates(null, null).stream()
516 .anyMatch(nodeTemplate -> nodeTemplate.getKey().getName().contains(INSTANCE_TEXT));