8362b00307e2c5d27f5cabb28f959570e3118603
[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 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
27 import org.onap.ccsdk.features.sdnr.wt.common.database.data.IndicesEntryList;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.ComponentName;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.DatabaseInfo;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.KeepDataSearchHitConverter;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.Release;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.data.SearchHitConverter;
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.elalto.ElAltoReleaseInformation;
34 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.frankfurt.FrankfurtReleaseInformation;
35 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.frankfurt.FrankfurtReleaseInformationR2;
36 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.guilin.GuilinReleaseInformation;
37 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.honolulu.HonoluluReleaseInformation;
38 import org.onap.ccsdk.features.sdnr.wt.dataprovider.setup.istanbul.IstanbulReleaseInformation;
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 HONOLULU_R1:
148                 return new HonoluluReleaseInformation();
149             case ISTANBUL_R1:
150                 return new IstanbulReleaseInformation();
151             default:
152                 return null;
153         }
154     }
155
156     /**
157      * @return
158      */
159     public Set<ComponentName> getComponents() {
160         return dbMap.keySet();
161     }
162
163     /**
164      * @param component
165      * @return
166      */
167     public boolean hasOwnDbIndex(ComponentName component) {
168         return this.getDatabaseMapping(component) != null;
169     }
170
171     /**
172      * @param indices
173      * @return true if components of this release are covered by the given indices
174      */
175     protected boolean containsIndices(IndicesEntryList indices) {
176
177         if (this.dbMap.size() <= 0) {
178             return false;
179         }
180         for (DatabaseInfo entry : this.dbMap.values()) {
181             String dbIndexName = entry.getIndex(this.release.getDBSuffix());
182             if (indices.findByIndex(dbIndexName) == null) {
183                 return false;
184             }
185         }
186         return true;
187
188     }
189
190     /**
191      * @param dbClient
192      * @return if succeeded or not
193      */
194     protected abstract boolean runPreInitCommands(HtDatabaseClient dbClient);
195
196     /**
197      *
198      * @param dbClient
199      * @return if succeeded or not
200      */
201     protected abstract boolean runPostInitCommands(HtDatabaseClient dbClient);
202
203 }