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