fcfc3fe7bb64513c1798188bcf73b14939189968
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-web / src / main / java / org / onap / dcae / analytics / tca / web / domain / TcaPolicyWrapper.java
1 /*
2  * ================================================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.tca.web.domain;
21
22 import static org.onap.dcae.analytics.tca.model.util.json.TcaModelJsonConversion.TCA_POLICY_JSON_FUNCTION;
23
24 import java.time.ZonedDateTime;
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import org.onap.dcae.analytics.model.common.ConfigSource;
28 import org.onap.dcae.analytics.tca.core.exception.AnalyticsParsingException;
29 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
30 import org.onap.dcae.analytics.tca.model.policy.TcaPolicyModel;
31 import org.onap.dcae.analytics.tca.web.TcaAppProperties;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * @author Rajiv Singla
37  */
38 public class TcaPolicyWrapper implements TcaPolicyModel {
39
40     private static final Logger logger = LoggerFactory.getLogger(TcaPolicyWrapper.class);
41
42     private final ZonedDateTime creationTime;
43     private ZonedDateTime updateDateTime;
44     private String tcaPolicy;
45     private ConfigSource configSource;
46     private AtomicInteger policyUpdateSequence;
47     private String policyVersion;
48
49     private final TcaAppProperties tcaAppProperties;
50
51     public TcaPolicyWrapper(final TcaAppProperties tcaAppProperties) {
52         this.tcaAppProperties = tcaAppProperties;
53         this.creationTime = ZonedDateTime.now();
54         this.tcaPolicy = tcaAppProperties.getTca().getPolicy();
55         policyUpdateSequence = new AtomicInteger(0);
56         this.updateDateTime = ZonedDateTime.now();
57         this.policyVersion = getPolicyVersion(new AtomicInteger(0));
58     }
59
60     public TcaPolicy getTcaPolicy() {
61         String tcaPolicyString = tcaAppProperties.getTca().getPolicy();
62         boolean isConfigBindingServiceProfileActive = tcaAppProperties.isConfigBindingServiceProfileActive();
63         if (isConfigBindingServiceProfileActive) {
64             this.configSource = ConfigSource.CONFIG_BINDING_SERVICE;
65         } else {
66             this.configSource = ConfigSource.CLASSPATH;
67         }
68
69         if (!tcaPolicyString.equals(tcaPolicy)) {
70             this.tcaPolicy = tcaPolicyString;
71             this.updateDateTime = ZonedDateTime.now();
72             policyUpdateSequence.getAndUpdate(sequence -> sequence + 1);
73             this.policyVersion = getPolicyVersion(policyUpdateSequence);
74             logger.info("Updated Tca Policy Wrapper with policy: {}, from Source: {}, policy Version: {}",
75                     tcaPolicy, configSource.name(), policyVersion);
76         }
77
78         return convertTcaPolicy(tcaPolicyString);
79     }
80
81     public void setTcaPolicy(TcaPolicy tcaPolicy, ConfigSource configSource) {
82         this.tcaPolicy = tcaPolicy.toString();
83         this.configSource = configSource;
84     }
85
86     public TcaPolicy convertTcaPolicy(String tcaPolicyString) {
87         return TCA_POLICY_JSON_FUNCTION.apply(tcaPolicyString).orElseThrow(
88                 () -> new AnalyticsParsingException("Unable to parse Tca Policy String: " + tcaPolicyString,
89                         new IllegalArgumentException()));
90     }
91
92     private static String getPolicyVersion(final AtomicInteger policyUpdateSequence) {
93         return "version-" + policyUpdateSequence.intValue();
94     }
95
96     public ZonedDateTime getCreationTime() {
97         return creationTime;
98     }
99
100     public ZonedDateTime getUpdateDateTime() {
101         return updateDateTime;
102     }
103
104     public ConfigSource getConfigSource() {
105         return configSource;
106     }
107
108     public AtomicInteger getPolicyUpdateSequence() {
109         return policyUpdateSequence;
110     }
111
112     public String getPolicyVersion() {
113         return policyVersion;
114     }
115
116 }