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;
29 import com.google.gson.JsonObject;
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.Service;
40 public class LoopService {
42 private final LoopsRepository loopsRepository;
43 private final MicroservicePolicyService microservicePolicyService;
44 private final OperationalPolicyService operationalPolicyService;
49 public LoopService(LoopsRepository loopsRepository,
50 MicroservicePolicyService microservicePolicyService,
51 OperationalPolicyService operationalPolicyService) {
52 this.loopsRepository = loopsRepository;
53 this.microservicePolicyService = microservicePolicyService;
54 this.operationalPolicyService = operationalPolicyService;
57 public Loop saveOrUpdateLoop(Loop loop) {
58 return loopsRepository.save(loop);
61 List<String> getClosedLoopNames() {
62 return loopsRepository.getAllLoopNames();
65 public Loop getLoop(String loopName) {
66 return loopsRepository
71 String getClosedLoopModelSVG(String loopName) {
72 Loop closedLoopByName = findClosedLoopByName(loopName);
73 return closedLoopByName.getSvgRepresentation();
76 Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
77 Loop loop = findClosedLoopByName(loopName);
78 updateOperationalPolicies(loop, newOperationalPolicies);
79 return loopsRepository.save(loop);
82 Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
83 Loop loop = findClosedLoopByName(loopName);
84 updateMicroservicePolicies(loop, newMicroservicePolicies);
85 return loopsRepository.save(loop);
88 Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
89 Loop loop = findClosedLoopByName(loopName);
90 updateGlobalPropertiesJson(loop, newGlobalPropertiesJson);
91 return loopsRepository.save(loop);
94 MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
95 Loop loop = findClosedLoopByName(loopName);
96 MicroServicePolicy newPolicies = microservicePolicyService
97 .getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
101 private Loop updateOperationalPolicies(Loop loop, List<OperationalPolicy> newOperationalPolicies) {
102 Set<OperationalPolicy> newPolicies = operationalPolicyService
103 .updatePolicies(loop, newOperationalPolicies);
105 loop.setOperationalPolicies(newPolicies);
109 private Loop updateMicroservicePolicies(Loop loop, List<MicroServicePolicy> newMicroservicePolicies) {
110 Set<MicroServicePolicy> newPolicies = microservicePolicyService
111 .updatePolicies(loop, newMicroservicePolicies);
112 loop.setMicroServicePolicies(newPolicies);
116 private Loop updateGlobalPropertiesJson(Loop loop, JsonObject newGlobalPropertiesJson) {
117 loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
121 private Loop findClosedLoopByName(String loopName) {
122 return loopsRepository.findById(loopName)
123 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));