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;
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;
44 * Closed loop operations.
47 public class LoopOperation {
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;
60 public enum TempLoopState {
61 NOT_SUBMITTED, SUBMITTED, DEPLOYED, NOT_DEPLOYED, PROCESSING, IN_ERROR;
66 * @param loopService The loop service
67 * @param refProp The clamp properties
70 public LoopOperation(LoopService loopService) {
71 this.loopService = loopService;
75 * Get the payload used to send the deploy closed loop request.
77 * @param loop The loop
78 * @return The payload used to send deploy closed loop request
79 * @throws IOException IOException
81 public String getDeployPayload(Loop loop) throws IOException {
82 JsonObject globalProp = loop.getGlobalPropertiesJson();
83 JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARA);
85 String serviceTypeId = loop.getDcaeBlueprintId();
87 JsonObject rootObject = new JsonObject();
88 rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
89 if (deploymentProp != null) {
90 rootObject.add(DCAE_INPUTS, deploymentProp);
92 String apiBodyString = rootObject.toString();
93 logger.info("Dcae api Body String - " + apiBodyString);
99 * Get the deployment id.
101 * @param loop The loop
102 * @return The deployment id
103 * @throws IOException IOException
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();
112 deploymentId = DCAE_DEPLOYMENT_PREFIX + loop.getName() + DCAE_DEPLOYMENT_SUFIX;
118 * Update the loop info.
120 * @param camelExchange The camel exchange
121 * @param loop The loop
122 * @param deploymentId The deployment id
123 * @throws ParseException The parse exception
125 public void updateLoopInfo(Exchange camelExchange, Loop loop, String deploymentId) throws ParseException {
126 Message in = camelExchange.getIn();
127 String msg = in.getBody(String.class);
129 JSONParser parser = new JSONParser();
130 Object obj0 = parser.parse(msg);
131 JSONObject jsonObj = (JSONObject) obj0;
133 JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);
134 String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);
136 // use http4 instead of http, because camel http4 component is used to do the http call
137 String newStatusUrl = statusUrl.replaceAll("http:", "http4:");
139 loop.setDcaeDeploymentId(deploymentId);
140 loop.setDcaeDeploymentStatusUrl(newStatusUrl);
141 loopService.saveOrUpdateLoop(loop);
145 * Get the Closed Loop status based on the reply from Policy.
147 * @param statusCode The status code
148 * @return The state based on policy response
149 * @throws ParseException The parse exception
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();
157 return TempLoopState.IN_ERROR.toString();
161 * Get the name of the first Operational policy.
163 * @param loop The closed loop
164 * @return The name of the first operational policy
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();
177 * Get the Closed Loop status based on the reply from DCAE.
179 * @param camelExchange The camel exchange
180 * @return The state based on DCAE response
181 * @throws ParseException The parse exception
183 public String analyseDcaeResponse(Exchange camelExchange, Integer statusCode) throws ParseException {
184 if (statusCode == null) {
185 return TempLoopState.NOT_DEPLOYED.toString();
187 if (statusCode == 200) {
188 Message in = camelExchange.getIn();
189 String msg = in.getBody(String.class);
191 JSONParser parser = new JSONParser();
192 Object obj0 = parser.parse(msg);
193 JSONObject jsonObj = (JSONObject) obj0;
195 String opType = (String) jsonObj.get("operationType");
196 String status = (String) jsonObj.get("status");
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();
205 } else if (status.equals("processing")) {
206 return TempLoopState.PROCESSING.toString();
208 } else if (statusCode == 404) {
209 return TempLoopState.NOT_DEPLOYED.toString();
211 return TempLoopState.IN_ERROR.toString();
215 * Update the status of the closed loop based on the response from Policy and DCAE.
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
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;
232 } else if (policyState == TempLoopState.NOT_SUBMITTED) {
233 if (dcaeState == TempLoopState.NOT_DEPLOYED) {
234 clState = LoopState.DESIGN;
237 loop.setLastComputedState(clState);
238 loopService.saveOrUpdateLoop(loop);