ec1a20880a60c619e2db4b6d2abecbc8149db6bb
[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"),
54                 PMDATA24H("historicalperformance24h", "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) throws ClassNotFoundException {
77                 super(dbClient, entity, clazz, false);
78                 this.dbClient = dbClient;
79                 this.mode = mode;
80         }
81
82         /**
83          * get aggregated list of ltps for filter NODE_KEY
84          * @param input
85          * @return
86          * @throws IOException
87          */
88         QueryResult<String> getDataLtpList(EntityInput input) throws IOException {
89                 long page = QueryByFilter.getPage(input);
90                 long pageSize = QueryByFilter.getPageSize(input);
91                 Filter nodeFilter = QueryByFilter.getFilter(input.getFilter(), NODE_KEY);
92                 if (nodeFilter != null) {
93                         SearchRequest request = new SearchRequest(mode.getIndex(), mode.getType());
94                         request.setQuery(QueryBuilders.matchQuery(NODE_KEY, nodeFilter.getFiltervalue()).aggregations(UUID_KEY).size(0));
95                         try {
96                                 SearchResponse response = this.dbClient.search(request);
97                                 AggregationEntries aggs = response.getAggregations(UUID_KEY);
98                                 String[] uuids = aggs.getKeysAsPagedStringList(pageSize, pageSize * (page - 1));
99                                 long totalSize = aggs.size();
100                                 return new QueryResult<String>(page, pageSize, new SearchResult<String>(uuids, totalSize));
101                         } catch (IOException e) {
102                                 throw new IOException("problem reading ltps for req="+request, e);
103                         }
104                 } else {
105                         String msg = "no nodename in filter found ";
106                         LOG.debug(msg);
107                         throw new IllegalArgumentException(msg);
108                 }
109         }
110         
111 //      QueryResult<String> getDataDeviceList(EntityInput input) throws IOException {
112 //
113 //              long page = QueryByFilter.getPage(input);
114 //              long pageSize = QueryByFilter.getPageSize(input);
115 //
116 //              SearchRequest request = new SearchRequest(mode.getIndex(), mode.getType());
117 //              request.setQuery(QueryBuilders.matchAllQuery().aggregations(KEY).size(0));
118 ////            try {
119 //                      SearchResponse response = this.dbClient.search(request);
120 //                      AggregationEntries aggs = response.getAggregations(KEY);
121 //                      String[] uuids = aggs.getKeysAsPagedStringList(pageSize, pageSize * (page - 1));
122 //                      long totalSize = aggs.size();
123 //                      return new QueryResult<String>(page, pageSize, new SearchResult<String>(uuids, totalSize));
124 ////            } catch (IOException e) {
125 ////                    throw new IOException("problem reading nodes for req="+request, e);
126 ////            }
127 //      }
128         /**
129          * get aggregated devices list
130          * @param input filter should be empty/no filter handled, only sortorder for KEY ('node-name')
131          * @return
132          * @throws IOException
133          */
134         QueryResult<String> getDataDeviceList(EntityInput input) throws IOException {
135
136                 long page = QueryByFilter.getPage(input);
137                 long pageSize = QueryByFilter.getPageSize(input);
138
139                 Sortorder soNode = QueryByFilter.getSortOrder(input.getSortorder(), KEY);
140                 SearchRequest request = new SearchRequest(mode.getIndex(), mode.getType());
141                 QueryBuilder query = null;
142                 if (soNode != null) {
143                         query = QueryBuilders.matchAllQuery()
144                                         .aggregations(KEY,
145                                                         soNode.getSortorder() == SortOrder.Ascending
146                                                                         ? org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.ASCENDING
147                                                                         : org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.DESCENDING)
148                                         .size(0);
149                 } else {
150                         query = QueryBuilders.matchAllQuery().aggregations(KEY).size(0);
151                 }
152                 request.setQuery(query);
153                 try {
154                         SearchResponse response = this.dbClient.search(request);
155                         AggregationEntries aggs = response.getAggregations(KEY);
156                         String[] uuids = aggs.getKeysAsPagedStringList(pageSize, pageSize * (page - 1));
157                         long totalSize = aggs.size();
158                         return new QueryResult<String>(page, pageSize, new SearchResult<String>(uuids, totalSize));
159                 } catch (IOException e) {
160                         throw new IOException("problem reading nodes for req=" + request, e);
161                 }
162
163         }
164
165 }