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.stereotype.Component;
38 import org.springframework.stereotype.Service;
42 public class LoopService {
44 private final LoopsRepository loopsRepository;
45 private final MicroservicePolicyService microservicePolicyService;
46 private final OperationalPolicyService operationalPolicyService;
51 public LoopService(LoopsRepository loopsRepository, MicroservicePolicyService microservicePolicyService,
52 OperationalPolicyService operationalPolicyService) {
53 this.loopsRepository = loopsRepository;
54 this.microservicePolicyService = microservicePolicyService;
55 this.operationalPolicyService = operationalPolicyService;
58 Loop saveOrUpdateLoop(Loop loop) {
59 return loopsRepository.save(loop);
62 List<String> getClosedLoopNames() {
63 return loopsRepository.getAllLoopNames();
66 public Loop getLoop(String loopName) {
67 return loopsRepository.findById(loopName).orElse(null);
70 public void deleteLoop(String loopName) {
71 loopsRepository.deleteById(loopName);
74 public void updateDcaeDeploymentFields(Loop loop, String deploymentId, String deploymentUrl) {
75 loop.setDcaeDeploymentId(deploymentId);
76 loop.setDcaeDeploymentStatusUrl(deploymentUrl);
77 loopsRepository.save(loop);
80 public void updateLoopState(Loop loop, String newState) {
81 loop.setLastComputedState(LoopState.valueOf(newState));
82 loopsRepository.save(loop);
85 Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
86 Loop loop = findClosedLoopByName(loopName);
87 Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies);
88 loop.setOperationalPolicies(newPolicies);
89 return loopsRepository.save(loop);
92 Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
93 Loop loop = findClosedLoopByName(loopName);
94 Set<MicroServicePolicy> newPolicies = microservicePolicyService.updatePolicies(loop, newMicroservicePolicies);
95 loop.setMicroServicePolicies(newPolicies);
96 return loopsRepository.save(loop);
99 Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
100 Loop loop = findClosedLoopByName(loopName);
101 loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
102 return loopsRepository.save(loop);
105 MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
106 Loop loop = findClosedLoopByName(loopName);
107 return microservicePolicyService.getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
110 private Loop findClosedLoopByName(String loopName) {
111 return loopsRepository.findById(loopName)
112 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));