Operational policy modification
[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.PolicyModel;
32 import org.onap.clamp.loop.template.PolicyModelsService;
33 import org.onap.clamp.policy.Policy;
34 import org.onap.clamp.policy.microservice.MicroServicePolicy;
35 import org.onap.clamp.policy.microservice.MicroServicePolicyService;
36 import org.onap.clamp.policy.operational.OperationalPolicy;
37 import org.onap.clamp.policy.operational.OperationalPolicyService;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Service;
40
41 @Service
42 public class LoopService {
43
44     @Autowired
45     private LoopsRepository loopsRepository;
46
47     @Autowired
48     private MicroServicePolicyService microservicePolicyService;
49
50     @Autowired
51     private OperationalPolicyService operationalPolicyService;
52
53     @Autowired
54     private PolicyModelsService policyModelsService;
55
56     Loop saveOrUpdateLoop(Loop loop) {
57         return loopsRepository.save(loop);
58     }
59
60     List<String> getClosedLoopNames() {
61         return loopsRepository.getAllLoopNames();
62     }
63
64     public Loop getLoop(String loopName) {
65         return loopsRepository.findById(loopName).orElse(null);
66     }
67
68     public void deleteLoop(String loopName) {
69         loopsRepository.deleteById(loopName);
70     }
71
72     /**
73      * This method is used to refresh the DCAE deployment status fields.
74      *
75      * @param loop          The loop instance to be modified
76      * @param deploymentId  The deployment ID as returned by DCAE
77      * @param deploymentUrl The Deployment URL as returned by DCAE
78      */
79     public void updateDcaeDeploymentFields(Loop loop, String deploymentId, String deploymentUrl) {
80         loop.setDcaeDeploymentId(deploymentId);
81         loop.setDcaeDeploymentStatusUrl(deploymentUrl);
82         loopsRepository.save(loop);
83     }
84
85     public void updateLoopState(Loop loop, String newState) {
86         loop.setLastComputedState(LoopState.valueOf(newState));
87         loopsRepository.save(loop);
88     }
89
90     Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) {
91         Loop loop = findClosedLoopByName(loopName);
92         PolicyModel policyModel = policyModelsService.getPolicyModel(policyType, policyVersion);
93         if (policyModel == null) {
94             return null;
95         }
96         loop.addOperationalPolicy(
97                 new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL", loop.getModelService().getName(),
98                         loop.getModelService().getVersion(), RandomStringUtils.randomAlphanumeric(3),
99                         RandomStringUtils.randomAlphanumeric(4)), loop, null, policyModel));
100         return loopsRepository.save(loop);
101     }
102
103     Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
104         Loop loop = findClosedLoopByName(loopName);
105         Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies);
106         loop.setOperationalPolicies(newPolicies);
107         return loopsRepository.save(loop);
108     }
109
110     Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
111         Loop loop = findClosedLoopByName(loopName);
112         Set<MicroServicePolicy> newPolicies = microservicePolicyService.updatePolicies(loop, newMicroservicePolicies);
113         loop.setMicroServicePolicies(newPolicies);
114         return loopsRepository.save(loop);
115     }
116
117     Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
118         Loop loop = findClosedLoopByName(loopName);
119         loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
120         return loopsRepository.save(loop);
121     }
122
123     MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
124         Loop loop = findClosedLoopByName(loopName);
125         return microservicePolicyService.getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
126     }
127
128     private Loop findClosedLoopByName(String loopName) {
129         return loopsRepository.findById(loopName)
130                 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
131     }
132
133     /**
134      * Api to refresh the Operational Policy UI window.
135      *
136      * @param loopName The loop Name
137      * @return The refreshed loop object
138      */
139     public Loop refreshOpPolicyJsonRepresentation(String loopName) {
140         Loop loop = findClosedLoopByName(loopName);
141         Set<OperationalPolicy> policyList = loop.getOperationalPolicies();
142         for (OperationalPolicy policy : policyList) {
143             policy.updateJsonRepresentation();
144         }
145         loop.setOperationalPolicies(policyList);
146         return loopsRepository.save(loop);
147     }
148 }