Add policy downloader
[clamp.git] / src / main / java / org / onap / clamp / clds / client / PolicyEngineServices.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 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.clds.client;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
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;
37
38 /**
39  * The class implements the communication with the Policy Engine to retrieve
40  * policy models (tosca). It mainly delegates the physical calls to Camel
41  * engine.
42  *
43  */
44 @Component
45 public class PolicyEngineServices {
46     private final CamelContext camelContext;
47
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;
53
54     public static final String POLICY_RETRY_INTERVAL = "policy.retry.interval";
55     public static final String POLICY_RETRY_LIMIT = "policy.retry.limit";
56
57     @Autowired
58     public PolicyEngineServices(CamelContext camelContext, ClampProperties refProp) {
59         this.camelContext = camelContext;
60
61         if (refProp.getStringValue(POLICY_RETRY_LIMIT) != null) {
62             retryLimit = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_LIMIT));
63         }
64         if (refProp.getStringValue(POLICY_RETRY_INTERVAL) != null) {
65             retryInterval = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_INTERVAL));
66         }
67     }
68
69     public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion)
70             throws InterruptedException {
71         return new PolicyModel(policyType, this.downloadOnePolicy(policyType, policyVersion), policyVersion,
72                 createPolicyAcronym(policyType));
73     }
74
75     public PolicyModel createPolicyModelFromPolicyEngine(BlueprintMicroService microService)
76             throws InterruptedException {
77         return createPolicyModelFromPolicyEngine(microService.getModelType(), microService.getModelVersion());
78     }
79
80     private static String createPolicyAcronym(String policyType) {
81         String[] policyNameArray = policyType.split("\\.");
82         return policyNameArray[policyNameArray.length - 1];
83     }
84
85     /**
86      * This method can be used to download all policy types + data types defined in
87      * policy engine.
88      * 
89      * @return A yaml containing all policy Types and all data types
90      * @throws InterruptedException In case of issue when sleeping during the retry
91      */
92     public String downloadAllPolicies() throws InterruptedException {
93         return callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-policy-models");
94     }
95
96     /**
97      * This method can be used to download a policy tosca model on the engine.
98      * 
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
103      */
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");
107     }
108
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();
114             } else {
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);
118             }
119         }
120         return "";
121     }
122 }