Add template and tosca model entities and repositories
[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.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Service;
39
40 @Service
41 public class LoopService {
42
43     @Autowired
44     private LoopsRepository loopsRepository;
45
46     @Autowired
47     private MicroServicePolicyService microservicePolicyService;
48
49     @Autowired
50     private OperationalPolicyService operationalPolicyService;
51
52     Loop saveOrUpdateLoop(Loop loop) {
53         return loopsRepository.save(loop);
54     }
55
56     List<String> getClosedLoopNames() {
57         return loopsRepository.getAllLoopNames();
58     }
59
60     public Loop getLoop(String loopName) {
61         return loopsRepository.findById(loopName).orElse(null);
62     }
63
64     public void deleteLoop(String loopName) {
65         loopsRepository.deleteById(loopName);
66     }
67
68     public void updateDcaeDeploymentFields(Loop loop, String deploymentId, String deploymentUrl) {
69         loop.setDcaeDeploymentId(deploymentId);
70         loop.setDcaeDeploymentStatusUrl(deploymentUrl);
71         loopsRepository.save(loop);
72     }
73
74     public void updateLoopState(Loop loop, String newState) {
75         loop.setLastComputedState(LoopState.valueOf(newState));
76         loopsRepository.save(loop);
77     }
78
79     Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
80         Loop loop = findClosedLoopByName(loopName);
81         Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies);
82         loop.setOperationalPolicies(newPolicies);
83         return loopsRepository.save(loop);
84     }
85
86     Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
87         Loop loop = findClosedLoopByName(loopName);
88         Set<MicroServicePolicy> newPolicies = microservicePolicyService.updatePolicies(loop, newMicroservicePolicies);
89         loop.setMicroServicePolicies(newPolicies);
90         return loopsRepository.save(loop);
91     }
92
93     Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
94         Loop loop = findClosedLoopByName(loopName);
95         loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
96         return loopsRepository.save(loop);
97     }
98
99     MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
100         Loop loop = findClosedLoopByName(loopName);
101         return microservicePolicyService.getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
102     }
103
104     private Loop findClosedLoopByName(String loopName) {
105         return loopsRepository.findById(loopName)
106                 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
107     }
108 }