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