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);
68 public void updateDcaeDeploymentFields(Loop loop, String deploymentId, String deploymentUrl) {
69 loop.setDcaeDeploymentId(deploymentId);
70 loop.setDcaeDeploymentStatusUrl(deploymentUrl);
71 loopsRepository.save(loop);
74 public void updateLoopState(Loop loop, String newState) {
75 loop.setLastComputedState(LoopState.valueOf(newState));
76 loopsRepository.save(loop);
79 Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
80 Loop loop = findClosedLoopByName(loopName);
81 Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies);
82 loop.setOperationalPolicies(newPolicies);
83 return loopsRepository.save(loop);
86 Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
87 Loop loop = findClosedLoopByName(loopName);
88 Set<MicroServicePolicy> newPolicies = microservicePolicyService.updatePolicies(loop, newMicroservicePolicies);
89 loop.setMicroServicePolicies(newPolicies);
90 return loopsRepository.save(loop);
93 Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
94 Loop loop = findClosedLoopByName(loopName);
95 loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
96 return loopsRepository.save(loop);
99 MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
100 Loop loop = findClosedLoopByName(loopName);
101 return microservicePolicyService.getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
104 private Loop findClosedLoopByName(String loopName) {
105 return loopsRepository.findById(loopName)
106 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));