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