Merge "Fix imports in CsarInstallerItCase."
[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 com.google.gson.JsonObject;
27
28 import java.util.List;
29 import java.util.Set;
30
31 import javax.persistence.EntityNotFoundException;
32
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.Component;
38 import org.springframework.stereotype.Service;
39
40 @Service
41 @Component
42 public class LoopService {
43
44     private final LoopsRepository loopsRepository;
45     private final MicroservicePolicyService microservicePolicyService;
46     private final OperationalPolicyService operationalPolicyService;
47
48     /**
49      * Constructor.
50      */
51     public LoopService(LoopsRepository loopsRepository, MicroservicePolicyService microservicePolicyService,
52         OperationalPolicyService operationalPolicyService) {
53         this.loopsRepository = loopsRepository;
54         this.microservicePolicyService = microservicePolicyService;
55         this.operationalPolicyService = operationalPolicyService;
56     }
57
58     Loop saveOrUpdateLoop(Loop loop) {
59         return loopsRepository.save(loop);
60     }
61
62     List<String> getClosedLoopNames() {
63         return loopsRepository.getAllLoopNames();
64     }
65
66     public Loop getLoop(String loopName) {
67         return loopsRepository.findById(loopName).orElse(null);
68     }
69
70     public void deleteLoop(String loopName) {
71         loopsRepository.deleteById(loopName);
72     }
73
74     public void updateDcaeDeploymentFields(Loop loop, String deploymentId, String deploymentUrl) {
75         loop.setDcaeDeploymentId(deploymentId);
76         loop.setDcaeDeploymentStatusUrl(deploymentUrl);
77         loopsRepository.save(loop);
78     }
79
80     public void updateLoopState(Loop loop, String newState) {
81         loop.setLastComputedState(LoopState.valueOf(newState));
82         loopsRepository.save(loop);
83     }
84
85     Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
86         Loop loop = findClosedLoopByName(loopName);
87         Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies);
88         loop.setOperationalPolicies(newPolicies);
89         return loopsRepository.save(loop);
90     }
91
92     Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
93         Loop loop = findClosedLoopByName(loopName);
94         Set<MicroServicePolicy> newPolicies = microservicePolicyService.updatePolicies(loop, newMicroservicePolicies);
95         loop.setMicroServicePolicies(newPolicies);
96         return loopsRepository.save(loop);
97     }
98
99     Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
100         Loop loop = findClosedLoopByName(loopName);
101         loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
102         return loopsRepository.save(loop);
103     }
104
105     MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
106         Loop loop = findClosedLoopByName(loopName);
107         return microservicePolicyService.getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
108     }
109
110     private Loop findClosedLoopByName(String loopName) {
111         return loopsRepository.findById(loopName)
112             .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
113     }
114 }