d230eb976b16c748448d536c3b26d7d030891a2d
[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.util.JsonUtils;
33 import org.onap.clamp.policy.microservice.MicroServicePolicy;
34 import org.onap.clamp.policy.operational.OperationalPolicy;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Controller;
37
38 @Controller
39 public class LoopController {
40
41     private final LoopService loopService;
42     private static final Type OPERATIONAL_POLICY_TYPE = new TypeToken<List<OperationalPolicy>>() {}.getType();
43
44     private static final Type MICROSERVICE_POLICY_TYPE = new TypeToken<List<MicroServicePolicy>>() {}.getType();
45
46     @Autowired
47     public LoopController(LoopService loopService) {
48         this.loopService = loopService;
49     }
50
51     public Loop createLoop(String loopName, String templateName) {
52         return loopService.createLoopFromTemplate(loopName, templateName);
53     }
54
55     public List<String> getLoopNames() {
56         return loopService.getClosedLoopNames();
57     }
58
59     public Loop getLoop(String loopName) {
60         return loopService.getLoop(loopName);
61     }
62
63     /**
64      * Update the Operational Policy properties.
65      *
66      * @param loopName                The loop name
67      * @param operationalPoliciesJson The new Operational Policy properties
68      * @return The updated loop
69      */
70     public Loop updateOperationalPolicies(String loopName, JsonArray operationalPoliciesJson) {
71         List<OperationalPolicy> operationalPolicies = JsonUtils.GSON_JPA_MODEL.fromJson(operationalPoliciesJson,
72                 OPERATIONAL_POLICY_TYPE);
73         return loopService.updateAndSaveOperationalPolicies(loopName, operationalPolicies);
74     }
75
76     /**
77      * Update the whole array of MicroService policies properties.
78      *
79      * @param loopName                 The loop name
80      * @param microServicePoliciesJson The array of all MicroService policies
81      *                                 properties
82      * @return The updated loop
83      */
84     public Loop updateMicroservicePolicies(String loopName, JsonArray microServicePoliciesJson) {
85         List<MicroServicePolicy> microservicePolicies = JsonUtils.GSON_JPA_MODEL.fromJson(microServicePoliciesJson,
86                 MICROSERVICE_POLICY_TYPE);
87         return loopService.updateAndSaveMicroservicePolicies(loopName, microservicePolicies);
88     }
89
90     /**
91      * Update the global properties.
92      *
93      * @param loopName         The loop name
94      * @param globalProperties The updated global properties
95      * @return The updated loop
96      */
97     public Loop updateGlobalPropertiesJson(String loopName, JsonObject globalProperties) {
98         return loopService.updateAndSaveGlobalPropertiesJson(loopName, globalProperties);
99     }
100
101     /**
102      * This method add an operational policy to a loop instance.
103      *
104      * @param loopName      The loop name
105      * @param policyType    The policy model type
106      * @param policyVersion The policy model  version
107      * @return The loop modified
108      */
109     public Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) throws IOException {
110         return loopService.addOperationalPolicy(loopName, policyType, policyVersion);
111     }
112
113     /**
114      * This method remove an operational policy to a loop instance.
115      *
116      * @param loopName      The loop name
117      * @param policyType    The policy model type
118      * @param policyVersion The policy model  version
119      * @return The loop modified
120      */
121     public Loop removeOperationalPolicy(String loopName, String policyType, String policyVersion) {
122         return loopService.removeOperationalPolicy(loopName, policyType, policyVersion);
123     }
124
125     /**
126      * This method deletes the loop.
127      *
128      * @param loopName The loop Name
129      */
130     public void deleteLoop(String loopName) {
131         loopService.deleteLoop(loopName);
132     }
133
134     /**
135      * Update one MicroService policy properties.
136      *
137      * @param loopName              The loop name
138      * @param newMicroservicePolicy The new MicroService policy properties
139      * @return The updated MicroService policy
140      */
141     public MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
142         return loopService.updateMicroservicePolicy(loopName, newMicroservicePolicy);
143     }
144
145     /**
146      * Get the SVG representation of the loop.
147      *
148      * @param loopName The loop name
149      * @return The SVG representation
150      */
151     public String getSvgRepresentation(String loopName) {
152         Loop loop = loopService.getLoop(loopName);
153         return loop != null ? loop.getSvgRepresentation() : null;
154     }
155
156     /**
157      * Refresh the Operational Policy Json representation of the loop.
158      *
159      * @param loopName The loop name
160      * @return The refreshed Loop
161      */
162     public Loop refreshOpPolicyJsonRepresentation(String loopName) {
163         return loopService.refreshOpPolicyJsonRepresentation(loopName);
164     }
165 }