7586a692c7f6d0593ae4fb96123f30dcacfd1b44
[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.instantiation;
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.stream.Collectors;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.Response.Status;
31 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
35 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
36 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
37 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.base.PfModelRuntimeException;
40 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42
43 /**
44  * This class is dedicated to the Instantiation of Commissioned control loop.
45  */
46 public class ControlLoopInstantiationProvider implements Closeable {
47     private final ControlLoopProvider controlLoopProvider;
48
49     private static final Object lockit = new Object();
50
51     /**
52      * Create a instantiation provider.
53      *
54      * @param databaseProviderParameters the parameters for database access
55      */
56     public ControlLoopInstantiationProvider(PolicyModelsProviderParameters databaseProviderParameters) {
57         try {
58             controlLoopProvider = new ControlLoopProvider(databaseProviderParameters);
59         } catch (PfModelException e) {
60             throw new PfModelRuntimeException(e);
61         }
62     }
63
64     @Override
65     public void close() throws IOException {
66         controlLoopProvider.close();
67     }
68
69     /**
70      * Create control loops.
71      *
72      * @param controlLoops the control loop
73      * @return the result of the instantiation operation
74      * @throws PfModelException on creation errors
75      */
76     public InstantiationResponse createControlLoops(ControlLoops controlLoops) throws PfModelException {
77
78         synchronized (lockit) {
79             for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
80                 ControlLoop checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier());
81                 if (checkControlLoop != null) {
82                     throw new PfModelException(Response.Status.BAD_REQUEST,
83                             controlLoop.getKey().asIdentifier() + " already defined");
84                 }
85             }
86             controlLoopProvider.createControlLoops(controlLoops.getControlLoopList());
87         }
88
89         InstantiationResponse response = new InstantiationResponse();
90         response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
91                 .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
92
93         return response;
94     }
95
96     /**
97      * Update control loops.
98      *
99      * @param controlLoops the control loop
100      * @return the result of the instantiation operation
101      * @throws PfModelException on update errors
102      */
103     public InstantiationResponse updateControlLoops(ControlLoops controlLoops) throws PfModelException {
104         synchronized (lockit) {
105             controlLoopProvider.updateControlLoops(controlLoops.getControlLoopList());
106         }
107
108         InstantiationResponse response = new InstantiationResponse();
109         response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
110                 .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
111
112         return response;
113     }
114
115     /**
116      * Delete the control loop with the given name and version.
117      *
118      * @param name the name of the control loop 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 InstantiationResponse deleteControlLoop(String name, String version) throws PfModelException {
124         InstantiationResponse response = new InstantiationResponse();
125         synchronized (lockit) {
126             List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version);
127             if (controlLoops.isEmpty()) {
128                 throw new PfModelException(Response.Status.NOT_FOUND, "Control Loop not found");
129             }
130             for (ControlLoop controlLoop : controlLoops) {
131                 if (!ControlLoopState.UNINITIALISED.equals(controlLoop.getState())) {
132                     throw new PfModelException(Response.Status.BAD_REQUEST,
133                             "Control Loop State is still " + controlLoop.getState());
134                 }
135             }
136
137             response.setAffectedControlLoops(Collections
138                     .singletonList(controlLoopProvider.deleteControlLoop(name, version).getKey().asIdentifier()));
139         }
140         return response;
141     }
142
143     /**
144      * Get the requested control loops.
145      *
146      * @param name the name of the control loop to get, null for all control loops
147      * @param version the version of the control loop to get, null for all control loops
148      * @return the control loops
149      * @throws PfModelException on errors getting control loops
150      */
151     public ControlLoops getControlLoops(String name, String version) throws PfModelException {
152         ControlLoops controlLoops = new ControlLoops();
153         controlLoops.setControlLoopList(controlLoopProvider.getControlLoops(name, version));
154
155         return controlLoops;
156     }
157
158     /**
159      * Issue a command to control loops, setting their ordered state.
160      *
161      * @param command the command to issue to control loops
162      * @return the result of the initiation command
163      * @throws PfModelException on errors setting the ordered state on the control loops
164      * @throws ControlLoopException on ordered state invalid
165      */
166     public InstantiationResponse issueControlLoopCommand(InstantiationCommand command)
167             throws ControlLoopException, PfModelException {
168
169         if (command.getOrderedState() == null) {
170             throw new ControlLoopException(Status.BAD_REQUEST, "ordered state invalid or not specified on command");
171         }
172
173         synchronized (lockit) {
174             List<ControlLoop> controlLoops = new ArrayList<>(command.getControlLoopIdentifierList().size());
175             for (ToscaConceptIdentifier id : command.getControlLoopIdentifierList()) {
176                 ControlLoop controlLoop = controlLoopProvider.getControlLoop(id);
177                 controlLoop.setCascadedOrderedState(command.getOrderedState());
178                 controlLoops.add(controlLoop);
179             }
180             controlLoopProvider.updateControlLoops(controlLoops);
181         }
182
183         InstantiationResponse response = new InstantiationResponse();
184         response.setAffectedControlLoops(command.getControlLoopIdentifierList());
185
186         return response;
187     }
188 }