Standalone TCA with EELF Logger
[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 lombok.Getter;
25 import lombok.ToString;
26
27 import java.time.ZonedDateTime;
28 import java.util.concurrent.atomic.AtomicInteger;
29
30 import org.onap.dcae.analytics.model.common.ConfigSource;
31 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
32 import org.onap.dcae.analytics.tca.model.policy.TcaPolicyModel;
33 import org.onap.dcae.analytics.tca.web.validation.TcaPolicyValidator;
34 import org.onap.dcae.analytics.web.exception.AnalyticsParsingException;
35 import org.onap.dcae.analytics.web.util.ValidationUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * @author Rajiv Singla
41  */
42 @Getter
43 @ToString
44 public class TcaPolicyWrapper implements TcaPolicyModel {
45
46     private static final long serialVersionUID = 1L;
47
48     private static final Logger logger = LoggerFactory.getLogger(TcaPolicyWrapper.class);
49
50     private final ZonedDateTime creationTime;
51     private ZonedDateTime updateDateTime;
52     private TcaPolicy tcaPolicy;
53     private ConfigSource configSource;
54     private AtomicInteger policyUpdateSequence;
55     private String policyVersion;
56
57     public TcaPolicyWrapper(final String tcaPolicyString, final ConfigSource configSource) {
58         createOrUpdatePolicy(getTcaPolicy(tcaPolicyString), configSource);
59         this.creationTime = ZonedDateTime.now();
60     }
61
62     public void setTcaPolicy(final String tcaPolicyString, final ConfigSource configSource) {
63         createOrUpdatePolicy(getTcaPolicy(tcaPolicyString), configSource);
64     }
65
66     public void setTcaPolicy(final TcaPolicy tcaPolicy, final ConfigSource configSource) {
67         createOrUpdatePolicy(tcaPolicy, configSource);
68     }
69
70     private void createOrUpdatePolicy(final TcaPolicy tcaPolicy, final ConfigSource configSource) {
71         ValidationUtils.validate(tcaPolicy, new TcaPolicyValidator());
72         this.tcaPolicy = tcaPolicy;
73         this.configSource = configSource;
74         this.updateDateTime = ZonedDateTime.now();
75         if (policyUpdateSequence == null) {
76             policyUpdateSequence = new AtomicInteger(0);
77         } else {
78             policyUpdateSequence.getAndUpdate(sequence -> sequence + 1);
79         }
80         this.policyVersion = getPolicyVersion(policyUpdateSequence);
81         final String configSourceName = configSource.name();
82         logger.info("Updated Tca Policy Wrapper with policy: {}, from Source: {}, policy Version: {}",
83                 tcaPolicy, configSourceName, policyVersion);
84     }
85
86
87     private TcaPolicy getTcaPolicy(final String tcaPolicyString) {
88         return TCA_POLICY_JSON_FUNCTION.apply(tcaPolicyString).orElseThrow(
89                 () -> new AnalyticsParsingException("Unable to parse Tca Policy String: " + tcaPolicyString,
90                         new IllegalArgumentException()));
91     }
92
93
94     private static String getPolicyVersion(final AtomicInteger policyUpdateSequence) {
95         return "version-" + policyUpdateSequence.intValue();
96     }
97
98 }