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
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;
36
37 @Service
38 public class LoopService {
39
40     private final LoopsRepository loopsRepository;
41     private final MicroservicePolicyService microservicePolicyService;
42     private final OperationalPolicyService operationalPolicyService;
43
44     public LoopService(LoopsRepository loopsRepository,
45         MicroservicePolicyService microservicePolicyService,
46         OperationalPolicyService operationalPolicyService) {
47         this.loopsRepository = loopsRepository;
48         this.microservicePolicyService = microservicePolicyService;
49         this.operationalPolicyService = operationalPolicyService;
50     }
51
52     Loop saveOrUpdateLoop(Loop loop) {
53         return loopsRepository.save(loop);
54     }
55
56     List<String> getClosedLoopNames() {
57         return loopsRepository.getAllLoopNames();
58     }
59
60     Loop getLoop(String loopName){
61         return loopsRepository
62             .findById(loopName)
63             .orElse(null);
64     }
65
66     String getClosedLoopModelSVG(String loopName) {
67         Loop closedLoopByName = findClosedLoopByName(loopName);
68         return closedLoopByName.getSvgRepresentation();
69     }
70
71     Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
72         Loop loop = findClosedLoopByName(loopName);
73         updateOperationalPolicies(loop, newOperationalPolicies);
74         return loopsRepository.save(loop);
75     }
76
77     Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
78         Loop loop = findClosedLoopByName(loopName);
79         updateMicroservicePolicies(loop, newMicroservicePolicies);
80         return loopsRepository.save(loop);
81     }
82
83     Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
84         Loop loop = findClosedLoopByName(loopName);
85         updateGlobalPropertiesJson(loop, newGlobalPropertiesJson);
86         return loopsRepository.save(loop);
87     }
88
89     MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
90         Loop loop = findClosedLoopByName(loopName);
91         MicroServicePolicy newPolicies = microservicePolicyService
92                 .getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
93         return newPolicies;
94     }
95
96     private Loop updateOperationalPolicies(Loop loop, List<OperationalPolicy> newOperationalPolicies) {
97         Set<OperationalPolicy> newPolicies = operationalPolicyService
98                 .updatePolicies(loop, newOperationalPolicies);
99
100         loop.setOperationalPolicies(newPolicies);
101         return loop;
102     }
103
104     private Loop updateMicroservicePolicies(Loop loop, List<MicroServicePolicy> newMicroservicePolicies) {
105         Set<MicroServicePolicy> newPolicies = microservicePolicyService
106                 .updatePolicies(loop, newMicroservicePolicies);
107         loop.setMicroServicePolicies(newPolicies);
108         return loop;
109     }
110
111     private Loop updateGlobalPropertiesJson(Loop loop, JsonObject newGlobalPropertiesJson) {
112         loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
113         return loop;
114     }
115
116     private Loop findClosedLoopByName(String loopName) {
117         return loopsRepository.findById(loopName)
118             .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
119     }
120 }