1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
24 import javax.annotation.Nullable;
26 import org.elasticsearch.index.query.QueryBuilder;
27 import org.elasticsearch.search.SearchHit;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Generic class to write lists of model classes to the database.
35 public class HtDataBaseReaderAndWriter<T extends IsEsObject> {
37 private static final Logger log = LoggerFactory.getLogger(HtDataBaseReaderAndWriter.class);
40 private final HtDataBase db;
41 private final String dataTypeName;
42 private final HtMapper<T> mapper;
45 * Class specific access to database
46 * @param db ES database descriptor
47 * @param dataTypeName datatype name
48 * @param clazz class of datatype
50 public HtDataBaseReaderAndWriter(HtDataBase db, String dataTypeName, Class<? extends T> clazz) {
53 this.dataTypeName = dataTypeName;
54 this.mapper = new HtMapper<>( clazz );
59 * Remove Object from database
60 * @param object Object with content
61 * @return true if remove is done
63 public boolean doRemove( T object) {
65 return db.doRemove(dataTypeName, object );
70 * Remove all data that match the filter
71 * @param query to specify data to be deleted
72 * @return number of removed objects
74 public int doRemoveByQuery(QueryBuilder query) {
76 int idx = 0; //Idx for getAll
77 int iterateLength = 100; //Step width for iterate
81 hits = db.doReadByQueryJsonData(idx, iterateLength, dataTypeName, query);
82 log.debug("Found: {} elements: {} Failures: {}",dataTypeName,hits.length, mapper.getMappingFailures());
86 for (SearchHit hit : hits) {
88 object = mapper.getObjectFromJson( hit.getSourceRef() );
90 log.debug("Mapp Object: {}\nSource: '{}'\nResult: '{}'\n Failures: {}", hit.getId(), hit.getSourceAsString(), object, mapper.getMappingFailures());
92 object.setEsId( hit.getId() );
95 log.warn("Mapp result null Object: {}\n Source: '{}'\n : '", hit.getId(), hit.getSourceAsString());
98 } while (hits.length == iterateLength); //Do it until end indicated, because less hits than iterateLength allows.
104 * Do the mapping for test purpose
105 * @param object object for test purpose
106 * @return json String
108 public String getJson( T object ) {
109 String json = mapper.objectToJson(object);
114 * Write one object into Database
115 * @param object Object with content
116 * @return This object for chained call pattern.
118 public T doWrite( T object) {
120 String json = mapper.objectToJson(object);
121 return doWrite(object, json);
126 * Write one object into Database
127 * @param object Object with content
129 * @return This object for chained call pattern.
131 public T doWrite( T object, String json) {
133 log.debug("doWrite {} {}",object.getClass().getSimpleName(), object.getEsId());
136 String esId = db.doWriteJsonString(dataTypeName, object, json);
137 object.setEsId(esId);
138 log.debug("doWrite done for {} {}",object.getClass().getSimpleName(), object.getEsId());
139 return esId == null ? null : object;
141 log.warn("Can not map object and write to database. {} {}",object.getClass().getSimpleName(), object);
149 * Write a list of Objects to the database.
150 * @param list Object list with content
151 * @return This object for chained call pattern.
153 public HtDataBaseReaderAndWriter<T> doWrite( Collection<T> list) {
156 String indexName = db.getNetworkIndex();
158 log.debug("Write to ES database {}, {} Class: {} {} elements",indexName,dataTypeName, mapper.getClazz().getSimpleName(), list.size());
160 if (indexName == null) {
161 throw new IllegalArgumentException("Missing Index");
164 if (list != null && !list.isEmpty()) {
166 if ( doWrite(s) == null ) {
167 if ( ++writeError > 5 ) {
168 log.warn("Leave because of to >5 write errors");
180 * Read one object via the object class specific ID
181 * @param object Object refrenced by idString
182 * @return The Object if found or null
184 public @Nullable T doRead( IsEsObject object ) {
185 T res = mapper.getObjectFromJson( db.doReadJsonData( dataTypeName, object) );
187 res.setEsId(object.getEsId());
193 * Read one object via the object class specific ID
194 * @param objectEsId Object refrence
195 * @return The Object if found or null
197 public @Nullable T doRead( String objectEsId ) {
198 T res = mapper.getObjectFromJson( db.doReadJsonData( dataTypeName, objectEsId ) );
200 res.setEsId(objectEsId);
207 * Read all existing objects of a type
208 * @return the list of all objects
210 public List<T> doReadAll() {
212 List<T> res = new ArrayList<>();
213 int idx = 0; //Idx for getAll
214 int iterateLength = 100; //Step width for iterate
220 hits = db.doReadAllJsonData(idx, iterateLength, dataTypeName);
221 log.debug("Read: {} elements: {} Failures: {}",dataTypeName,hits.length, mapper.getMappingFailures());
225 for (SearchHit hit : hits) {
227 object = mapper.getObjectFromJson( hit.getSourceRef() );
229 log.debug("Mapp Object: {}\nSource: '{}'\nResult: '{}'\n Failures: {}", hit.getId(), hit.getSourceAsString(), object, mapper.getMappingFailures());
230 if (object != null) {
231 object.setEsId( hit.getId() );
234 log.warn("Mapp result null Object: {}\n Source: '{}'\n : '", hit.getId(), hit.getSourceAsString());
237 } while (hits.length == iterateLength); //Do it until end indicated, because less hits than iterateLength allows.