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