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