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