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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.commissioning;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.fasterxml.jackson.databind.PropertyNamingStrategies;
26 import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
27 import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
28 import java.io.Closeable;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
34 import java.util.stream.Collectors;
35 import org.apache.commons.collections4.CollectionUtils;
36 import org.apache.commons.collections4.MapUtils;
37 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
38 import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse;
39 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.base.PfModelRuntimeException;
42 import org.onap.policy.models.provider.PolicyModelsProvider;
43 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityType;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
55 import org.springframework.stereotype.Component;
58 * This class provides the create, read and delete actions on Commissioning of Control Loop concepts in the database to
62 public class CommissioningProvider implements Closeable {
63 public static final String CONTROL_LOOP_NODE_TYPE = "org.onap.policy.clamp.controlloop.ControlLoop";
65 private final PolicyModelsProvider modelsProvider;
66 private final ControlLoopProvider clProvider;
68 private static final Object lockit = new Object();
71 * Create a commissioning provider.
73 * @param controlLoopParameters the parameters for access to the database
74 * @throws PfModelRuntimeException on errors creating the database provider
76 public CommissioningProvider(ClRuntimeParameterGroup controlLoopParameters) {
78 modelsProvider = new PolicyModelsProviderFactory()
79 .createPolicyModelsProvider(controlLoopParameters.getDatabaseProviderParameters());
80 } catch (PfModelException e) {
81 throw new PfModelRuntimeException(e);
85 clProvider = new ControlLoopProvider(controlLoopParameters.getDatabaseProviderParameters());
86 } catch (PfModelException e) {
87 throw new PfModelRuntimeException(e);
92 public void close() throws IOException {
94 modelsProvider.close();
96 } catch (PfModelException e) {
97 throw new IOException("error closing modelsProvider", e);
102 * Create control loops from a service template.
104 * @param serviceTemplate the service template
105 * @return the result of the commissioning operation
106 * @throws PfModelException on creation errors
108 public CommissioningResponse createControlLoopDefinitions(ToscaServiceTemplate serviceTemplate)
109 throws PfModelException {
110 synchronized (lockit) {
111 modelsProvider.createServiceTemplate(serviceTemplate);
114 var response = new CommissioningResponse();
116 response.setAffectedControlLoopDefinitions(serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
119 .map(template -> template.getKey().asIdentifier())
120 .collect(Collectors.toList()));
127 * Delete the control loop definition with the given name and version.
129 * @param name the name of the control loop definition to delete
130 * @param version the version of the control loop to delete
131 * @return the result of the deletion
132 * @throws PfModelException on deletion errors
134 public CommissioningResponse deleteControlLoopDefinition(String name, String version) throws PfModelException {
135 synchronized (lockit) {
136 modelsProvider.deleteServiceTemplate(name, version);
139 var response = new CommissioningResponse();
140 response.setAffectedControlLoopDefinitions(
141 Collections.singletonList(new ToscaConceptIdentifier(name, version)));
147 * Get control loop node templates.
149 * @param clName the name of the control loop, null for all
150 * @param clVersion the version of the control loop, null for all
151 * @return list of control loop node templates
152 * @throws PfModelException on errors getting control loop definitions
154 public List<ToscaNodeTemplate> getControlLoopDefinitions(String clName, String clVersion) throws PfModelException {
157 ToscaTypedEntityFilter<ToscaNodeTemplate> nodeTemplateFilter = ToscaTypedEntityFilter
158 .<ToscaNodeTemplate>builder()
161 .type(CONTROL_LOOP_NODE_TYPE)
165 return clProvider.getFilteredNodeTemplates(nodeTemplateFilter);
169 * Get the control loop elements from a control loop node template.
171 * @param controlLoopNodeTemplate the control loop node template
172 * @return a list of the control loop element node templates in a control loop node template
173 * @throws PfModelException on errors get control loop element node templates
175 public List<ToscaNodeTemplate> getControlLoopElementDefinitions(ToscaNodeTemplate controlLoopNodeTemplate)
176 throws PfModelException {
177 if (!CONTROL_LOOP_NODE_TYPE.equals(controlLoopNodeTemplate.getType())) {
178 return Collections.emptyList();
181 if (MapUtils.isEmpty(controlLoopNodeTemplate.getProperties())) {
182 return Collections.emptyList();
185 @SuppressWarnings("unchecked")
186 List<Map<String, String>> controlLoopElements =
187 (List<Map<String, String>>) controlLoopNodeTemplate.getProperties().get("elements");
189 if (CollectionUtils.isEmpty(controlLoopElements)) {
190 return Collections.emptyList();
193 List<ToscaNodeTemplate> controlLoopElementList = new ArrayList<>();
195 controlLoopElementList.addAll(
198 .map(elementMap -> clProvider.getNodeTemplates(elementMap.get("name"),
199 elementMap.get("version")))
200 .flatMap(List::stream)
201 .collect(Collectors.toList())
205 return controlLoopElementList;
209 * Get the requested control loop definitions.
211 * @param name the name of the definition to get, null for all definitions
212 * @param version the version of the definition to get, null for all definitions
213 * @return the control loop definitions
214 * @throws PfModelException on errors getting control loop definitions
216 public ToscaServiceTemplate getToscaServiceTemplate(String name, String version) throws PfModelException {
217 var serviceTemplates = new ToscaServiceTemplates();
218 serviceTemplates.setServiceTemplates(modelsProvider.getServiceTemplateList(name, version));
219 return serviceTemplates.getServiceTemplates().get(0);
223 * Get the requested json schema.
225 * @param section section of the tosca service template to get schema for
226 * @return the specified tosca service template or section Json Schema
227 * @throws PfModelException on errors with retrieving the classes
228 * @throws JsonProcessingException on errors generating the schema
230 public String getToscaServiceTemplateSchema(String section) throws PfModelException, JsonProcessingException {
231 ObjectMapper mapper = new ObjectMapper();
232 mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
233 SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
237 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaDataType.class), visitor);
239 case "capability_types":
240 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaCapabilityType.class), visitor);
243 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaNodeType.class), visitor);
245 case "relationship_types":
246 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaRelationshipType.class), visitor);
249 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaPolicyType.class), visitor);
251 case "topology_template":
252 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaTopologyTemplate.class), visitor);
254 case "node_templates":
255 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaNodeTemplate.class), visitor);
258 mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaServiceTemplate.class), visitor);
261 JsonSchema jsonSchema = visitor.finalSchema();
262 String response = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);