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