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