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;
29 import org.apache.camel.CamelContext;
30 import org.apache.camel.Exchange;
31 import org.apache.camel.builder.ExchangeBuilder;
32 import org.onap.clamp.clds.config.ClampProperties;
33 import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService;
34 import org.onap.clamp.loop.template.PolicyModel;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
39 * The class implements the communication with the Policy Engine to retrieve
40 * policy models (tosca). It mainly delegates the physical calls to Camel
45 public class PolicyEngineServices {
46 private final CamelContext camelContext;
48 private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyEngineServices.class);
49 private static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
50 private static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
51 private static int retryInterval = 0;
52 private static int retryLimit = 1;
54 public static final String POLICY_RETRY_INTERVAL = "policy.retry.interval";
55 public static final String POLICY_RETRY_LIMIT = "policy.retry.limit";
58 public PolicyEngineServices(CamelContext camelContext, ClampProperties refProp) {
59 this.camelContext = camelContext;
61 if (refProp.getStringValue(POLICY_RETRY_LIMIT) != null) {
62 retryLimit = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_LIMIT));
64 if (refProp.getStringValue(POLICY_RETRY_INTERVAL) != null) {
65 retryInterval = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_INTERVAL));
69 public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion)
70 throws InterruptedException {
71 return new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion,
72 createPolicyAcronym(policyType));
75 public PolicyModel createPolicyModelFromPolicyEngine(BlueprintMicroService microService)
76 throws InterruptedException {
77 return createPolicyModelFromPolicyEngine(microService.getModelType(), microService.getModelVersion());
80 private static String createPolicyAcronym(String policyType) {
81 String[] policyNameArray = policyType.split("\\.");
82 return policyNameArray[policyNameArray.length - 1];
86 * This method can be used to download all policy types + data types defined in
89 * @return A yaml containing all policy Types and all data types
90 * @throws InterruptedException In case of issue when sleeping during the retry
92 public String downloadAllPolicies() throws InterruptedException {
93 return callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-policy-models");
97 * This method can be used to download a policy tosca model on the engine.
99 * @param policyType The policy type (id)
100 * @param policyVersion The policy version
101 * @return A string with the whole policy tosca model
102 * @throws InterruptedException In case of issue when sleeping during the retry
104 public String downloadOnePolicy(String policyType, String policyVersion) throws InterruptedException {
105 return callCamelRoute(ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType)
106 .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model");
109 private String callCamelRoute(Exchange exchange, String camelFlow) throws InterruptedException {
110 for (int i = 0; i < retryLimit; i++) {
111 Exchange exchangeResponse = camelContext.createProducerTemplate().send(camelFlow, exchange);
112 if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {
113 return (String) exchangeResponse.getIn().getBody();
115 logger.info("Policy query " + retryInterval + "ms before retrying ...");
116 // wait for a while and try to connect to DCAE again
117 Thread.sleep(retryInterval);