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;
25 import lombok.ToString;
27 import java.time.ZonedDateTime;
28 import java.util.concurrent.atomic.AtomicInteger;
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;
40 * @author Rajiv Singla
44 public class TcaPolicyWrapper implements TcaPolicyModel {
46 private static final long serialVersionUID = 1L;
48 private static final Logger logger = LoggerFactory.getLogger(TcaPolicyWrapper.class);
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;
57 public TcaPolicyWrapper(final String tcaPolicyString, final ConfigSource configSource) {
58 createOrUpdatePolicy(getTcaPolicy(tcaPolicyString), configSource);
59 this.creationTime = ZonedDateTime.now();
62 public void setTcaPolicy(final String tcaPolicyString, final ConfigSource configSource) {
63 createOrUpdatePolicy(getTcaPolicy(tcaPolicyString), configSource);
66 public void setTcaPolicy(final TcaPolicy tcaPolicy, final ConfigSource configSource) {
67 createOrUpdatePolicy(tcaPolicy, configSource);
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);
78 policyUpdateSequence.getAndUpdate(sequence -> sequence + 1);
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);
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()));
94 private static String getPolicyVersion(final AtomicInteger policyUpdateSequence) {
95 return "version-" + policyUpdateSequence.intValue();