ccae07c0ed4f1e879d20874ba5883661617f0240
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.setup;
23
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
28 import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntryList;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DatabaseInfo;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.KeepDataSearchHitConverter;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release;
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter;
34 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.elalto.ElAltoReleaseInformation;
35 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.frankfurt.FrankfurtReleaseInformation;
36 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.guilin.GuilinReleaseInformation;
37
38 public abstract class ReleaseInformation {
39
40         // variables
41         private final Release release;
42         private final Map<ComponentName, DatabaseInfo> dbMap;
43         // end of variables
44
45         // constructors
46         public ReleaseInformation(Release r, Map<ComponentName, DatabaseInfo> dbMap) {
47                 this.release = r;
48                 this.dbMap = dbMap;
49         }
50         // end of constructors
51
52         /**
53          * get database alias for component
54          * @param name
55          * @return alias or null if not exists
56          */
57         public String getAlias(ComponentName name) {
58                 return this.getAlias(name, "");
59         }
60
61         public String getAlias(ComponentName name, String prefix) {
62                 return dbMap.get(name) == null ? null : prefix + dbMap.get(name).alias;
63         }
64
65         /**
66          * get index name for component
67          * @param comp
68          * @return null if component does not exists in this release, otherwise index name
69          */
70         public String getIndex(ComponentName comp) {
71                 return this.getIndex(comp, "");
72         }
73
74         /**
75          * get index name for component with prefix 
76          * @param comp
77          * @param prefix
78          * @return null if component does not exists in this release, otherwise index name
79          */
80         public String getIndex(ComponentName comp, String prefix) {
81                 return dbMap.get(comp) == null ? null : (prefix + dbMap.get(comp).getIndex(this.release.getDBSuffix()));
82         }
83
84         /**
85          * get database datatype (doctype) for component
86          * @param name
87          * @return datatype or null if not exists
88          */
89         public String getDataType(ComponentName name) {
90                 return dbMap.get(name) == null ? null : dbMap.get(name).doctype;
91         }
92
93         public String getDatabaseMapping(ComponentName name) {
94                 return dbMap.get(name) == null ? null : dbMap.get(name).getMapping();
95         }
96
97         /**
98          * get database doctype definition for component
99          * @param name
100          * @return mappings or null if not exists
101          */
102         public String getDatabaseMapping(ComponentName name, boolean useStrict) {
103                 return dbMap.get(name) == null ? null : dbMap.get(name).getMapping(useStrict);
104         }
105
106         /**
107          * get database settings definition for component
108          * @param name
109          * @return settings or null if not exists
110          */
111         public String getDatabaseSettings(ComponentName name, int shards, int replicas) {
112                 return dbMap.get(name) == null ? null : dbMap.get(name).getSettings(shards, replicas);
113         }
114
115         /**
116          * get converter for component data
117          * @param dst destination release
118          * @param comp component to convert
119          * @return
120          */
121         public SearchHitConverter getConverter(Release dst, ComponentName comp) {
122                 if (dst == this.release && this.getComponents().contains(comp)) {
123                         return new KeepDataSearchHitConverter(comp);
124                 }
125                 return null;
126         }
127
128         public static ReleaseInformation getInstance(Release r) {
129                 switch (r) {
130                 case EL_ALTO:
131                         return new ElAltoReleaseInformation();
132                 case FRANKFURT_R1:
133                         return new FrankfurtReleaseInformation();
134                 case GUILIN:
135                         return new GuilinReleaseInformation();
136                 default:
137                         return null;
138                 }
139         }
140
141         /**
142          * @return
143          */
144         public Set<ComponentName> getComponents() {
145                 return dbMap.keySet();
146         }
147
148         /**
149          * @param component
150          * @return
151          */
152         public boolean hasOwnDbIndex(ComponentName component) {
153                 return this.getDatabaseMapping(component) != null;
154         }
155
156         /**
157          * @param indices
158          * @return true if components of this release are covered by the given indices
159          */
160         protected boolean containsIndices(IndicesEntryList indices) {
161
162                 if (this.dbMap.size() <= 0) {
163                         return false;
164                 }
165                 for (DatabaseInfo entry : this.dbMap.values()) {
166                         String dbIndexName = entry.getIndex(this.release.getDBSuffix());
167                         if (indices.findByIndex(dbIndexName) == null) {
168                                 return false;
169                         }
170                 }
171                 return true;
172
173         }
174
175         /**
176          * @param dbClient
177          * @return if succeeded or not
178          */
179         protected abstract boolean runPreInitCommands(HtDatabaseClient dbClient);
180
181         /**
182          * 
183          * @param dbClient
184          * @return if succeeded or not
185          */
186         protected abstract boolean runPostInitCommands(HtDatabaseClient dbClient);
187
188 }