2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.loop;
26 import com.google.gson.JsonObject;
28 import java.util.List;
31 import javax.persistence.EntityNotFoundException;
33 import org.onap.clamp.policy.microservice.MicroServicePolicy;
34 import org.onap.clamp.policy.microservice.MicroServicePolicyService;
35 import org.onap.clamp.policy.operational.OperationalPolicy;
36 import org.onap.clamp.policy.operational.OperationalPolicyService;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Service;
41 public class LoopService {
44 private LoopsRepository loopsRepository;
47 private MicroServicePolicyService microservicePolicyService;
50 private OperationalPolicyService operationalPolicyService;
52 Loop saveOrUpdateLoop(Loop loop) {
53 return loopsRepository.save(loop);
56 List<String> getClosedLoopNames() {
57 return loopsRepository.getAllLoopNames();
60 public Loop getLoop(String loopName) {
61 return loopsRepository.findById(loopName).orElse(null);
64 public void deleteLoop(String loopName) {
65 loopsRepository.deleteById(loopName);
69 * This method is used to refresh the DCAE deployment status fields.
71 * @param loop The loop instance to be modified
72 * @param deploymentId The deployment ID as returned by DCAE
73 * @param deploymentUrl The Deployment URL as returned by DCAE
75 public void updateDcaeDeploymentFields(Loop loop, String deploymentId, String deploymentUrl) {
76 loop.setDcaeDeploymentId(deploymentId);
77 loop.setDcaeDeploymentStatusUrl(deploymentUrl);
78 loopsRepository.save(loop);
81 public void updateLoopState(Loop loop, String newState) {
82 loop.setLastComputedState(LoopState.valueOf(newState));
83 loopsRepository.save(loop);
86 Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
87 Loop loop = findClosedLoopByName(loopName);
88 Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies);
89 loop.setOperationalPolicies(newPolicies);
90 return loopsRepository.save(loop);
93 Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
94 Loop loop = findClosedLoopByName(loopName);
95 Set<MicroServicePolicy> newPolicies = microservicePolicyService.updatePolicies(loop, newMicroservicePolicies);
96 loop.setMicroServicePolicies(newPolicies);
97 return loopsRepository.save(loop);
100 Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
101 Loop loop = findClosedLoopByName(loopName);
102 loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
103 return loopsRepository.save(loop);
106 MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
107 Loop loop = findClosedLoopByName(loopName);
108 return microservicePolicyService.getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
111 private Loop findClosedLoopByName(String loopName) {
112 return loopsRepository.findById(loopName)
113 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
117 * Api to refresh the Operational Policy UI window.
119 * @param loopName The loop Name
120 * @return The refreshed loop object
122 public Loop refreshOpPolicyJsonRepresentation(String loopName) {
123 Loop loop = findClosedLoopByName(loopName);
124 Set<OperationalPolicy> policyList = loop.getOperationalPolicies();
125 for (OperationalPolicy policy : policyList) {
126 policy.updateJsonRepresentation();
128 loop.setOperationalPolicies(policyList);
129 return loopsRepository.save(loop);