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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.instantiation;
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;
44 * This class is dedicated to the Instantiation of Commissioned control loop.
46 public class ControlLoopInstantiationProvider implements Closeable {
47 private final ControlLoopProvider controlLoopProvider;
49 private static final Object lockit = new Object();
52 * Create a instantiation provider.
54 * @param databaseProviderParameters the parameters for database access
56 public ControlLoopInstantiationProvider(PolicyModelsProviderParameters databaseProviderParameters) {
58 controlLoopProvider = new ControlLoopProvider(databaseProviderParameters);
59 } catch (PfModelException e) {
60 throw new PfModelRuntimeException(e);
65 public void close() throws IOException {
66 controlLoopProvider.close();
70 * Create control loops.
72 * @param controlLoops the control loop
73 * @return the result of the instantiation operation
74 * @throws PfModelException on creation errors
76 public InstantiationResponse createControlLoops(ControlLoops controlLoops) throws PfModelException {
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");
86 controlLoopProvider.createControlLoops(controlLoops.getControlLoopList());
89 InstantiationResponse response = new InstantiationResponse();
90 response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
91 .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
97 * Update control loops.
99 * @param controlLoops the control loop
100 * @return the result of the instantiation operation
101 * @throws PfModelException on update errors
103 public InstantiationResponse updateControlLoops(ControlLoops controlLoops) throws PfModelException {
104 synchronized (lockit) {
105 controlLoopProvider.updateControlLoops(controlLoops.getControlLoopList());
108 InstantiationResponse response = new InstantiationResponse();
109 response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
110 .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
116 * Delete the control loop with the given name and version.
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
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");
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());
137 response.setAffectedControlLoops(Collections
138 .singletonList(controlLoopProvider.deleteControlLoop(name, version).getKey().asIdentifier()));
144 * Get the requested control loops.
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
151 public ControlLoops getControlLoops(String name, String version) throws PfModelException {
152 ControlLoops controlLoops = new ControlLoops();
153 controlLoops.setControlLoopList(controlLoopProvider.getControlLoops(name, version));
159 * Issue a command to control loops, setting their ordered state.
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
166 public InstantiationResponse issueControlLoopCommand(InstantiationCommand command)
167 throws ControlLoopException, PfModelException {
169 if (command.getOrderedState() == null) {
170 throw new ControlLoopException(Status.BAD_REQUEST, "ordered state invalid or not specified on command");
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);
180 controlLoopProvider.updateControlLoops(controlLoops);
183 InstantiationResponse response = new InstantiationResponse();
184 response.setAffectedControlLoops(command.getControlLoopIdentifierList());