Remove policy from PEF when removing op policy
[clamp.git] / src / main / java / org / onap / clamp / loop / LoopController.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.JsonArray;
27 import com.google.gson.JsonObject;
28 import com.google.gson.reflect.TypeToken;
29 import java.io.IOException;
30 import java.lang.reflect.Type;
31 import java.util.List;
32 import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
33 import org.onap.clamp.clds.util.JsonUtils;
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.Controller;
40
41 @Controller
42 public class LoopController {
43
44     private final LoopService loopService;
45
46     private final ToscaConverterWithDictionarySupport toscaConverter;
47
48     private final OperationalPolicyService operationalPolicyService;
49
50     private final MicroServicePolicyService microServicePolicyService;
51
52     private static final Type OPERATIONAL_POLICY_TYPE = new TypeToken<List<OperationalPolicy>>() {
53     }.getType();
54
55     private static final Type MICROSERVICE_POLICY_TYPE = new TypeToken<List<MicroServicePolicy>>() {
56     }.getType();
57
58
59     /**
60      * Constructor.
61      *
62      * @param loopService               loopService
63      * @param operationalPolicyService  operationalPolicyService
64      * @param microServicePolicyService microServicePolicyService
65      * @param toscaConverter            toscaConverter
66      */
67     @Autowired
68     public LoopController(LoopService loopService, OperationalPolicyService operationalPolicyService,
69                           MicroServicePolicyService microServicePolicyService,
70                           ToscaConverterWithDictionarySupport toscaConverter) {
71         this.loopService = loopService;
72         this.toscaConverter = toscaConverter;
73         this.operationalPolicyService = operationalPolicyService;
74         this.microServicePolicyService = microServicePolicyService;
75     }
76
77     public Loop createLoop(String loopName, String templateName) {
78         return loopService.createLoopFromTemplate(loopName, templateName);
79     }
80
81     public List<String> getLoopNames() {
82         return loopService.getClosedLoopNames();
83     }
84
85     public Loop getLoop(String loopName) {
86         return loopService.getLoop(loopName);
87     }
88
89     /**
90      * Update the Operational Policy properties.
91      *
92      * @param loopName                The loop name
93      * @param operationalPoliciesJson The new Operational Policy properties
94      * @return The updated loop
95      */
96     public Loop updateOperationalPolicies(String loopName, JsonArray operationalPoliciesJson) {
97         List<OperationalPolicy> operationalPolicies = JsonUtils.GSON_JPA_MODEL.fromJson(operationalPoliciesJson,
98                 OPERATIONAL_POLICY_TYPE);
99         return loopService.updateAndSaveOperationalPolicies(loopName, operationalPolicies);
100     }
101
102     /**
103      * Update the whole array of MicroService policies properties.
104      *
105      * @param loopName                 The loop name
106      * @param microServicePoliciesJson The array of all MicroService policies
107      *                                 properties
108      * @return The updated loop
109      */
110     public Loop updateMicroservicePolicies(String loopName, JsonArray microServicePoliciesJson) {
111         List<MicroServicePolicy> microservicePolicies = JsonUtils.GSON_JPA_MODEL.fromJson(microServicePoliciesJson,
112                 MICROSERVICE_POLICY_TYPE);
113         return loopService.updateAndSaveMicroservicePolicies(loopName, microservicePolicies);
114     }
115
116     /**
117      * Update the global properties.
118      *
119      * @param loopName         The loop name
120      * @param globalProperties The updated global properties
121      * @return The updated loop
122      */
123     public Loop updateGlobalPropertiesJson(String loopName, JsonObject globalProperties) {
124         return loopService.updateAndSaveGlobalPropertiesJson(loopName, globalProperties);
125     }
126
127     /**
128      * This method add an operational policy to a loop instance.
129      *
130      * @param loopName      The loop name
131      * @param policyType    The policy model type
132      * @param policyVersion The policy model  version
133      * @return The loop modified
134      */
135     public Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) throws IOException {
136         return loopService.addOperationalPolicy(loopName, policyType, policyVersion);
137     }
138
139     /**
140      * This method removes an operational policy from a loop instance.
141      *
142      * @param loopName      The loop name
143      * @param policyType    The policy model type
144      * @param policyVersion The policy model version
145      * @return The loop modified
146      */
147     public Loop removeOperationalPolicy(String loopName, String policyType, String policyVersion) {
148         return loopService.removeOperationalPolicy(loopName, policyType, policyVersion);
149     }
150
151     /**
152      * This method deletes the loop.
153      *
154      * @param loopName The loop Name
155      */
156     public void deleteLoop(String loopName) {
157         loopService.deleteLoop(loopName);
158     }
159
160     /**
161      * Update one MicroService policy properties.
162      *
163      * @param loopName              The loop name
164      * @param newMicroservicePolicy The new MicroService policy properties
165      * @return The updated MicroService policy
166      */
167     public MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
168         return loopService.updateMicroservicePolicy(loopName, newMicroservicePolicy);
169     }
170
171     /**
172      * Get the SVG representation of the loop.
173      *
174      * @param loopName The loop name
175      * @return The SVG representation
176      */
177     public String getSvgRepresentation(String loopName) {
178         Loop loop = loopService.getLoop(loopName);
179         return loop != null ? loop.getSvgRepresentation() : null;
180     }
181
182     /**
183      * Refresh the Operational Policy Json representation of the loop.
184      *
185      * @param loop                  The loop
186      * @param operationalPolicyName The operational policy name that needs a refresh
187      * @return The loop object
188      */
189     public Loop refreshOperationalPolicyJsonRepresentation(Loop loop, String operationalPolicyName) {
190         for (OperationalPolicy operationalPolicy : loop.getOperationalPolicies()) {
191             if (operationalPolicy.getName().equals(operationalPolicyName)) {
192                 this.operationalPolicyService
193                         .refreshOperationalPolicyJsonRepresentation(operationalPolicy, toscaConverter);
194             }
195         }
196         return loop;
197     }
198
199     /**
200      * Refresh the Config Policy Json representation of the loop.
201      *
202      * @param loop                   The loop
203      * @param microServicePolicyName The microservice policy name that needs a refresh
204      * @return The loop object
205      */
206     public Loop refreshMicroServicePolicyJsonRepresentation(Loop loop, String microServicePolicyName) {
207         for (MicroServicePolicy microServicePolicy : loop.getMicroServicePolicies()) {
208             if (microServicePolicy.getName().equals(microServicePolicyName)) {
209                 this.microServicePolicyService
210                         .refreshMicroServicePolicyJsonRepresentation(microServicePolicy, toscaConverter, loop);
211             }
212         }
213         return loop;
214     }
215 }