Add policy downloader
[clamp.git] / src / main / java / org / onap / clamp / policy / downloader / PolicyDownloader.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.policy.downloader;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.util.LinkedHashMap;
30 import java.util.Map.Entry;
31
32 import org.onap.clamp.clds.client.PolicyEngineServices;
33 import org.onap.clamp.loop.template.PolicyModel;
34 import org.onap.clamp.loop.template.PolicyModelId;
35 import org.onap.clamp.loop.template.PolicyModelsRepository;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.context.annotation.Profile;
39 import org.springframework.scheduling.annotation.Scheduled;
40 import org.yaml.snakeyaml.Yaml;
41
42 /**
43  * This class implements a periodic job that is done in the background to
44  * synchronize policy models available on the policy engine and the clamp
45  * database table PolicyModel.
46  */
47 @Configuration
48 @Profile("clamp-policy-controller")
49 public class PolicyDownloader {
50
51     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyDownloader.class);
52     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
53     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
54     public static final String POLICY_RETRY_INTERVAL = "policy.retry.interval";
55     public static final String POLICY_RETRY_LIMIT = "policy.retry.limit";
56
57     private final PolicyEngineServices policyEngineServices;
58     private final PolicyModelsRepository policyModelsRepository;
59
60     @Autowired
61     public PolicyDownloader(PolicyEngineServices policyEngineService, PolicyModelsRepository policyModelsRepository) {
62         this.policyEngineServices = policyEngineService;
63         this.policyModelsRepository = policyModelsRepository;
64     }
65
66     private void createPolicyInDbIfNeeded(PolicyModel policyModel) {
67         if (!policyModelsRepository
68                 .existsById(new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) {
69             policyModelsRepository.save(policyModel);
70         }
71     }
72
73     @Scheduled(fixedRate = 120000)
74     public void synchronizeAllPolicies() throws InterruptedException {
75         try {
76             LinkedHashMap<String, Object> loadedYaml = new Yaml().load(policyEngineServices.downloadAllPolicies());
77             if (loadedYaml == null || loadedYaml.isEmpty()) {
78                 logger.warn(
79                         "getAllPolicyType yaml returned by policy engine could not be decoded, as it's null or empty");
80                 return;
81             }
82
83             LinkedHashMap<String, Object> policyTypesList = (LinkedHashMap<String, Object>) loadedYaml
84                     .get("policy_types");
85             for (Entry<String, Object> policyType : policyTypesList.entrySet()) {
86                 createPolicyInDbIfNeeded(policyEngineServices.createPolicyModelFromPolicyEngine(policyType.getKey(),
87                         ((String) ((LinkedHashMap<String, Object>) policyType.getValue()).get("version"))));
88             }
89         } catch (InterruptedException e) {
90             logger.warn("query to policy engine has been interrupted", e);
91             throw e;
92         }
93
94     }
95
96 }