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