Update UI to define Pdp Group
[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.util.List;
28 import java.util.Set;
29 import javax.persistence.EntityNotFoundException;
30 import org.apache.commons.lang3.RandomStringUtils;
31 import org.onap.clamp.loop.template.LoopTemplatesService;
32 import org.onap.clamp.loop.template.PolicyModel;
33 import org.onap.clamp.loop.template.PolicyModelsService;
34 import org.onap.clamp.policy.Policy;
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     Loop saveOrUpdateLoop(Loop loop) {
61         return loopsRepository.save(loop);
62     }
63
64     List<String> getClosedLoopNames() {
65         return loopsRepository.getAllLoopNames();
66     }
67
68     public Loop getLoop(String loopName) {
69         return loopsRepository.findById(loopName).orElse(null);
70     }
71
72     public void deleteLoop(String loopName) {
73         loopsRepository.deleteById(loopName);
74     }
75
76     /**
77      * Creates a Loop Instance from Loop Template Name.
78      *
79      * @param loopName Name of the Loop to be created
80      * @param templateName Loop Template to used for Loop
81      * @return Loop Instance
82      */
83     public Loop createLoopFromTemplate(String loopName, String templateName) {
84         return loopsRepository.save(new Loop(loopName,loopTemplateService.getLoopTemplate(templateName)));
85     }
86
87     /**
88      * This method is used to refresh the DCAE deployment status fields.
89      *
90      * @param loop          The loop instance to be modified
91      * @param deploymentId  The deployment ID as returned by DCAE
92      * @param deploymentUrl The Deployment URL as returned by DCAE
93      */
94     public void updateDcaeDeploymentFields(Loop loop, String deploymentId, String deploymentUrl) {
95         loop.setDcaeDeploymentId(deploymentId);
96         loop.setDcaeDeploymentStatusUrl(deploymentUrl);
97         loopsRepository.save(loop);
98     }
99
100     public void updateLoopState(Loop loop, String newState) {
101         loop.setLastComputedState(LoopState.valueOf(newState));
102         loopsRepository.save(loop);
103     }
104
105     /**
106      * This method add an operational policy to a loop instance.
107      *
108      * @param loopName The loop name
109      * @param policyType The policy model type
110      * @param policyVersion The policy model  version
111      * @return The loop modified
112      */
113     Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) {
114         Loop loop = getLoop(loopName);
115         PolicyModel policyModel = policyModelsService.getPolicyModel(policyType, policyVersion);
116         if (policyModel == null) {
117             return null;
118         }
119         loop.addOperationalPolicy(
120                 new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL", loop.getModelService().getName(),
121                         loop.getModelService().getVersion(), RandomStringUtils.randomAlphanumeric(3),
122                         RandomStringUtils.randomAlphanumeric(4)), loop, null, policyModel, null, null, null));
123         return loopsRepository.save(loop);
124     }
125
126     Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
127         Loop loop = findClosedLoopByName(loopName);
128         Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies);
129         loop.setOperationalPolicies(newPolicies);
130         return loopsRepository.save(loop);
131     }
132
133     Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
134         Loop loop = findClosedLoopByName(loopName);
135         Set<MicroServicePolicy> newPolicies = microservicePolicyService.updatePolicies(loop, newMicroservicePolicies);
136         loop.setMicroServicePolicies(newPolicies);
137         return loopsRepository.save(loop);
138     }
139
140     Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
141         Loop loop = findClosedLoopByName(loopName);
142         loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
143         return loopsRepository.save(loop);
144     }
145
146     MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
147         Loop loop = findClosedLoopByName(loopName);
148         return microservicePolicyService.getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
149     }
150
151     private Loop findClosedLoopByName(String loopName) {
152         return loopsRepository.findById(loopName)
153                 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
154     }
155
156     /**
157      * Api to refresh the Operational Policy UI window.
158      *
159      * @param loopName The loop Name
160      * @return The refreshed loop object
161      */
162     public Loop refreshOpPolicyJsonRepresentation(String loopName) {
163         Loop loop = findClosedLoopByName(loopName);
164         Set<OperationalPolicy> policyList = loop.getOperationalPolicies();
165         for (OperationalPolicy policy : policyList) {
166             policy.updateJsonRepresentation();
167         }
168         loop.setOperationalPolicies(policyList);
169         return loopsRepository.save(loop);
170     }
171 }