aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/EsDataObjectReaderWriter2.java
blob: bf1aff372d6acb224dbd9ecff8fd34c69b39b28d (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * ============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 com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.eclipse.jdt.annotation.NonNull;
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.YangToolsMapper2;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 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()
 *
 * Due to using Jackson base interfaces the org.eclipse.jdt.annotation.NonNull needs to be used here to get rid of
 * warnings
 *
 * @param <T> Yang tools generated class object.
 */
public class EsDataObjectReaderWriter2<T extends DataObject> {

    private final Logger LOG = LoggerFactory.getLogger(EsDataObjectReaderWriter2.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 YangToolsMapper2<T> 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"
    /** Flag true to sync this attribute during write always, what is slow and false do not sync */
    private final boolean syncAfterWrite;

    /**
     * Elasticsearch database read and write for specific class, defined by opendaylight yang-tools.
     *
     * @param <X>
     * @param <B>
     * @param db Database access client
     * @param dataTypeName typename in database schema
     * @param clazz class of type to be handled
     * @param builderClazz class to build related object if builder pattern should be used.
     * @param syncAfterWrite
     * @throws ClassNotFoundException
     */
    public <X extends T, @NonNull B extends Builder<X>> EsDataObjectReaderWriter2(DatabaseClient db,
            String dataTypeName, @Nonnull Class<T> clazz, @Nullable Class<B> builderClazz, boolean syncAfterWrite)
            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 YangToolsMapper2<>(clazz, builderClazz);
        this.clazz = clazz;
        this.syncAfterWrite = syncAfterWrite;
    }

    public <X extends T, @NonNull B extends Builder<X>> EsDataObjectReaderWriter2(DatabaseClient db,
            Entity dataTypeName, @Nonnull Class<T> clazz, @Nullable Class<B> builderClazz)
            throws ClassNotFoundException {
        this(db, dataTypeName.getName(), clazz, builderClazz, false);
    }

    public <X extends T, @NonNull B extends Builder<X>> EsDataObjectReaderWriter2(DatabaseClient db,
            Entity dataTypeName, @Nonnull Class<T> clazz, @Nullable Class<B> builderClazz, boolean syncAfterWrite)
            throws ClassNotFoundException {
        this(db, dataTypeName.getName(), clazz, builderClazz, syncAfterWrite);
    }

    public EsDataObjectReaderWriter2(DatabaseClient db, Entity dataTypeName, @Nonnull Class<T> clazz,
            boolean syncAfterWrite) throws ClassNotFoundException {
        this(db, dataTypeName.getName(), clazz, null, syncAfterWrite);
    }

    public EsDataObjectReaderWriter2(DatabaseClient db, Entity dataTypeName, Class<T> clazz)
            throws ClassNotFoundException {
        this(db, dataTypeName.getName(), clazz, null, false);
    }

    /**
     * Get Datatype name
     *
     * @return string with dataTypeName
     */
    public String getDataTypeName() {
        return dataTypeName;
    }

    /**
     * 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 EsDataObjectReaderWriter2<T> setEsIdAttributeNameCamelized(String esIdAttributeName) {
        return setEsIdAttributeName(YangToolsMapper2.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 EsDataObjectReaderWriter2<T> setEsIdAttributeName(String esIdAttributeName) {
        LOG.debug("Set attribute '{}'", esIdAttributeName);
        this.esIdAddAtributteName = null; // Reset status
        this.field = null;

        Field attributeField;
        try {
            Builder<? extends T> builder = yangtoolsMapper.getBuilder(clazz);
            if (builder == null) {
                String msg = "No builder for " + clazz;
                LOG.debug(msg);
                throw new IllegalArgumentException(msg);
            } else {
                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 EsDataObjectReaderWriter2<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;
    }

    public interface IdGetter<S extends DataObject> {
        String getId(S object);
    }

    public <S extends DataObject> void write(List<S> objectList, IdGetter<S> idGetter) {
        for (S object : objectList) {
            write(object, idGetter.getId(object));
        }
    }

    /**
     * Write child object to database with specific id
     *
     * @param object to be written
     * @param 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 (object != null && writeInterfaceClazz.isInstance(object)) {
            try {
                String json = yangtoolsMapper.writeValueAsString(object);
                return db.doWriteRaw(dataTypeName, esId, json, this.syncAfterWrite);
            } 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 <S> of object
     * @param object to write
     * @param query for write of specific attributes
     * @return json string with new Object
     */
    public @Nullable <S extends DataObject> boolean update(S object, QueryBuilder query) {
        if (object != null && 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.updateOrCreate(object, esId, null);
    }

    /**
     * See {@link doUpdateOrCreate(String dataTypeName, String esId, String json, List<String> doNotUpdateField) }
     */
    public @Nullable <S extends DataObject> String updateOrCreate(S object, String esId, List<String> onlyForInsert) {
        if (object != null && writeInterfaceClazz.isInstance(object)) {
            try {
                String json = yangtoolsMapper.writeValueAsString(object);
                return db.doUpdateOrCreate(dataTypeName, esId, json, onlyForInsert);
            } 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 = null;
        if (esId != null) {
            String json = db.doReadJsonData(dataTypeName, esId);
            if (json != null) {
                try {
                    res = yangtoolsMapper.readValue(json.getBytes(), clazz);
                } catch (IOException e) {
                    LOG.error("Problem: ", e);
                }
            } else {
                LOG.debug("Can not read from DB id {} type {}", esId, dataTypeName);
            }
        }
        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>();

        SearchResult<SearchHit> result;
        List<SearchHit> hits;
        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;
        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());
            }
        }
        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 null;
        }
    }

}