Improve metadata support
[clamp.git] / src / main / java / org / onap / clamp / policy / downloader / PolicyEngineController.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.time.Instant;
30
31 import org.json.simple.parser.ParseException;
32 import org.onap.clamp.clds.client.PolicyEngineServices;
33 import org.onap.clamp.loop.template.PolicyModelsRepository;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.context.annotation.Configuration;
36 import org.springframework.context.annotation.Profile;
37 import org.springframework.scheduling.annotation.Scheduled;
38
39 /**
40  * This class implements a periodic job that is done in the background to
41  * synchronize policy models available on the policy engine and the clamp
42  * database table PolicyModel.
43  */
44 @Configuration
45 @Profile("clamp-policy-controller")
46 public class PolicyEngineController {
47
48     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyEngineController.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     private final PolicyEngineServices policyEngineServices;
55
56     private Instant lastInstantExecuted;
57
58     @Autowired
59     public PolicyEngineController(PolicyEngineServices policyEngineService,
60                                   PolicyModelsRepository policyModelsRepository) {
61         this.policyEngineServices = policyEngineService;
62     }
63
64     @Scheduled(fixedRate = 300000)
65     public synchronized void synchronizeAllPolicies() {
66         policyEngineServices.synchronizeAllPolicies();
67         lastInstantExecuted = Instant.now();
68     }
69
70     public Instant getLastInstantExecuted() {
71         return lastInstantExecuted;
72     }
73
74     @Scheduled(fixedRate = 300000)
75     public synchronized void downloadPdpGroups() throws ParseException {
76         policyEngineServices.downloadPdpGroups();
77     }
78 }