migrate sdnr features to phosphorus
[ccsdk/features.git] / sdnr / wt / data-provider / dblib / src / main / java / org / onap / ccsdk / features / sdnr / wt / dataprovider / database / sqldb / database / SqlDBReaderWriterPm.java
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.io.IOException;
26 import java.lang.reflect.InvocationTargetException;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient;
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.entity.DatabaseIdGenerator;
34 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.rpctypehelper.QueryResult;
35 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.SelectQuery;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EntityInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.PmdataEntity;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Filter;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterKey;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class SqlDBReaderWriterPm<T extends DataObject> extends SqlDBReaderWriter<T> {
47
48     private final Logger LOG = LoggerFactory.getLogger(SqlDBReaderWriterPm.class);
49
50     private static final String UUID_KEY = "uuid-interface";
51     private static final String NODE_KEY = "node-name";
52     private static final String KEY = "node-name";
53
54     private static final FilterKey FILTERKEY = new FilterKey(KEY);
55
56     public SqlDBReaderWriterPm(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz,
57             String controllerId) {
58         super(dbService, e, dbSuffix, clazz, controllerId);
59     }
60
61     /**
62      * get aggregated list of ltps for filter NODE_KEY
63      *
64      * @param input
65      * @return
66      * @throws IOException
67      */
68     public QueryResult<String> getDataLtpList(EntityInput input) throws IOException {
69
70         SelectQuery query = new SelectQuery(this.tableName, UUID_KEY, this.controllerId).groupBy(UUID_KEY);
71         query.setPagination(input.getPagination());
72         Map<FilterKey, Filter> filter = input.nonnullFilter();
73         if (!filter.containsKey(FILTERKEY)) {
74             String msg = "no node-name in filter found ";
75             LOG.debug(msg);
76             throw new IllegalArgumentException(msg);
77         }
78         for (Filter f : filter.values()) {
79             query.addFilter(f.getProperty(), f.getFiltervalue());
80         }
81
82
83         try {
84             ResultSet data = this.dbService.read(query.toSql());
85             List<String> mappedData = SqlDBMapper.read(data, String.class, UUID_KEY);
86             try { data.close(); } catch (SQLException ignore) { }
87             Map<FilterKey, Filter> inpFilter = input.getFilter();
88             long total = this.count(inpFilter != null ? new ArrayList<>(inpFilter.values()) : null, this.controllerId);
89             return new QueryResult<>(mappedData, query.getPage(), query.getPageSize(), total);
90         } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
91                 | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) {
92             LOG.warn("problem reading ltp list: ", e);
93         }
94         return null;
95     }
96
97     /**
98      * get aggregated devices list
99      *
100      * @param input filter should be empty/no filter handled, only sortorder for KEY ('node-name')
101      * @return
102      * @throws IOException
103      */
104     public QueryResult<String> getDataDeviceList(EntityInput input) throws IOException {
105
106         SelectQuery query = new SelectQuery(this.tableName, NODE_KEY, this.controllerId).groupBy(NODE_KEY);
107         query.setPagination(input.getPagination());
108         Map<FilterKey, Filter> filter = input.getFilter();
109         if (filter != null) {
110             for (Filter f : filter.values()) {
111                 query.addFilter(f.getProperty(), f.getFiltervalue());
112             }
113         }
114
115         try {
116             ResultSet data = this.dbService.read(query.toSql());
117             List<String> mappedData = SqlDBMapper.read(data, String.class, NODE_KEY);
118             try { data.close(); } catch (SQLException ignore) { }
119             Map<FilterKey, Filter> inpFilter = input.getFilter();
120             long total = this.count(inpFilter != null ? new ArrayList<>(inpFilter.values()) : null, this.controllerId);
121             return new QueryResult<>(mappedData, query.getPage(), query.getPageSize(), total);
122         } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
123                 | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) {
124             LOG.warn("problem reading device list: ", e);
125         }
126         return null;
127     }
128
129     public void write(PmdataEntity pmData) {
130         DateAndTime date = pmData.getTimeStamp();
131         final String id = DatabaseIdGenerator.getPmData15mId(pmData.getNodeName(), pmData.getUuidInterface(),
132                 date != null ? date.getValue() : "null");
133         this.updateOrInsert(pmData, id);
134     }
135
136 }