Merge "Rework UI"
[clamp.git] / src / main / java / org / onap / clamp / loop / LoopService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop;
25
26 import java.util.List;
27 import java.util.Set;
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;
34
35 @Service
36 public class LoopService {
37
38     private final LoopsRepository loopsRepository;
39     private final MicroservicePolicyService microservicePolicyService;
40     private final OperationalPolicyService operationalPolicyService;
41
42     public LoopService(LoopsRepository loopsRepository,
43         MicroservicePolicyService microservicePolicyService,
44         OperationalPolicyService operationalPolicyService) {
45         this.loopsRepository = loopsRepository;
46         this.microservicePolicyService = microservicePolicyService;
47         this.operationalPolicyService = operationalPolicyService;
48     }
49
50     Loop saveOrUpdateLoop(Loop loop) {
51         return loopsRepository.save(loop);
52     }
53
54     List<String> getClosedLoopNames() {
55         return loopsRepository.getAllLoopNames();
56     }
57
58     Loop getLoop(String loopName){
59         return loopsRepository
60             .findById(loopName)
61             .orElse(null);
62     }
63
64     String getClosedLoopModelSVG(String loopName) {
65         Loop closedLoopByName = findClosedLoopByName(loopName);
66         return closedLoopByName.getSvgRepresentation();
67     }
68
69     Loop updateOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
70         Loop loop = findClosedLoopByName(loopName);
71         Set<OperationalPolicy> newPolicies = operationalPolicyService
72             .updatePolicies(loop, newOperationalPolicies);
73
74         loop.setOperationalPolicies(newPolicies);
75         return loopsRepository.save(loop);
76     }
77
78     Loop updateMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
79         Loop loop = findClosedLoopByName(loopName);
80         Set<MicroServicePolicy> newPolicies = microservicePolicyService
81             .updatePolicies(loop, newMicroservicePolicies);
82
83         loop.setMicroServicePolicies(newPolicies);
84         return loopsRepository.save(loop);
85     }
86
87     private Loop findClosedLoopByName(String loopName) {
88         return loopsRepository.findById(loopName)
89             .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
90     }
91 }