d772dc2966eb02a2144389dd67bcbcfe72634cc3
[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.frankfurt.FrankfurtReleaseInformationR2;
37 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.guilin.GuilinReleaseInformation;
38 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.guilin.GuilinReleaseInformationR2;
39
40 public abstract class ReleaseInformation {
41
42     // variables
43     private final Release release;
44     private final Map<ComponentName, DatabaseInfo> dbMap;
45     // end of variables
46
47     // constructors
48     public ReleaseInformation(Release r, Map<ComponentName, DatabaseInfo> dbMap) {
49         this.release = r;
50         this.dbMap = dbMap;
51     }
52     // end of constructors
53
54     /**
55      * get database alias for component
56      * 
57      * @param name
58      * @return alias or null if not exists
59      */
60     public String getAlias(ComponentName name) {
61         return this.getAlias(name, "");
62     }
63
64     public String getAlias(ComponentName name, String prefix) {
65         return dbMap.get(name) == null ? null : prefix + dbMap.get(name).alias;
66     }
67
68     /**
69      * get index name for component
70      * 
71      * @param comp
72      * @return null if component does not exists in this release, otherwise index name
73      */
74     public String getIndex(ComponentName comp) {
75         return this.getIndex(comp, "");
76     }
77
78     /**
79      * get index name for component with prefix
80      * 
81      * @param comp
82      * @param prefix
83      * @return null if component does not exists in this release, otherwise index name
84      */
85     public String getIndex(ComponentName comp, String prefix) {
86         return dbMap.get(comp) == null ? null : (prefix + dbMap.get(comp).getIndex(this.release.getDBSuffix()));
87     }
88
89     /**
90      * get database datatype (doctype) for component
91      * 
92      * @param name
93      * @return datatype or null if not exists
94      */
95     public String getDataType(ComponentName name) {
96         return dbMap.get(name) == null ? null : dbMap.get(name).doctype;
97     }
98
99     public String getDatabaseMapping(ComponentName name) {
100         return dbMap.get(name) == null ? null : dbMap.get(name).getMapping();
101     }
102
103     /**
104      * get database doctype definition for component
105      * 
106      * @param name
107      * @return mappings or null if not exists
108      */
109     public String getDatabaseMapping(ComponentName name, boolean useStrict) {
110         return dbMap.get(name) == null ? null : dbMap.get(name).getMapping(useStrict);
111     }
112
113     /**
114      * get database settings definition for component
115      * 
116      * @param name
117      * @return settings or null if not exists
118      */
119     public String getDatabaseSettings(ComponentName name, int shards, int replicas) {
120         return dbMap.get(name) == null ? null : dbMap.get(name).getSettings(shards, replicas);
121     }
122
123     /**
124      * get converter for component data
125      * 
126      * @param dst destination release
127      * @param comp component to convert
128      * @return
129      */
130     public SearchHitConverter getConverter(Release dst, ComponentName comp) {
131         if (dst == this.release && this.getComponents().contains(comp)) {
132             return new KeepDataSearchHitConverter(comp);
133         }
134         return null;
135     }
136
137     public static ReleaseInformation getInstance(Release r) {
138         switch (r) {
139             case EL_ALTO:
140                 return new ElAltoReleaseInformation();
141             case FRANKFURT_R1:
142                 return new FrankfurtReleaseInformation();
143             case FRANKFURT_R2:
144                 return new FrankfurtReleaseInformationR2();
145             case GUILIN_R1:
146                 return new GuilinReleaseInformation();
147             case GUILIN_R2:
148                 return new GuilinReleaseInformationR2();
149             default:
150                 return null;
151         }
152     }
153
154     /**
155      * @return
156      */
157     public Set<ComponentName> getComponents() {
158         return dbMap.keySet();
159     }
160
161     /**
162      * @param component
163      * @return
164      */
165     public boolean hasOwnDbIndex(ComponentName component) {
166         return this.getDatabaseMapping(component) != null;
167     }
168
169     /**
170      * @param indices
171      * @return true if components of this release are covered by the given indices
172      */
173     protected boolean containsIndices(IndicesEntryList indices) {
174
175         if (this.dbMap.size() <= 0) {
176             return false;
177         }
178         for (DatabaseInfo entry : this.dbMap.values()) {
179             String dbIndexName = entry.getIndex(this.release.getDBSuffix());
180             if (indices.findByIndex(dbIndexName) == null) {
181                 return false;
182             }
183         }
184         return true;
185
186     }
187
188     /**
189      * @param dbClient
190      * @return if succeeded or not
191      */
192     protected abstract boolean runPreInitCommands(HtDatabaseClient dbClient);
193
194     /**
195      * 
196      * @param dbClient
197      * @return if succeeded or not
198      */
199     protected abstract boolean runPostInitCommands(HtDatabaseClient dbClient);
200
201 }