Fix checkstyle issue
[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     /**
69      * This method is used to refresh the DCAE deployment status fields.
70      * 
71      * @param loop          The loop instance to be modified
72      * @param deploymentId  The deployment ID as returned by DCAE
73      * @param deploymentUrl The Deployment URL as returned by DCAE
74      */
75     public void updateDcaeDeploymentFields(Loop loop, String deploymentId, String deploymentUrl) {
76         loop.setDcaeDeploymentId(deploymentId);
77         loop.setDcaeDeploymentStatusUrl(deploymentUrl);
78         loopsRepository.save(loop);
79     }
80
81     public void updateLoopState(Loop loop, String newState) {
82         loop.setLastComputedState(LoopState.valueOf(newState));
83         loopsRepository.save(loop);
84     }
85
86     Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
87         Loop loop = findClosedLoopByName(loopName);
88         Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies);
89         loop.setOperationalPolicies(newPolicies);
90         return loopsRepository.save(loop);
91     }
92
93     Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
94         Loop loop = findClosedLoopByName(loopName);
95         Set<MicroServicePolicy> newPolicies = microservicePolicyService.updatePolicies(loop, newMicroservicePolicies);
96         loop.setMicroServicePolicies(newPolicies);
97         return loopsRepository.save(loop);
98     }
99
100     Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
101         Loop loop = findClosedLoopByName(loopName);
102         loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
103         return loopsRepository.save(loop);
104     }
105
106     MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
107         Loop loop = findClosedLoopByName(loopName);
108         return microservicePolicyService.getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy);
109     }
110
111     private Loop findClosedLoopByName(String loopName) {
112         return loopsRepository.findById(loopName)
113                 .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
114     }
115
116     /**
117     * Api to refresh the Operational Policy UI window.
118     * 
119     * @param loopName The loop Name
120     * @return The refreshed loop object
121     */
122     public Loop refreshOpPolicyJsonRepresentation(String loopName) {
123         Loop loop = findClosedLoopByName(loopName);
124         Set<OperationalPolicy> policyList = loop.getOperationalPolicies();
125         for (OperationalPolicy policy : policyList) {
126             policy.updateJsonRepresentation();
127         }
128         loop.setOperationalPolicies(policyList);
129         return loopsRepository.save(loop);
130     }
131 }