2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.clds.client;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonObject;
30 import java.util.Iterator;
31 import java.util.LinkedHashMap;
32 import java.util.LinkedList;
33 import java.util.List;
34 import org.apache.camel.CamelContext;
35 import org.apache.camel.Exchange;
36 import org.apache.camel.builder.ExchangeBuilder;
37 import org.onap.clamp.clds.config.ClampProperties;
38 import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService;
39 import org.onap.clamp.clds.util.JsonUtils;
40 import org.onap.clamp.loop.template.PolicyModel;
41 import org.onap.clamp.loop.template.PolicyModelsService;
42 import org.onap.clamp.policy.pdpgroup.PdpGroup;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
45 import org.yaml.snakeyaml.Yaml;
49 * The class implements the communication with the Policy Engine to retrieve
50 * policy models (tosca). It mainly delegates the physical calls to Camel
54 public class PolicyEngineServices {
55 private final CamelContext camelContext;
57 private final PolicyModelsService policyModelsService;
59 private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyEngineServices.class);
60 private static int retryInterval = 0;
61 private static int retryLimit = 1;
63 public static final String POLICY_RETRY_INTERVAL = "policy.retry.interval";
64 public static final String POLICY_RETRY_LIMIT = "policy.retry.limit";
67 * Default constructor.
69 * @param camelContext Camel context bean
70 * @param clampProperties ClampProperties bean
71 * @param policyModelsService policyModel service
74 public PolicyEngineServices(CamelContext camelContext, ClampProperties clampProperties,
75 PolicyModelsService policyModelsService) {
76 this.camelContext = camelContext;
77 this.policyModelsService = policyModelsService;
78 if (clampProperties.getStringValue(POLICY_RETRY_LIMIT) != null) {
79 retryLimit = Integer.parseInt(clampProperties.getStringValue(POLICY_RETRY_LIMIT));
81 if (clampProperties.getStringValue(POLICY_RETRY_INTERVAL) != null) {
82 retryInterval = Integer.parseInt(clampProperties.getStringValue(POLICY_RETRY_INTERVAL));
87 * This method query Policy engine and create a PolicyModel object with type and version.
89 * @param policyType The policyType id
90 * @param policyVersion The policy version of that type
91 * @return A PolicyModel created from policyEngine data
93 public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion) {
94 return new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion);
98 * This method query Policy engine and create a PolicyModel object with type and version.
100 * @param microService microservice object instance
101 * @return A PolicyModel created from policyEngine data
103 public PolicyModel createPolicyModelFromPolicyEngine(BlueprintMicroService microService) {
104 return createPolicyModelFromPolicyEngine(microService.getModelType(), microService.getModelVersion());
108 * This method synchronize the clamp database and the policy engine.
109 * So it creates the required PolicyModel.
111 public void synchronizeAllPolicies() {
112 LinkedHashMap<String, Object> loadedYaml;
113 loadedYaml = new Yaml().load(downloadAllPolicies());
114 if (loadedYaml == null || loadedYaml.isEmpty()) {
115 logger.warn("getAllPolicyType yaml returned by policy engine could not be decoded, as it's null or empty");
119 LinkedHashMap<String, Object> policyTypesMap = (LinkedHashMap<String, Object>) loadedYaml
120 .get("policy_types");
121 policyTypesMap.entrySet().stream().forEach(entryPolicyType -> {
122 policyModelsService.createPolicyInDbIfNeeded(
123 createPolicyModelFromPolicyEngine(entryPolicyType.getKey(),
124 ((String) ((LinkedHashMap<String, Object>) entryPolicyType.getValue()).get("version"))));
129 * This method can be used to download all policy types + data types defined in
132 * @return A yaml containing all policy Types and all data types
134 public String downloadAllPolicies() {
135 return callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-policy-models",
140 * This method can be used to download a policy tosca model on the engine.
142 * @param policyType The policy type (id)
143 * @param policyVersion The policy version
144 * @return A string with the whole policy tosca model
146 public String downloadOnePolicy(String policyType, String policyVersion) {
147 return callCamelRoute(ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType)
148 .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model",
153 * This method can be used to download all Pdp Groups data from policy engine.
155 public void downloadPdpGroups() {
156 String responseBody =
157 callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-pdp-groups",
160 if (responseBody == null || responseBody.isEmpty()) {
161 logger.warn("getPdpGroups returned by policy engine could not be decoded, as it's null or empty");
165 JsonObject jsonObj = JsonUtils.GSON.fromJson(responseBody, JsonObject.class);
167 List<PdpGroup> pdpGroupList = new LinkedList<>();
168 JsonArray itemsArray = (JsonArray) jsonObj.get("groups");
170 Iterator it = itemsArray.iterator();
171 while (it.hasNext()) {
172 JsonObject item = (JsonObject) it.next();
173 PdpGroup pdpGroup = JsonUtils.GSON.fromJson(item.toString(), PdpGroup.class);
174 pdpGroupList.add(pdpGroup);
177 policyModelsService.updatePdpGroupInfo(pdpGroupList);
180 private String callCamelRoute(Exchange exchange, String camelFlow, String logMsg) {
181 for (int i = 0; i < retryLimit; i++) {
182 Exchange exchangeResponse = camelContext.createProducerTemplate().send(camelFlow, exchange);
183 if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {
184 return (String) exchangeResponse.getIn().getBody();
186 logger.info(logMsg + " query " + retryInterval + "ms before retrying ...");
187 // wait for a while and try to connect to DCAE again
189 Thread.sleep(retryInterval);
190 } catch (InterruptedException e) {
191 Thread.currentThread().interrupt();