aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/EsDataObjectReaderWriter.java
blob: 4a748069f46ad65573ba1e907139d4c4850dcb6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.dataprovider.database;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.onap.ccsdk.features.sdnr.wt.common.database.DatabaseClient;
import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit;
import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult;
import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.yangtools.YangToolsMapper;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.Entity;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;

/**
 * Class to rw yang-tool generated objects into elasticsearch database. For "ES _id" exchange the esIdAddAtributteName is used.
 * This attribute mast be of type String and contains for read and write operations the object id.
 * The function can be used without id handling.
 * If id handling is required the parameter needs to be specified by class definition in yang and  setting the name by using setAttributeName()
 *
 * @param <T> Yang tools generated class object.
 */
public class EsDataObjectReaderWriter<T extends DataObject> {

 	private final Logger LOG = LoggerFactory.getLogger(EsDataObjectReaderWriter.class);

 	/** Typename for elastic search data schema **/
	private String dataTypeName;

	/** Elasticsearch Database client to be used **/
	private DatabaseClient db;

	/** Mapper with configuration to use opendaylight yang-tools builder pattern for object creation **/
	private YangToolsMapper yangtoolsMapper;

	/** Class of T as attribute to allow JSON to Class object mapping **/
	private Class<T> clazz;

	/** Field is used to write id. If null no id handling **/
	private @Nullable Field field;

	/** Attribute that is used as id field for the database object **/
	private @Nullable String esIdAddAtributteName;

	/** Interface to be used for write operations. Rule for write: T extends S and  **/
	private Class<? extends DataObject> writeInterfaceClazz; // == "S"

	/**
	 * Elasticsearch database read and write for specific class, defined by opendaylight yang-tools.
	 *
	 * @param db Database access client
	 * @param dataTypeName typename in database schema
	 * @param clazz class of type to be handled
	 * @throws ClassNotFoundException
	 */
	public EsDataObjectReaderWriter(DatabaseClient db, Entity dataTypeName, Class<T> clazz) throws ClassNotFoundException {
		this(db, dataTypeName.getName(), clazz);
	}
	public EsDataObjectReaderWriter(DatabaseClient db, String dataTypeName, Class<T> clazz) throws ClassNotFoundException {
		LOG.info("Create {} for datatype {} class {}", this.getClass().getName(), dataTypeName, clazz.getName());

		this.esIdAddAtributteName = null;
		this.field = null;
		this.writeInterfaceClazz = clazz;
		this.db = db;
		this.dataTypeName = dataTypeName;
		this.yangtoolsMapper = new YangToolsMapper();
		//this.yangtoolsMapper.assertBuilderClass(clazz);
		this.clazz = clazz;
//
//		if (! db.isExistsIndex(dataTypeName)) {
//			throw new IllegalArgumentException("Index "+dataTypeName+" not existing.");
//		}
	}

	public String getDataTypeName() {
		return dataTypeName;
	}
	public Class<T> getClazz() {
		return clazz;
	}
	/**
	 * Simlar to {@link #setEsIdAttributeName()}, but adapts the parameter to yangtools attribute naming schema
	 * @param esIdAttributeName is converted to UnderscoreCamelCase
	 * @return this for further operations.
	 */
	public EsDataObjectReaderWriter<T> setEsIdAttributeNameCamelized(String esIdAttributeName) {
		return setEsIdAttributeName(YangToolsMapper.toCamelCaseAttributeName(esIdAttributeName));
	}

	/**
	 * Attribute name of class that is containing the object id
	 * @param esIdAttributeName of the implementation class for the yangtools interface.
	 *        Expected attribute name format is CamelCase with leading underline. @
	 * @return this for further operations.
	 * @throws SecurityException if no access or IllegalArgumentException if wrong type or no attribute with this name.
	 */
	public EsDataObjectReaderWriter<T> setEsIdAttributeName(String esIdAttributeName) {
		LOG.debug("Set attribute '{}'", esIdAttributeName);
		this.esIdAddAtributteName = null; // Reset status
		this.field = null;

		Field attributeField;
		try {
			Builder<T> builder = yangtoolsMapper.getBuilder(clazz);
			T object = builder.build();
			attributeField = object.getClass().getDeclaredField(esIdAttributeName);
			if (attributeField.getType().equals(String.class)) {
				attributeField.setAccessible(true);
				this.esIdAddAtributteName = esIdAttributeName; //Set new status if everything OK
				this.field = attributeField;
			} else {
				String msg = "Wrong field type " + attributeField.getType().getName() + " of " + esIdAttributeName;
				LOG.debug(msg);
				throw new IllegalArgumentException(msg);
			}
		} catch (NoSuchFieldException e) {
			// Convert to run-time exception
			String msg = "NoSuchFieldException for '" + esIdAttributeName + "' in class " + clazz.getName();
			LOG.debug(msg);
			throw new IllegalArgumentException(msg);
		} catch (SecurityException e) {
			LOG.debug("Access problem "+esIdAttributeName,e);
			throw e;
		}
		return this;
	}

	/**
	 * Specify subclass of T for write operations.
	 * @param writeInterfaceClazz
	 */
	public EsDataObjectReaderWriter<T> setWriteInterface( @Nonnull Class<? extends DataObject> writeInterfaceClazz ) {
		LOG.debug("Set write interface to {}", writeInterfaceClazz);
		if (writeInterfaceClazz == null)
			throw new IllegalArgumentException("Null not allowed here.");

		this.writeInterfaceClazz = writeInterfaceClazz;
		return this;
	}

