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 java.util.List;
28 import javax.persistence.EntityNotFoundException;
30 import com.google.gson.JsonObject;
31 import org.onap.clamp.policy.microservice.MicroservicePolicyService;
32 import org.onap.clamp.policy.operational.OperationalPolicyService;
33 import org.onap.clamp.policy.microservice.MicroServicePolicy;
34 import org.onap.clamp.policy.operational.OperationalPolicy;
35 import org.springframework.stereotype.Service;
38 public class LoopService {
40 private final LoopsRepository loopsRepository;
41 private final MicroservicePolicyService microservicePolicyService;
42 private final OperationalPolicyService operationalPolicyService;
44 public LoopService(LoopsRepository loopsRepository,
45 MicroservicePolicyService microservicePolicyService,
46 OperationalPolicyService operationalPolicyService) {
47 this.loopsRepository = loopsRepository;
48 this.microservicePolicyService = microservicePolicyService;
49 this.operationalPolicyService = operationalPolicyService;
52 Loop saveOrUpdateLoop(Loop loop) {
53 return loopsRepository.save(loop);
56 List<String> getClosedLoopNames() {
57 return loopsRepository.getAllLoopNames();
60 Loop getLoop(String loopName){
61 return loopsRepository
66 String getClosedLoopModelSVG(String loopName) {
67 Loop closedLoopByName = findClosedLoopByName(loopName);
68 return closedLoopByName.getSvgRepresentation();
71 Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
72 Loop loop = findClosedLoopByName(loopName);
73 updateOperationalPolicies(loop, newOperationalPolicies);
74 return loopsRepository.save(loop);
77 Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
78 Loop loop = findClosedLoopByName(loopName);
79 updateMicroservicePolicies(loop, newMicroservicePolicies);
80 return loopsRepository.save(loop);
83 Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
84 Loop loop = findClosedLoopByName(loopName);
85 updateGlobalPropertiesJson(loop, newGlobalPropertiesJson);
86 return loopsRepository.save(loop);
89 MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
90 Loop loop = findClosedLoopByName(loopName);
91 MicroServicePolicy newPolicies = microservicePolicyService
92 .getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
96 private Loop updateOperationalPolicies(Loop loop, List<OperationalPolicy> newOperationalPolicies) {
97 Set<OperationalPolicy> newPolicies = operationalPolicyService
98 .updatePolicies(loop, newOperationalPolicies);
100 loop.setOperationalPolicies(newPolicies);
104 private Loop updateMicroservicePolicies(Loop loop, List<MicroServicePolicy> newMicroservicePolicies) {
105 Set<MicroServicePolicy> newPolicies = microservicePolicyService
106 .updatePolicies(loop, newMicroservicePolicies);
107 loop.setMicroServicePolicies(newPolicies);
111 private Loop updateGlobalPropertiesJson(Loop loop, JsonObject newGlobalPropertiesJson) {
112 loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
116 private Loop findClosedLoopByName(String loopName) {
117 return loopsRepository.findById(loopName)
118 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));