Get policy in CsarInstaller
[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.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35
36 /**
37  * The class implements the communication with the Policy Engine to retrieve
38  * policy models (tosca). It mainly delegates the physical calls to Camel
39  * engine.
40  *
41  */
42 @Component
43 public class PolicyEngineServices {
44     private final CamelContext camelContext;
45
46     private final ClampProperties refProp;
47
48     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyEngineServices.class);
49     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
50     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
51     public static final String POLICY_RETRY_INTERVAL = "policy.retry.interval";
52     public static final String POLICY_RETRY_LIMIT = "policy.retry.limit";
53
54     @Autowired
55     public PolicyEngineServices(CamelContext camelContext, ClampProperties refProp) {
56         this.refProp = refProp;
57         this.camelContext = camelContext;
58     }
59
60     private void downloadAllPolicies() {
61         /*
62          * Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
63          * .withProperty("blueprintResourceId",
64          * resourceUuid).withProperty("blueprintServiceId", serviceUuid)
65          * .withProperty("blueprintName", artifactName).build();
66          * metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory");
67          * 
68          * Exchange exchangeResponse =
69          * camelContext.createProducerTemplate().send("direct:get-all-policy-models",
70          * myCamelExchange);
71          */
72     }
73
74     /**
75      * This method can be used to download a policy tosca model on the engine.
76      * 
77      * @param policyType    The policy type (id)
78      * @param policyVersion The policy version
79      * @return A string with the whole policy tosca model
80      * @throws InterruptedException in case of issue when sleeping during the retry
81      */
82     public String downloadOnePolicy(String policyType, String policyVersion) throws InterruptedException {
83         int retryInterval = 0;
84         int retryLimit = 1;
85         if (refProp.getStringValue(POLICY_RETRY_LIMIT) != null) {
86             retryLimit = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_LIMIT));
87         }
88         if (refProp.getStringValue(POLICY_RETRY_INTERVAL) != null) {
89             retryInterval = Integer.valueOf(refProp.getStringValue(POLICY_RETRY_INTERVAL));
90         }
91         for (int i = 0; i < retryLimit; i++) {
92             Exchange paramExchange = ExchangeBuilder.anExchange(camelContext)
93                     .withProperty("policyModelName", policyType).withProperty("policyModelVersion", policyVersion)
94                     .build();
95
96             Exchange exchangeResponse = camelContext.createProducerTemplate().send("direct:get-policy-model",
97                     paramExchange);
98
99             if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {
100                 return (String) exchangeResponse.getIn().getBody();
101             } else {
102                 logger.info("Policy " + retryInterval + "ms before retrying ...");
103                 // wait for a while and try to connect to DCAE again
104                 Thread.sleep(retryInterval);
105             }
106         }
107         return "";
108     }
109
110 }