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;
29 import org.onap.clamp.policy.microservice.MicroservicePolicyService;
30 import org.onap.clamp.policy.operational.OperationalPolicyService;
31 import org.onap.clamp.policy.microservice.MicroServicePolicy;
32 import org.onap.clamp.policy.operational.OperationalPolicy;
33 import org.springframework.stereotype.Service;
36 public class LoopService {
38 private final LoopsRepository loopsRepository;
39 private final MicroservicePolicyService microservicePolicyService;
40 private final OperationalPolicyService operationalPolicyService;
42 public LoopService(LoopsRepository loopsRepository,
43 MicroservicePolicyService microservicePolicyService,
44 OperationalPolicyService operationalPolicyService) {
45 this.loopsRepository = loopsRepository;
46 this.microservicePolicyService = microservicePolicyService;
47 this.operationalPolicyService = operationalPolicyService;
50 Loop addNewLoop(Loop loop) {
51 return loopsRepository.save(loop);
54 List<String> getClosedLoopNames() {
55 return loopsRepository.getAllLoopNames();
58 Loop getLoop(String loopName){
59 return loopsRepository
64 String getClosedLoopModelSVG(String loopName) {
65 Loop closedLoopByName = findClosedLoopByName(loopName);
66 return closedLoopByName.getSvgRepresentation();
69 Loop updateOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
70 Loop loop = findClosedLoopByName(loopName);
71 Set<OperationalPolicy> newPolicies = operationalPolicyService
72 .updatePolicies(loop, newOperationalPolicies);
74 loop.setOperationalPolicies(newPolicies);
75 return loopsRepository.save(loop);
78 Loop updateMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
79 Loop loop = findClosedLoopByName(loopName);
80 Set<MicroServicePolicy> newPolicies = microservicePolicyService
81 .updatePolicies(loop, newMicroservicePolicies);
83 loop.setMicroServicePolicies(newPolicies);
84 return loopsRepository.save(loop);
87 private Loop findClosedLoopByName(String loopName) {
88 return loopsRepository.findById(loopName)
89 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));