04849004f831ac7e209e3b55d14a0c46d9aabd55
[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.database.elasticsearch.data.acessor;
23
24 import java.io.IOException;
25 import org.onap.ccsdk.features.sdnr.wt.common.database.ExtRestClient;
26 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
27 import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult;
28 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest;
29 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.AggregationEntries;
30 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper.QueryByFilter;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.data.rpctypehelper.QueryResult;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EntityInput;
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     public enum Intervall {
49         PMDATA15M("historicalperformance15min", "historicalperformance15min"), PMDATA24H("historicalperformance24h",
50                 "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,
73             boolean doFullsizeRequest) throws ClassNotFoundException {
74         super(dbClient, entity, clazz, doFullsizeRequest);
75         LOG.info("DataObjectAcessorPm");
76         this.dbClient = dbClient;
77         this.mode = mode;
78     }
79
80     /**
81      * get aggregated list of ltps for filter NODE_KEY
82      *
83      * @param input
84      * @return
85      * @throws IOException
86      */
87     public QueryResult<String> getDataLtpList(EntityInput input) throws IOException {
88
89         QueryByFilter queryByFilter = new QueryByFilter(input);
90         SearchRequest request = queryByFilter.getSearchRequestByFilter(NODE_KEY, UUID_KEY, mode.getIndex(),
91                 mode.getType(), this.doFullsizeRequest);
92         try {
93             SearchResponse response = this.dbClient.search(request);
94             AggregationEntries aggs = response.getAggregations(UUID_KEY);
95             String[] uuids =
96                     aggs.getKeysAsPagedStringList(queryByFilter.getPageSize(), queryByFilter.getPageStartIndex());
97             long totalSize = aggs.size();
98             return new QueryResult<String>(queryByFilter.getPage(), queryByFilter.getPageSize(),
99                     new SearchResult<String>(uuids, totalSize));
100         } catch (IOException e) {
101             throw new IOException("problem reading ltps for req=" + request, e);
102         }
103     }
104
105     /**
106      * get aggregated devices list
107      *
108      * @param input filter should be empty/no filter handled, only sortorder for KEY ('node-name')
109      * @return
110      * @throws IOException
111      */
112     public QueryResult<String> getDataDeviceList(EntityInput input) throws IOException {
113
114         QueryByFilter queryByFilter = new QueryByFilter(input);
115         SearchRequest request =
116                 queryByFilter.getSearchRequestBySortOrder(NODE_KEY, UUID_KEY, mode.getIndex(), mode.getType(), this.doFullsizeRequest);
117         try {
118             SearchResponse response = this.dbClient.search(request);
119             AggregationEntries aggs = response.getAggregations(KEY);
120             String[] uuids =
121                     aggs.getKeysAsPagedStringList(queryByFilter.getPageSize(), queryByFilter.getPageStartIndex());
122             long totalSize = aggs.size();
123             return new QueryResult<String>(queryByFilter.getPage(), queryByFilter.getPageSize(),
124                     new SearchResult<String>(uuids, totalSize));
125         } catch (IOException e) {
126             throw new IOException("problem reading nodes for req=" + request, e);
127         }
128
129     }
130
131 }