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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.analytics.tca.web.domain;
22 import static org.onap.dcae.analytics.tca.model.util.json.TcaModelJsonConversion.TCA_POLICY_JSON_FUNCTION;
24 import java.time.ZonedDateTime;
25 import java.util.concurrent.atomic.AtomicInteger;
27 import org.onap.dcae.analytics.model.common.ConfigSource;
28 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
29 import org.onap.dcae.analytics.tca.model.policy.TcaPolicyModel;
30 import org.onap.dcae.analytics.tca.web.TcaAppProperties;
31 import org.onap.dcae.analytics.web.exception.AnalyticsParsingException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * @author Rajiv Singla
38 public class TcaPolicyWrapper implements TcaPolicyModel {
40 private static final Logger logger = LoggerFactory.getLogger(TcaPolicyWrapper.class);
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;
49 private final TcaAppProperties tcaAppProperties;
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));
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;
66 this.configSource = ConfigSource.CLASSPATH;
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);
78 return convertTcaPolicy(tcaPolicyString);
81 public void setTcaPolicy(TcaPolicy tcaPolicy, ConfigSource configSource) {
82 this.tcaPolicy = tcaPolicy.toString();
83 this.configSource = configSource;
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()));
92 private static String getPolicyVersion(final AtomicInteger policyUpdateSequence) {
93 return "version-" + policyUpdateSequence.intValue();
96 public ZonedDateTime getCreationTime() {
100 public ZonedDateTime getUpdateDateTime() {
101 return updateDateTime;
104 public ConfigSource getConfigSource() {
108 public AtomicInteger getPolicyUpdateSequence() {
109 return policyUpdateSequence;
112 public String getPolicyVersion() {
113 return policyVersion;