8eb8fbcbcaa4aa2e23fc17a91acaebf6c1c52233
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2022 Nordix Foundation. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.tasks;
22
23 import io.micrometer.core.instrument.MeterRegistry;
24 import java.lang.invoke.MethodHandles;
25 import java.util.concurrent.atomic.AtomicLong;
26 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
27 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
28 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.scheduling.annotation.EnableScheduling;
33 import org.springframework.scheduling.annotation.Scheduled;
34 import org.springframework.stereotype.Component;
35
36 /**
37  * The aim is to collect statistical values from the A1 Policy Management Service.
38  * The counters are being updated every minute.
39  */
40 @EnableScheduling
41 @Component
42 public class RefreshCounterTask {
43
44     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
45
46     private final Rics rics;
47     private final PolicyTypes policyTypes;
48     private final Policies policies;
49
50     private final MeterRegistry meterRegistry;
51
52     private final AtomicLong ricCount;
53     private final AtomicLong policyTypeCount;
54     private final AtomicLong policyCount;
55
56     @Autowired
57     public RefreshCounterTask(Rics rics, PolicyTypes policyTypes, Policies policies, MeterRegistry meterRegistry) {
58         this.rics = rics;
59         this.policyTypes = policyTypes;
60         this.policies = policies;
61         this.meterRegistry = meterRegistry;
62
63         ricCount = meterRegistry.gauge("total_ric_count", new AtomicLong(0));
64         policyTypeCount = meterRegistry.gauge("total_policy_type_count", new AtomicLong(0));
65         policyCount = meterRegistry.gauge("total_policy_count", new AtomicLong(0));
66     }
67
68     /**
69      * Every minute, updates counters for statistical purposes.
70      */
71     @Scheduled(fixedRate = 1000 * 60)
72     public void checkAllCounters() {
73         logger.trace("Checking counters starting...");
74         ricCount.set(rics.size());
75         policyCount.set(policies.size());
76         policyTypeCount.set(policyTypes.size());
77     }
78 }