	/**
	 * Write child object to database with specific id
	 * @param object
	 * @param @Nullable esId use the id or if null generate unique id
	 * @return String with id or null
	 */
	public @Nullable <S extends DataObject> String write(S object, @Nullable String esId) {
		if (writeInterfaceClazz.isInstance(object)) {
			try {
				String json = yangtoolsMapper.writeValueAsString(object);
				return db.doWriteRaw(dataTypeName, esId, json);
			} catch (JsonProcessingException e) {
				LOG.error("Write problem: ", e);
			}
		} else {
			LOG.error("Type {} does not provide interface {}", object!=null?object.getClass().getName():"null",
					writeInterfaceClazz.getName());
		}
		return null;
	}
	/**
	 * Update partial child object to database with match/term query
	 * @param object
	 * @param esId
	 * @return String with esId or null
	 */
	public @Nullable <S extends DataObject> boolean update(S object, QueryBuilder query) {
		if (writeInterfaceClazz.isInstance(object)) {
			try {
				String json = yangtoolsMapper.writeValueAsString(object);
				return db.doUpdate(this.dataTypeName,json,query);
			} catch (JsonProcessingException e) {
				LOG.error("Update problem: ", e);
			}
		} else {
			LOG.error("Type {} does not provide interface {}", object!=null?object.getClass().getName():"null",
					writeInterfaceClazz.getName());
		}
		return false;
	}
	/**
	 * Write/ update partial child object to database with specific id Write if not
	 * exists, else update
	 * @param object
	 * @param esId
	 * @return String with esId or null
	 */
	public @Nullable <S extends DataObject> String update(S object, String esId) {
		return this.update(object, esId,null);
	}
	public @Nullable <S extends DataObject> String update(S object, String esId,List<String> onylForInsert) {
		if (writeInterfaceClazz.isInstance(object)) {
			try {
				String json = yangtoolsMapper.writeValueAsString(object);
				return db.doUpdateOrCreate(dataTypeName, esId, json,onylForInsert);
			} catch (JsonProcessingException e) {
				LOG.error("Update problem: ", e);
			}
		} else {
			LOG.error("Type {} does not provide interface {}", object!=null?object.getClass().getName():"null",
					writeInterfaceClazz.getName());
		}
		return null;
	}

    /**
     * Read object from database, by using the id field
     * @param object
     * @return
     */
    public @Nullable T read(String esId) {
    	@Nullable T res = (T)null;
		if (esId != null) {
			String json = db.doReadJsonData(dataTypeName, esId);
			try {
				res = yangtoolsMapper.readValue(json.getBytes(), clazz);
			} catch (IOException e) {
				LOG.error("Problem: ", e);
			}
		}
        return res;
    }

    /**
     * Remove object
     * @param esId to identify the object.
     * @return success
     */
    public boolean remove(String esId) {
    	return db.doRemove(this.dataTypeName, esId);
    }

	public int remove(QueryBuilder query) {
		return this.db.doRemove(this.dataTypeName, query);
	}
    /**
     * Get all elements of related type
     * @return all Elements
     */
    public SearchResult<T> doReadAll() {
        return doReadAll(null);
    }
    public SearchResult<T> doReadAll(QueryBuilder query) {
    	return this.doReadAll(query,false);
	}
    /**
     * Read all existing objects of a type
     * @param query for the elements
     * @return the list of all objects
     */

    public SearchResult<T> doReadAll(QueryBuilder query, boolean ignoreException) {

    	SearchResult<T> res = new SearchResult<T>();
        int idx = 0;                //Idx for getAll
        int iterateLength = 100;    //Step width for iterate

        SearchResult<SearchHit> result;
        List<SearchHit> hits;
        do {
            if(query!=null) {
                LOG.debug("read data in {} with query {}",dataTypeName,query.toJSON());
                result=db.doReadByQueryJsonData( dataTypeName, query,ignoreException);
            }
            else {
            	result = db.doReadAllJsonData(dataTypeName,ignoreException);
            }
            hits=result.getHits();
            LOG.debug("Read: {} elements: {}  Failures: {}",dataTypeName,hits.size(), yangtoolsMapper.getMappingFailures());

            T object;
            idx += result.getHits().size();
			for (SearchHit hit : hits) {
				object = getT(hit.getSourceAsString());
				LOG.debug("Mapp Object: {}\nSource: '{}'\nResult: '{}'\n Failures: {}", hit.getId(),
						hit.getSourceAsString(), object, yangtoolsMapper.getMappingFailures());
				if (object != null) {
					setEsId(object, hit.getId());
					res.add(object);
				} else {
					LOG.warn("Mapp result null Object: {}\n Source: '{}'\n : '", hit.getId(), hit.getSourceAsString());
				}
			}

		} while (hits.size() == iterateLength); // Do it until end indicated, because less hits than iterateLength
												// allows.
        res.setTotal(result.getTotal());
        return res;
    }

	/* ---------------------------------------------
     * Private functions
     */

	private void setEsId(T object, String esId) {
		if (field != null) {
			try {
				field.set(object, esId);
			} catch (IllegalArgumentException | IllegalAccessException e) {
				LOG.debug("Field set problem.", e);			}
		}
	}

    private @Nullable T getT(String jsonString) {
    	try {
			return yangtoolsMapper.readValue( jsonString, clazz );
		} catch (IOException e) {
			LOG.info("Mapping problem", e);
			return (T)null;
		}
    }

}