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 Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
71 Loop loop = findClosedLoopByName(loopName);
72 Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies);
73 loop.setOperationalPolicies(newPolicies);
74 return loopsRepository.save(loop);
77 Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
78 Loop loop = findClosedLoopByName(loopName);
79 Set<MicroServicePolicy> newPolicies = microservicePolicyService.updatePolicies(loop, newMicroservicePolicies);
80 loop.setMicroServicePolicies(newPolicies);
81 return loopsRepository.save(loop);
84 Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
85 Loop loop = findClosedLoopByName(loopName);
86 loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
87 return loopsRepository.save(loop);
90 MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
91 Loop loop = findClosedLoopByName(loopName);
92 MicroServicePolicy newPolicies = microservicePolicyService.getAndUpdateMicroServicePolicy(loop,
93 newMicroservicePolicy);
97 private Loop findClosedLoopByName(String loopName) {
98 return loopsRepository.findById(loopName)
99 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));