8eb51b6aa3277f09e365f5d635fa99aebe0a4494
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 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.data;
23
24 import java.io.IOException;
25
26 import org.onap.ccsdk.features.sdnr.wt.common.database.ExtRestClient;
27 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
28 import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult;
29 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder;
30 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
31 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest;
32 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.AggregationEntries;
33 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.Entity;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.EntityInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.SortOrder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.entity.input.Filter;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.entity.input.Sortorder;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class DataObjectAcessorPm<T extends DataObject> extends DataObjectAcessor<T> {
44
45     private final Logger LOG = LoggerFactory.getLogger(DataObjectAcessorPm.class);
46
47     private static final String UUID_KEY = "uuid-interface";
48     private static final String NODE_KEY = "node-name";
49     private static final String KEY = "node-name";
50
51
52     enum Intervall {
53         PMDATA15M("historicalperformance15min", "historicalperformance15min"), PMDATA24H("historicalperformance24h",
54                 "historicalperformance24h");
55
56         String index;
57         String type;
58
59         Intervall(String index, String type) {
60             this.index = index;
61             this.type = type;
62         }
63
64         public String getIndex() {
65             return index;
66         }
67
68         public String getType() {
69             return type;
70         }
71     }
72
73     private ExtRestClient dbClient;
74     private Intervall mode;
75
76     public DataObjectAcessorPm(HtDatabaseClient dbClient, Intervall mode, Entity entity, Class<T> clazz)
77             throws ClassNotFoundException {
78         super(dbClient, entity, clazz, false);
79         this.dbClient = dbClient;
80         this.mode = mode;
81     }
82
83     /**
84      * get aggregated list of ltps for filter NODE_KEY
85      * 
86      * @param input
87      * @return
88      * @throws IOException
89      */
90     QueryResult<String> getDataLtpList(EntityInput input) throws IOException {
91         long page = QueryByFilter.getPage(input);
92         long pageSize = QueryByFilter.getPageSize(input);
93         Filter nodeFilter = QueryByFilter.getFilter(input.getFilter(), NODE_KEY);
94         if (nodeFilter != null) {
95             SearchRequest request = new SearchRequest(mode.getIndex(), mode.getType());
96             request.setQuery(
97                     QueryBuilders.matchQuery(NODE_KEY, nodeFilter.getFiltervalue()).aggregations(UUID_KEY).size(0));
98             try {
99                 SearchResponse response = this.dbClient.search(request);
100                 AggregationEntries aggs = response.getAggregations(UUID_KEY);
101                 String[] uuids = aggs.getKeysAsPagedStringList(pageSize, pageSize * (page - 1));
102                 long totalSize = aggs.size();
103                 return new QueryResult<String>(page, pageSize, new SearchResult<String>(uuids, totalSize));
104             } catch (IOException e) {
105                 throw new IOException("problem reading ltps for req=" + request, e);
106             }
107         } else {
108             String msg = "no nodename in filter found ";
109             LOG.debug(msg);
110             throw new IllegalArgumentException(msg);
111         }
112     }
113
114     //  QueryResult<String> getDataDeviceList(EntityInput input) throws IOException {
115     //
116     //          long page = QueryByFilter.getPage(input);
117     //          long pageSize = QueryByFilter.getPageSize(input);
118     //
119     //          SearchRequest request = new SearchRequest(mode.getIndex(), mode.getType());
120     //          request.setQuery(QueryBuilders.matchAllQuery().aggregations(KEY).size(0));
121     ////                try {
122     //                  SearchResponse response = this.dbClient.search(request);
123     //                  AggregationEntries aggs = response.getAggregations(KEY);
124     //                  String[] uuids = aggs.getKeysAsPagedStringList(pageSize, pageSize * (page - 1));
125     //                  long totalSize = aggs.size();
126     //                  return new QueryResult<String>(page, pageSize, new SearchResult<String>(uuids, totalSize));
127     ////                } catch (IOException e) {
128     ////                        throw new IOException("problem reading nodes for req="+request, e);
129     ////                }
130     //  }
131     /**
132      * get aggregated devices list
133      * 
134      * @param input filter should be empty/no filter handled, only sortorder for KEY ('node-name')
135      * @return
136      * @throws IOException
137      */
138     QueryResult<String> getDataDeviceList(EntityInput input) throws IOException {
139
140         long page = QueryByFilter.getPage(input);
141         long pageSize = QueryByFilter.getPageSize(input);
142
143         Sortorder soNode = QueryByFilter.getSortOrder(input.getSortorder(), KEY);
144         SearchRequest request = new SearchRequest(mode.getIndex(), mode.getType());
145         QueryBuilder query = null;
146         if (soNode != null) {
147             query = QueryBuilders.matchAllQuery()
148                     .aggregations(KEY,
149                             soNode.getSortorder() == SortOrder.Ascending
150                                     ? org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.ASCENDING
151                                     : org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.DESCENDING)
152                     .size(0);
153         } else {
154             query = QueryBuilders.matchAllQuery().aggregations(KEY).size(0);
155         }
156         request.setQuery(query);
157         try {
158             SearchResponse response = this.dbClient.search(request);
159             AggregationEntries aggs = response.getAggregations(KEY);
160             String[] uuids = aggs.getKeysAsPagedStringList(pageSize, pageSize * (page - 1));
161             long totalSize = aggs.size();
162             return new QueryResult<String>(page, pageSize, new SearchResult<String>(uuids, totalSize));
163         } catch (IOException e) {
164             throw new IOException("problem reading nodes for req=" + request, e);
165         }
166
167     }
168
169 }