Add info in the SVG
[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 import java.io.IOException;
28 import java.util.List;
29 import java.util.Set;
30 import javax.persistence.EntityNotFoundException;
31 import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
32 import org.onap.clamp.loop.template.LoopTemplatesService;
33 import org.onap.clamp.loop.template.PolicyModel;
34 import org.onap.clamp.loop.template.PolicyModelsService;
35 import org.onap.clamp.policy.microservice.MicroServicePolicy;
36 import org.onap.clamp.policy.microservice.MicroServicePolicyService;
37 import org.onap.clamp.policy.operational.OperationalPolicy;
38 import org.onap.clamp.policy.operational.OperationalPolicyService;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Service;
41
42 @Service
43 public class LoopService {
44
45     @Autowired
46     private LoopsRepository loopsRepository;
47
48     @Autowired
49     private MicroServicePolicyService microservicePolicyService;
50
51     @Autowired
52     private OperationalPolicyService operationalPolicyService;
53
54     @Autowired
55     private PolicyModelsService policyModelsService;
56
57     @Autowired
58     private LoopTemplatesService loopTemplateService;
59
60     @Autowired
61     private ToscaConverterWithDictionarySupport toscaConverter;
62
63     Loop saveOrUpdateLoop(Loop loop) {
64         return loopsRepository.save(loop);
65     }
66
67     List<String> getClosedLoopNames() {
68         return loopsRepository.getAllLoopNames();
69     }
70
71     public Loop getLoop(String loopName) {
72         return loopsRepository.findById(loopName).orElse(null);
73     }
74
75     public void deleteLoop(String loopName) {
76         loopsRepository.deleteById(loopName);
77     }
78
79     /**
80      * Creates a Loop Instance from Loop Template Name.
81      *
82      * @param loopName     Name of the Loop to be created
83      * @param templateName Loop Template to used for Loop
84      * @return Loop Instance
85      */
86     public Loop createLoopFromTemplate(String loopName, String templateName) {
87         return loopsRepository
88                 .save(new Loop(loopName, loopTemplateService.getLoopTemplate(templateName), toscaConverter));
89     }
90
91     /**
92      * This method is used to refresh the DCAE deployment status fields.
93      *
94      * @param loop          The loop instance to be modified
95      * @param deploymentId  The deployment ID as returned by DCAE
96      * @param deploymentUrl The Deployment URL as returned by DCAE
97      */
98     public void updateDcaeDeploymentFields(Loop loop, String deploymentId, String deploymentUrl) {
99         loop.setDcaeDeploymentId(deploymentId);
100         loop.setDcaeDeploymentStatusUrl(deploymentUrl);
101         loopsRepository.saveAndFlush(loop);
102     }
103
104     public void updateLoopState(Loop loop, String newState) {
105         loop.setLastComputedState(LoopState.valueOf(newState));
106         loopsRepository.save(loop);
107     }
108
109     /**
110      * This method add an operational policy to a loop instance.
111      * This creates an operational policy from the policy model info and not the loop element model
112      *
113      * @param loopName      The loop name
114      * @param policyType    The policy model type
115      * @param policyVersion The policy model  version
116      * @return The loop modified
117      */
118     Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) throws IOException {
119         Loop loop = getLoop(loopName);
120         PolicyModel policyModel = policyModelsService.getPolicyModel(policyType, policyVersion);
121         Set<OperationalPolicy> opPolicySet = loop.getOperationalPolicies();
122         for (OperationalPolicy opPolicy : opPolicySet) {
123             if (opPolicy.getPolicyModel().equals(policyModel)) {
124                 throw new IllegalArgumentException(
125                         "This type of Operational Policy is already added to the loop. Please choose another one.");
126             }
127         }
128         if (policyModel == null) {
129             return null;
130         }
131         loop.addOperationalPolicy(
132                 new OperationalPolicy(loop, loop.getModelService(), policyModel, toscaConverter));
133         return loopsRepository.saveAndFlush(loop);
134     }
135
136     /**
137      * This method remove an operational policy to a loop instance.
138      *
139      * @param loopName      The loop name
140      * @param policyType    The policy model type
141      * @param policyVersion The policy model version
142      * @return The loop modified
143      */
144     Loop removeOperationalPolicy(String loopName, String policyType, String policyVersion) {
145         Loop loop = getLoop(loopName);
146         PolicyModel policyModel = policyModelsService.getPolicyModel(policyType, policyVersion);
147         if (policyModel == null) {
148             return null;
149         }
150         for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) {
151             if (opPolicy.getPolicyModel().getPolicyModelType().equals(policyType)
152                     && opPolicy.getPolicyModel().getVersion().equals(policyVersion)) {
153                 loop.removeOperationalPolicy(opPolicy);
154                 break;
155             }
156         }
157         return loopsRepository.saveAndFlush(loop);
158     }
159
160     Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
161         Loop loop = findClosedLoopByName(loopName);
162         Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies);
163         loop.setOperationalPolicies(newPolicies);
164         return loopsRepository.save(loop);
165     }
166
167     Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
168         Loop loop = findClosedLoopByName(loopName);
169         Set<MicroServicePolicy> newPolicies = microservicePolicyService.updatePolicies(loop, newMicroservicePolicies);
170         loop.setMicroServicePolicies(newPolicies);
171         return loopsRepository.save(loop);
172     }
173
174     Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
175         Loop loop = findClosedLoopByName(loopName);
176         loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
177         return loopsRepository.save(loop);
178     }
179
180     MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
181         Loop loop = findClosedLoopByName(loopName);
182         return microservicePolicyService.getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
183     }
184
185     private Loop findClosedLoopByName(String loopName) {
186         return loopsRepository.findById(loopName)
187                 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
188     }
189 }
190