5ed44cb29274b5bf59ffb006628693a4b805bcd8
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 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.database.sqldb.database;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import java.lang.reflect.InvocationTargetException;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.rpctypehelper.QueryResult;
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.SelectQuery;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EntityInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Filter;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterKey;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class SqlDBReaderWriterInventory<T extends DataObject> extends SqlDBReaderWriter<T> {
43
44     private final Logger LOG = LoggerFactory.getLogger(SqlDBReaderWriterInventory.class);
45
46    private static final String KEY = "node-id";
47
48     private static final FilterKey FILTERKEY = new FilterKey(KEY);
49
50     public SqlDBReaderWriterInventory(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz,
51                                       String controllerId) {
52         super(dbService, e, dbSuffix, clazz, controllerId);
53     }
54
55     /**
56      * get aggregated devices list
57      *
58      * @param input filter should be empty/no filter handled, only sortorder for KEY ('node-name')
59      * @return
60      */
61     public QueryResult<String> getDataDeviceList(EntityInput input) {
62
63         SelectQuery query = new SelectQuery(this.tableName, KEY, this.controllerId).groupBy(KEY);
64         query.setPagination(input.getPagination());
65         Map<FilterKey, Filter> filter = input.getFilter();
66         if (filter != null) {
67             for (Filter f : filter.values()) {
68                 query.addFilter(f.getProperty(), f.getFiltervalue());
69             }
70         }
71
72         try {
73             ResultSet data = this.dbService.read(query.toSql());
74             List<String> mappedData = SqlDBMapper.read(data, String.class, KEY);
75             try { data.close(); } catch (SQLException ignore) { }
76             Map<FilterKey, Filter> inpFilter = input.getFilter();
77             long total = this.count(inpFilter != null ? new ArrayList<>(inpFilter.values()) : null, this.controllerId);
78             return new QueryResult<>(mappedData, query.getPage(), query.getPageSize(), total);
79         } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
80                 | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) {
81             LOG.warn("problem reading device list: ", e);
82         }
83         return null;
84     }
85
86
87 }