a145a9f1b0c6810207f337dc5ade7eac45381d86
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.dataprovider.data;
19
20 import java.io.IOException;
21
22 import org.onap.ccsdk.features.sdnr.wt.common.database.ExtRestClient;
23 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
24 import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult;
25 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder;
26 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
27 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest;
28 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.AggregationEntries;
29 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.Entity;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.EntityInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.SortOrder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.entity.input.Filter;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.entity.input.Sortorder;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class DataObjectAcessorPm<T extends DataObject> extends DataObjectAcessor<T> {
40
41         private final Logger LOG = LoggerFactory.getLogger(DataObjectAcessorPm.class);
42
43         private static final String UUID_KEY = "uuid-interface";
44         private static final String NODE_KEY = "node-name";
45         private static final String KEY = "node-name";
46
47
48         enum Intervall {
49                 PMDATA15M("historicalperformance15min", "historicalperformance15min"),
50                 PMDATA24H("historicalperformance24h", "historicalperformance24h");
51
52                 String index;
53                 String type;
54
55                 Intervall(String index, String type) {
56                     this.index = index;
57                     this.type = type;
58                 }
59
60                 public String getIndex() {
61                         return index;
62                 }
63
64                 public String getType() {
65                         return type;
66                 }
67         }
68
69         private ExtRestClient dbClient;
70         private Intervall mode;
71
72         public DataObjectAcessorPm(HtDatabaseClient dbClient, Intervall mode, Entity entity, Class<T> clazz) throws ClassNotFoundException {
73                 super(dbClient, entity, clazz, false);
74                 this.dbClient = dbClient;
75                 this.mode = mode;
76         }
77
78         /**
79          * get aggregated list of ltps for filter NODE_KEY
80          * @param input
81          * @return
82          * @throws IOException
83          */
84         QueryResult<String> getDataLtpList(EntityInput input) throws IOException {
85                 long page = QueryByFilter.getPage(input);
86                 long pageSize = QueryByFilter.getPageSize(input);
87                 Filter nodeFilter = QueryByFilter.getFilter(input.getFilter(), NODE_KEY);
88                 if (nodeFilter != null) {
89                         SearchRequest request = new SearchRequest(mode.getIndex(), mode.getType());
90                         request.setQuery(QueryBuilders.matchQuery(NODE_KEY, nodeFilter.getFiltervalue()).aggregations(UUID_KEY).size(0));
91                         try {
92                                 SearchResponse response = this.dbClient.search(request);
93                                 AggregationEntries aggs = response.getAggregations(UUID_KEY);
94                                 String[] uuids = aggs.getKeysAsPagedStringList(pageSize, pageSize * (page - 1));
95                                 long totalSize = aggs.size();
96                                 return new QueryResult<String>(page, pageSize, new SearchResult<String>(uuids, totalSize));
97                         } catch (IOException e) {
98                                 throw new IOException("problem reading ltps for req="+request, e);
99                         }
100                 } else {
101                         String msg = "no nodename in filter found ";
102                         LOG.debug(msg);
103                         throw new IllegalArgumentException(msg);
104                 }
105         }
106         
107 //      QueryResult<String> getDataDeviceList(EntityInput input) throws IOException {
108 //
109 //              long page = QueryByFilter.getPage(input);
110 //              long pageSize = QueryByFilter.getPageSize(input);
111 //
112 //              SearchRequest request = new SearchRequest(mode.getIndex(), mode.getType());
113 //              request.setQuery(QueryBuilders.matchAllQuery().aggregations(KEY).size(0));
114 ////            try {
115 //                      SearchResponse response = this.dbClient.search(request);
116 //                      AggregationEntries aggs = response.getAggregations(KEY);
117 //                      String[] uuids = aggs.getKeysAsPagedStringList(pageSize, pageSize * (page - 1));
118 //                      long totalSize = aggs.size();
119 //                      return new QueryResult<String>(page, pageSize, new SearchResult<String>(uuids, totalSize));
120 ////            } catch (IOException e) {
121 ////                    throw new IOException("problem reading nodes for req="+request, e);
122 ////            }
123 //      }
124         /**
125          * get aggregated devices list
126          * @param input filter should be empty/no filter handled, only sortorder for KEY ('node-name')
127          * @return
128          * @throws IOException
129          */
130         QueryResult<String> getDataDeviceList(EntityInput input) throws IOException {
131
132                 long page = QueryByFilter.getPage(input);
133                 long pageSize = QueryByFilter.getPageSize(input);
134
135                 Sortorder soNode = QueryByFilter.getSortOrder(input.getSortorder(), KEY);
136                 SearchRequest request = new SearchRequest(mode.getIndex(), mode.getType());
137                 QueryBuilder query = null;
138                 if (soNode != null) {
139                         query = QueryBuilders.matchAllQuery()
140                                         .aggregations(KEY,
141                                                         soNode.getSortorder() == SortOrder.Ascending
142                                                                         ? org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.ASCENDING
143                                                                         : org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.DESCENDING)
144                                         .size(0);
145                 } else {
146                         query = QueryBuilders.matchAllQuery().aggregations(KEY).size(0);
147                 }
148                 request.setQuery(query);
149                 try {
150                         SearchResponse response = this.dbClient.search(request);
151                         AggregationEntries aggs = response.getAggregations(KEY);
152                         String[] uuids = aggs.getKeysAsPagedStringList(pageSize, pageSize * (page - 1));
153                         long totalSize = aggs.size();
154                         return new QueryResult<String>(page, pageSize, new SearchResult<String>(uuids, totalSize));
155                 } catch (IOException e) {
156                         throw new IOException("problem reading nodes for req=" + request, e);
157                 }
158
159         }
160
161 }