22809e8c0fd6535be8df7f3c74eb570c2c6ce88f
[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.runtime.commissioning;
22
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.stream.Collectors;
30 import org.apache.commons.collections4.CollectionUtils;
31 import org.apache.commons.collections4.MapUtils;
32 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
33 import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse;
34 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.provider.PolicyModelsProvider;
38 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
44 import org.springframework.stereotype.Component;
45
46 /**
47  * This class provides the create, read and delete actions on Commissioning of Control Loop concepts in the database to
48  * the callers.
49  */
50 @Component
51 public class CommissioningProvider implements Closeable {
52     public static final String CONTROL_LOOP_NODE_TYPE = "org.onap.policy.clamp.controlloop.ControlLoop";
53
54     private final PolicyModelsProvider modelsProvider;
55     private final ControlLoopProvider clProvider;
56
57     private static final Object lockit = new Object();
58
59     /**
60      * Create a commissioning provider.
61      *
62      * @param controlLoopParameters the parameters for access to the database
63      * @throws PfModelRuntimeException on errors creating the database provider
64      */
65     public CommissioningProvider(ClRuntimeParameterGroup controlLoopParameters) {
66         try {
67             modelsProvider = new PolicyModelsProviderFactory()
68                     .createPolicyModelsProvider(controlLoopParameters.getDatabaseProviderParameters());
69         } catch (PfModelException e) {
70             throw new PfModelRuntimeException(e);
71         }
72
73         try {
74             clProvider = new ControlLoopProvider(controlLoopParameters.getDatabaseProviderParameters());
75         } catch (PfModelException e) {
76             throw new PfModelRuntimeException(e);
77         }
78     }
79
80     @Override
81     public void close() throws IOException {
82         try {
83             modelsProvider.close();
84             clProvider.close();
85         } catch (PfModelException e) {
86             throw new IOException("error closing modelsProvider", e);
87         }
88     }
89
90     /**
91      * Create control loops from a service template.
92      *
93      * @param serviceTemplate the service template
94      * @return the result of the commissioning operation
95      * @throws PfModelException on creation errors
96      */
97     public CommissioningResponse createControlLoopDefinitions(ToscaServiceTemplate serviceTemplate)
98             throws PfModelException {
99         synchronized (lockit) {
100             modelsProvider.createServiceTemplate(serviceTemplate);
101         }
102
103         var response = new CommissioningResponse();
104         // @formatter:off
105         response.setAffectedControlLoopDefinitions(serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
106                 .values()
107                 .stream()
108                 .map(template -> template.getKey().asIdentifier())
109                 .collect(Collectors.toList()));
110         // @formatter:on
111
112         return response;
113     }
114
115     /**
116      * Delete the control loop definition with the given name and version.
117      *
118      * @param name the name of the control loop definition to delete
119      * @param version the version of the control loop to delete
120      * @return the result of the deletion
121      * @throws PfModelException on deletion errors
122      */
123     public CommissioningResponse deleteControlLoopDefinition(String name, String version) throws PfModelException {
124         synchronized (lockit) {
125             modelsProvider.deleteServiceTemplate(name, version);
126         }
127
128         var response = new CommissioningResponse();
129         response.setAffectedControlLoopDefinitions(
130                 Collections.singletonList(new ToscaConceptIdentifier(name, version)));
131
132         return response;
133     }
134
135     /**
136      * Get control loop node templates.
137      *
138      * @param clName the name of the control loop, null for all
139      * @param clVersion the version of the control loop, null for all
140      * @return list of control loop node templates
141      * @throws PfModelException on errors getting control loop definitions
142      */
143     public List<ToscaNodeTemplate> getControlLoopDefinitions(String clName, String clVersion) throws PfModelException {
144
145         // @formatter:off
146         ToscaTypedEntityFilter<ToscaNodeTemplate> nodeTemplateFilter = ToscaTypedEntityFilter
147                 .<ToscaNodeTemplate>builder()
148                 .name(clName)
149                 .version(clVersion)
150                 .type(CONTROL_LOOP_NODE_TYPE)
151                 .build();
152         // @formatter:on
153
154         return clProvider.getFilteredNodeTemplates(nodeTemplateFilter);
155     }
156
157     /**
158      * Get the control loop elements from a control loop node template.
159      *
160      * @param controlLoopNodeTemplate the control loop node template
161      * @return a list of the control loop element node templates in a control loop node template
162      * @throws PfModelException on errors get control loop element node templates
163      */
164     public List<ToscaNodeTemplate> getControlLoopElementDefinitions(ToscaNodeTemplate controlLoopNodeTemplate)
165             throws PfModelException {
166         if (!CONTROL_LOOP_NODE_TYPE.equals(controlLoopNodeTemplate.getType())) {
167             return Collections.emptyList();
168         }
169
170         if (MapUtils.isEmpty(controlLoopNodeTemplate.getProperties())) {
171             return Collections.emptyList();
172         }
173
174         @SuppressWarnings("unchecked")
175         List<Map<String, String>> controlLoopElements =
176                 (List<Map<String, String>>) controlLoopNodeTemplate.getProperties().get("elements");
177
178         if (CollectionUtils.isEmpty(controlLoopElements)) {
179             return Collections.emptyList();
180         }
181
182         List<ToscaNodeTemplate> controlLoopElementList = new ArrayList<>();
183         // @formatter:off
184         controlLoopElementList.addAll(
185                 controlLoopElements
186                         .stream()
187                         .map(elementMap -> clProvider.getNodeTemplates(elementMap.get("name"),
188                                 elementMap.get("version")))
189                         .flatMap(List::stream)
190                         .collect(Collectors.toList())
191         );
192         // @formatter:on
193
194         return controlLoopElementList;
195     }
196
197     /**
198      * Get the requested control loop definitions.
199      *
200      * @param name the name of the definition to get, null for all definitions
201      * @param version the version of the definition to get, null for all definitions
202      * @return the control loop definitions
203      * @throws PfModelException on errors getting control loop definitions
204      */
205     public ToscaServiceTemplate getToscaServiceTemplate(String name, String version) throws PfModelException {
206         var serviceTemplates = new ToscaServiceTemplates();
207         serviceTemplates.setServiceTemplates(modelsProvider.getServiceTemplateList(name, version));
208         return serviceTemplates.getServiceTemplates().get(0);
209     }
210 }