2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.loop;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonObject;
30 import java.io.IOException;
31 import java.util.Iterator;
33 import java.util.UUID;
35 import org.apache.camel.Exchange;
36 import org.apache.camel.Message;
37 import org.json.simple.JSONObject;
38 import org.json.simple.parser.JSONParser;
39 import org.json.simple.parser.ParseException;
40 import org.onap.clamp.policy.operational.OperationalPolicy;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
45 * Closed loop operations.
48 public class LoopOperation {
50 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(LoopOperation.class);
51 protected static final EELFLogger auditLogger = EELFManager.getInstance().getMetricsLogger();
52 private static final String DCAE_LINK_FIELD = "links";
53 private static final String DCAE_STATUS_FIELD = "status";
54 private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
55 private static final String DCAE_INPUTS = "inputs";
56 private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_";
57 private static final String DEPLOYMENT_PARA = "dcaeDeployParameters";
58 private final LoopService loopService;
60 public enum TempLoopState {
61 NOT_SUBMITTED, SUBMITTED, DEPLOYED, NOT_DEPLOYED, PROCESSING, IN_ERROR;
70 * The clamp properties
73 public LoopOperation(LoopService loopService) {
74 this.loopService = loopService;
78 * Get the payload used to send the deploy closed loop request.
82 * @return The payload used to send deploy closed loop request
86 public String getDeployPayload(Loop loop) throws IOException {
87 JsonObject globalProp = loop.getGlobalPropertiesJson();
88 JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARA);
90 String serviceTypeId = loop.getDcaeBlueprintId();
92 JsonObject rootObject = new JsonObject();
93 rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
94 if (deploymentProp != null) {
95 rootObject.add(DCAE_INPUTS, deploymentProp);
97 String apiBodyString = rootObject.toString();
98 logger.info("Dcae api Body String - " + apiBodyString);
100 return apiBodyString;
104 * Get the deployment id.
108 * @return The deployment id
109 * @throws IOException
112 public String getDeploymentId(Loop loop) {
113 // Set the deploymentId if not present yet
114 String deploymentId = "";
115 // If model is already deployed then pass same deployment id
116 if (loop.getDcaeDeploymentId() != null && !loop.getDcaeDeploymentId().isEmpty()) {
117 deploymentId = loop.getDcaeDeploymentId();
119 deploymentId = DCAE_DEPLOYMENT_PREFIX + UUID.randomUUID();
125 * Update the loop info.
127 * @param camelExchange
131 * @param deploymentId
133 * @throws ParseException
134 * The parse exception
136 public void updateLoopInfo(Exchange camelExchange, Loop loop, String deploymentId) throws ParseException {
137 Message in = camelExchange.getIn();
138 String msg = in.getBody(String.class);
140 JSONParser parser = new JSONParser();
141 Object obj0 = parser.parse(msg);
142 JSONObject jsonObj = (JSONObject) obj0;
144 JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);
145 String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);
147 // use http4 instead of http, because camel http4 component is used to do the
149 String newStatusUrl = statusUrl.replaceAll("http:", "http4:").replaceAll("https:", "https4:");
151 loop.setDcaeDeploymentId(deploymentId);
152 loop.setDcaeDeploymentStatusUrl(newStatusUrl);
153 loopService.saveOrUpdateLoop(loop);
157 * Get the Closed Loop status based on the reply from Policy.
161 * @return The state based on policy response
162 * @throws ParseException
163 * The parse exception
165 public String analysePolicyResponse(int statusCode) {
166 if (statusCode == 200) {
167 return TempLoopState.SUBMITTED.toString();
168 } else if (statusCode == 404) {
169 return TempLoopState.NOT_SUBMITTED.toString();
171 return TempLoopState.IN_ERROR.toString();
175 * Get the name of the first Operational policy.
179 * @return The name of the first operational policy
181 public String getOperationalPolicyName(Loop loop) {
182 Set<OperationalPolicy> opSet = loop.getOperationalPolicies();
183 Iterator<OperationalPolicy> iterator = opSet.iterator();
184 while (iterator.hasNext()) {
185 OperationalPolicy policy = iterator.next();
186 return policy.getName();
192 * Get the Closed Loop status based on the reply from DCAE.
194 * @param camelExchange
196 * @return The state based on DCAE response
197 * @throws ParseException
198 * The parse exception
200 public String analyseDcaeResponse(Exchange camelExchange, Integer statusCode) throws ParseException {
201 if (statusCode == null) {
202 return TempLoopState.NOT_DEPLOYED.toString();
204 if (statusCode == 200) {
205 Message in = camelExchange.getIn();
206 String msg = in.getBody(String.class);
208 JSONParser parser = new JSONParser();
209 Object obj0 = parser.parse(msg);
210 JSONObject jsonObj = (JSONObject) obj0;
212 String opType = (String) jsonObj.get("operationType");
213 String status = (String) jsonObj.get("status");
215 // status = processing/successded/failed
216 if (status.equals("succeeded")) {
217 if (opType.equals("install")) {
218 return TempLoopState.DEPLOYED.toString();
219 } else if (opType.equals("uninstall")) {
220 return TempLoopState.NOT_DEPLOYED.toString();
222 } else if (status.equals("processing")) {
223 return TempLoopState.PROCESSING.toString();
225 } else if (statusCode == 404) {
226 return TempLoopState.NOT_DEPLOYED.toString();
228 return TempLoopState.IN_ERROR.toString();
232 * Update the status of the closed loop based on the response from Policy and
238 * The state get from Policy
240 * The state get from DCAE
241 * @throws ParseException
242 * The parse exception
244 public LoopState updateLoopStatus(Loop loop, TempLoopState policyState, TempLoopState dcaeState) {
245 LoopState clState = LoopState.IN_ERROR;
246 if (policyState == TempLoopState.SUBMITTED) {
247 if (dcaeState == TempLoopState.DEPLOYED) {
248 clState = LoopState.DEPLOYED;
249 } else if (dcaeState == TempLoopState.PROCESSING) {
250 clState = LoopState.WAITING;
251 } else if (dcaeState == TempLoopState.NOT_DEPLOYED) {
252 clState = LoopState.SUBMITTED;
254 } else if (policyState == TempLoopState.NOT_SUBMITTED) {
255 if (dcaeState == TempLoopState.NOT_DEPLOYED) {
256 clState = LoopState.DESIGN;
259 loop.setLastComputedState(clState);
260 loopService.saveOrUpdateLoop(loop);