Fix the deploy cl payload issue
[clamp.git] / src / main / java / org / onap / clamp / loop / LoopOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T 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.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonObject;
29
30 import java.io.IOException;
31 import java.util.Iterator;
32 import java.util.Set;
33
34 import org.apache.camel.Exchange;
35 import org.apache.camel.Message;
36 import org.json.simple.JSONObject;
37 import org.json.simple.parser.JSONParser;
38 import org.json.simple.parser.ParseException;
39 import org.onap.clamp.policy.operational.OperationalPolicy;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42
43 /**
44  * Closed loop operations.
45  */
46 @Component
47 public class LoopOperation {
48
49     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(LoopOperation.class);
50     protected static final EELFLogger auditLogger = EELFManager.getInstance().getMetricsLogger();
51     private static final String DCAE_LINK_FIELD = "links";
52     private static final String DCAE_STATUS_FIELD = "status";
53     private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
54     private static final String DCAE_INPUTS = "inputs";
55     private static final String DCAE_DEPLOYMENT_PREFIX = "closedLoop_";
56     private static final String DCAE_DEPLOYMENT_SUFIX = "_deploymentId";
57     private static final String DEPLOYMENT_PARA = "dcaeDeployParameters";
58     private final LoopService loopService;
59
60     public enum TempLoopState {
61         NOT_SUBMITTED, SUBMITTED, DEPLOYED, NOT_DEPLOYED, PROCESSING, IN_ERROR;
62     }
63
64     /**
65      * The constructor.
66      * @param loopService The loop service
67      * @param refProp The clamp properties
68      */
69     @Autowired
70     public LoopOperation(LoopService loopService) {
71         this.loopService = loopService;
72     }
73
74     /**
75      * Get the payload used to send the deploy closed loop request.
76      *
77      * @param loop The loop
78      * @return The payload used to send deploy closed loop request
79      * @throws IOException IOException
80      */
81     public String getDeployPayload(Loop loop) throws IOException {
82         JsonObject globalProp = loop.getGlobalPropertiesJson();
83         JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARA);
84
85         String serviceTypeId = loop.getDcaeBlueprintId();
86
87         JsonObject rootObject = new JsonObject();
88         rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
89         if (deploymentProp != null) {
90             rootObject.add(DCAE_INPUTS, deploymentProp);
91         }
92         String apiBodyString = rootObject.toString();
93         logger.info("Dcae api Body String - " + apiBodyString);
94
95         return apiBodyString;
96     }
97
98     /**
99      * Get the deployment id.
100      *
101      * @param loop The loop
102      * @return The deployment id
103      * @throws IOException IOException
104      */
105     public String getDeploymentId(Loop loop) {
106         // Set the deploymentId if not present yet
107         String deploymentId = "";
108         // If model is already deployed then pass same deployment id
109         if (loop.getDcaeDeploymentId() != null && !loop.getDcaeDeploymentId().isEmpty()) {
110             deploymentId = loop.getDcaeDeploymentId();
111         } else {
112             deploymentId = DCAE_DEPLOYMENT_PREFIX + loop.getName() + DCAE_DEPLOYMENT_SUFIX;
113         }
114         return deploymentId;
115     }
116
117     /**
118      * Update the loop info.
119      *
120      * @param camelExchange The camel exchange
121      * @param loop The loop
122      * @param deploymentId The deployment id
123      * @throws ParseException The parse exception
124      */
125     public void updateLoopInfo(Exchange camelExchange, Loop loop, String deploymentId) throws ParseException {
126         Message in = camelExchange.getIn();
127         String msg = in.getBody(String.class);
128
129         JSONParser parser = new JSONParser();
130         Object obj0 = parser.parse(msg);
131         JSONObject jsonObj = (JSONObject) obj0;
132
133         JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);
134         String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);
135
136         // use http4 instead of http, because camel http4 component is used to do the http call
137         String newStatusUrl = statusUrl.replaceAll("http:", "http4:");
138
139         loop.setDcaeDeploymentId(deploymentId);
140         loop.setDcaeDeploymentStatusUrl(newStatusUrl);
141         loopService.saveOrUpdateLoop(loop);
142     }
143
144     /**
145      * Get the Closed Loop status based on the reply from Policy.
146      *
147      * @param statusCode The status code
148      * @return The state based on policy response
149      * @throws ParseException The parse exception
150      */
151     public String analysePolicyResponse(int statusCode) {
152         if (statusCode == 200) {
153             return TempLoopState.SUBMITTED.toString();
154         } else if (statusCode == 404) {
155             return TempLoopState.NOT_SUBMITTED.toString();
156         }
157         return TempLoopState.IN_ERROR.toString();
158     }
159
160     /**
161      * Get the name of the first Operational policy.
162      *
163      * @param loop The closed loop
164      * @return The name of the first operational policy
165      */
166     public String getOperationalPolicyName(Loop loop) {
167         Set<OperationalPolicy> opSet = (Set<OperationalPolicy>)loop.getOperationalPolicies();
168         Iterator<OperationalPolicy> iterator = opSet.iterator();
169         while (iterator.hasNext()) {
170             OperationalPolicy policy = iterator.next();
171             return policy.getName();
172         }
173         return null;
174     }
175
176     /**
177      * Get the Closed Loop status based on the reply from DCAE.
178      *
179      * @param camelExchange The camel exchange
180      * @return The state based on DCAE response
181      * @throws ParseException The parse exception
182      */
183     public String analyseDcaeResponse(Exchange camelExchange, Integer statusCode) throws ParseException {
184         if (statusCode == null) {
185             return TempLoopState.NOT_DEPLOYED.toString();
186         }
187         if (statusCode == 200) {
188             Message in = camelExchange.getIn();
189             String msg = in.getBody(String.class);
190
191             JSONParser parser = new JSONParser();
192             Object obj0 = parser.parse(msg);
193             JSONObject jsonObj = (JSONObject) obj0;
194
195             String opType = (String) jsonObj.get("operationType");
196             String status = (String) jsonObj.get("status");
197
198             // status = processing/successded/failed
199             if (status.equals("succeeded")) {
200                 if (opType.equals("install")) {
201                     return TempLoopState.DEPLOYED.toString();
202                 } else if (opType.equals("uninstall")) {
203                     return TempLoopState.NOT_DEPLOYED.toString();
204                 }
205             } else if (status.equals("processing")) {
206                 return TempLoopState.PROCESSING.toString();
207             }
208         } else if (statusCode == 404) {
209             return TempLoopState.NOT_DEPLOYED.toString();
210         }
211         return TempLoopState.IN_ERROR.toString();
212     }
213
214     /**
215      * Update the status of the closed loop based on the response from Policy and DCAE.
216      *
217      * @param loop The closed loop
218      * @param policyState The state get from Policy
219      * @param dcaeState The state get from DCAE
220      * @throws ParseException The parse exception
221      */
222     public LoopState updateLoopStatus(Loop loop, TempLoopState policyState, TempLoopState dcaeState) {
223         LoopState clState = LoopState.IN_ERROR;
224         if (policyState == TempLoopState.SUBMITTED) {
225             if (dcaeState == TempLoopState.DEPLOYED) {
226                 clState = LoopState.DEPLOYED;
227             } else if (dcaeState == TempLoopState.PROCESSING) {
228                 clState = LoopState.WAITING;
229             } else if (dcaeState == TempLoopState.NOT_DEPLOYED) {
230                 clState = LoopState.SUBMITTED;
231             }
232         } else if (policyState == TempLoopState.NOT_SUBMITTED) {
233             if (dcaeState == TempLoopState.NOT_DEPLOYED) {
234                 clState = LoopState.DESIGN;
235             }
236         }
237         loop.setLastComputedState(clState);
238         loopService.saveOrUpdateLoop(loop);
239         return clState;
240     }
241
242 }