021741bd825d6c7c0675dc592eeaed2e141fe1db
[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, String dbName,
57             String controllerId) {
58         super(dbService, e, dbSuffix, clazz, dbName, 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             Map<FilterKey, Filter> inpFilter = input.getFilter();
87             long total = this.count(inpFilter != null ? new ArrayList<>(inpFilter.values()) : null, this.controllerId);
88             return new QueryResult<>(mappedData, query.getPage(), query.getPageSize(), total);
89         } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
90                 | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) {
91             LOG.warn("problem reading ltp list: ", e);
92         }
93         return null;
94     }
95
96     /**
97      * get aggregated devices list
98      *
99      * @param input filter should be empty/no filter handled, only sortorder for KEY ('node-name')
100      * @return
101      * @throws IOException
102      */
103     public QueryResult<String> getDataDeviceList(EntityInput input) throws IOException {
104
105         SelectQuery query = new SelectQuery(this.tableName, NODE_KEY, this.controllerId).groupBy(NODE_KEY);
106         query.setPagination(input.getPagination());
107         Map<FilterKey, Filter> filter = input.getFilter();
108         if (filter != null) {
109             for (Filter f : filter.values()) {
110                 query.addFilter(f.getProperty(), f.getFiltervalue());
111             }
112         }
113
114         try {
115             ResultSet data = this.dbService.read(query.toSql());
116             List<String> mappedData = SqlDBMapper.read(data, String.class, NODE_KEY);
117             Map<FilterKey, Filter> inpFilter = input.getFilter();
118             long total = this.count(inpFilter != null ? new ArrayList<>(inpFilter.values()) : null, this.controllerId);
119             return new QueryResult<>(mappedData, query.getPage(), query.getPageSize(), total);
120         } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
121                 | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) {
122             LOG.warn("problem reading device list: ", e);
123         }
124         return null;
125     }
126
127     public void write(PmdataEntity pmData) {
128         DateAndTime date = pmData.getTimeStamp();
129         final String id = DatabaseIdGenerator.getPmData15mId(pmData.getNodeName(), pmData.getUuidInterface(),
130                 date != null ? date.getValue() : "null");
131         this.updateOrInsert(pmData, id);
132     }
133
134 }