TCAGen2 java upgrade
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-web / src / main / java / org / onap / dcae / analytics / tca / web / TcaAppProperties.java
1 /*
2  * ================================================================================
3  * Copyright (c) 2019-2020 China Mobile. 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;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.stream.Stream;
26
27 import org.onap.dcae.analytics.model.AnalyticsProfile;
28 import org.onap.dcae.analytics.model.TcaModelConstants;
29 import org.onap.dcae.analytics.model.configbindingservice.BaseConfigBindingServiceProperties;
30 import org.onap.dcae.analytics.model.configbindingservice.ConfigBindingServiceConstants;
31 import org.springframework.core.env.Environment;
32
33 import lombok.Data;
34 import lombok.ToString;
35
36 /**
37  * @author Kai Lu
38  */
39
40 public class TcaAppProperties extends BaseConfigBindingServiceProperties {
41
42     private final Environment environment;
43
44     public TcaAppProperties(final Environment environment) {
45         this.environment = environment;
46     }
47     /**
48      * TCA Application properties
49      */
50     @Data
51     public static class Tca {
52         private String policy;
53         private Integer processingBatchSize = TcaModelConstants.DEFAULT_TCA_PROCESSING_BATCH_SIZE;
54         private Boolean enableAbatement = TcaModelConstants.DEFAULT_ABATEMENT_ENABLED;
55         private Boolean enableEcompLogging = TcaModelConstants.DEFAULT_ECOMP_LOGGING_ENABLED;
56         private Aai aai = new Aai();
57     }
58
59     /**
60      * A&AI properties
61      */
62     @Data
63     @ToString(exclude = "password")
64     public static class Aai {
65
66         private Boolean enableEnrichment = TcaModelConstants.DEFAULT_AAI_ENRICHMENT_ENABLED;
67         private String url;
68         private String username;
69         private String password;
70
71         private String proxyUrl = null;
72         private Boolean ignoreSSLValidation = TcaModelConstants.DEFAULT_TCA_AAI_IGNORE_SSL_VALIDATION;
73
74         private String genericVnfPath = TcaModelConstants.DEFAULT_TCA_AAI_GENERIC_VNF_PATH;
75         private String nodeQueryPath = TcaModelConstants.DEFAULT_TCA_AAI_NODE_QUERY_PATH;
76
77     }
78
79     public Tca getTca() {
80         Tca tca = new Tca();
81         tca.setPolicy(getPolicy());
82         tca.setProcessingBatchSize(environment.getProperty(ConfigBindingServiceConstants.PROCESSINGBATCHSIZE, Integer.class));
83         tca.setEnableAbatement(environment.getProperty(ConfigBindingServiceConstants.ENABLEABATEMENT, Boolean.class));
84         tca.setEnableEcompLogging(environment.getProperty(ConfigBindingServiceConstants.ENABLEECOMPLOGGING, Boolean.class));
85
86         Aai aai = tca.getAai();
87         aai.setEnableEnrichment(environment.getProperty(ConfigBindingServiceConstants.ENABLEENRICHMENT, Boolean.class));
88         aai.setUrl(environment.getProperty(ConfigBindingServiceConstants.AAIURL));
89         aai.setUsername(environment.getProperty(ConfigBindingServiceConstants.AAIUSERNAME));
90         aai.setPassword(environment.getProperty(ConfigBindingServiceConstants.AAIPASSWORD));
91         aai.setGenericVnfPath(environment.getProperty(ConfigBindingServiceConstants.AAIGENERICVNFPATH));
92         aai.setNodeQueryPath(environment.getProperty(ConfigBindingServiceConstants.AAINODEQUERYPATH));
93         tca.setAai(aai);
94
95         return tca;
96     }
97
98     /**
99      * Check policies items exist, and return policy.
100      * @return Policy policy
101      */
102     private String getPolicy() {
103
104         String policies = environment.getProperty(ConfigBindingServiceConstants.WITHPOLICIES);
105         if (policies == null) {
106             return environment.getProperty(ConfigBindingServiceConstants.POLICY);
107         }
108
109         return policies;
110
111     }
112
113     @Override
114     public Map<String, PublisherDetails> getStreamsPublishes() {
115         DmaapInfo dmaapInfo = new DmaapInfo();
116         dmaapInfo.setTopicUrl(environment.getProperty(ConfigBindingServiceConstants.PUBTOPICURL));
117
118         PublisherDetails detail = new PublisherDetails();
119         detail.setType(environment.getProperty(ConfigBindingServiceConstants.PUBTYPE));
120         detail.setDmaapInfo(dmaapInfo);
121         streamsPublishes.put(ConfigBindingServiceConstants.PUBKEY, detail);
122         return streamsPublishes;
123     }
124
125     @Override
126     public Map<String, SubscriberDetails> getStreamsSubscribes() {
127         DmaapInfo dmaapInfo = new DmaapInfo();
128         dmaapInfo.setTopicUrl(environment.getProperty(ConfigBindingServiceConstants.SUBTOPICURL));
129
130         AutoAdjusting autoAdjust = new AutoAdjusting();
131         autoAdjust.setStepUp(environment.getProperty(ConfigBindingServiceConstants.SUBAUTOADJUSTINGSTEPUP, Integer.class));
132         autoAdjust.setStepDown(environment.getProperty(ConfigBindingServiceConstants.SUBAUTOADJUSTINGSTEPDOWN, Integer.class));
133         autoAdjust.setMax(environment.getProperty(ConfigBindingServiceConstants.SUBAUTOADJUSTINGMAX, Integer.class));
134         autoAdjust.setMin(environment.getProperty(ConfigBindingServiceConstants.SUBAUTOADJUSTINGMIN, Integer.class));
135
136         Polling poll = new Polling();
137         poll.setAutoAdjusting(autoAdjust);
138         poll.setFixedRate(environment.getProperty(ConfigBindingServiceConstants.SUBFIXEDRATE, Integer.class));
139
140         SubscriberDetails detail = new SubscriberDetails();
141         detail.setType(environment.getProperty(ConfigBindingServiceConstants.SUBTYPE));
142         detail.setDmaapInfo(dmaapInfo);
143         detail.setPolling(poll);
144
145         detail.setConsumerGroup(environment.getProperty(ConfigBindingServiceConstants.SUBCONSUMERGROUP));
146         detail.setMessageLimit(environment.getProperty(ConfigBindingServiceConstants.SUBMESSAGELIMIT, Integer.class));
147         detail.setTimeout(environment.getProperty(ConfigBindingServiceConstants.SUBTIMEOUT, Integer.class));
148         List<String> consumerIds = new ArrayList<>();
149         consumerIds.add(environment.getProperty(ConfigBindingServiceConstants.SUBCONSUMERIDS0));
150         consumerIds.add(environment.getProperty(ConfigBindingServiceConstants.SUBCONSUMERIDS1));
151         detail.setConsumerIds(consumerIds);
152
153         streamsSubscribes.put(ConfigBindingServiceConstants.SUBKEY, detail);
154         return streamsSubscribes;
155     }
156
157     public boolean isConfigBindingServiceProfileActive() {
158         return Stream.of(environment.getActiveProfiles())
159         .anyMatch(profile ->
160                 profile.equalsIgnoreCase(AnalyticsProfile.CONFIG_BINDING_SERVICE_PROFILE_NAME));
161
162     }
163 }