83017366d06eb72d05a0149555ca64b3807a403f
[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.requests.SearchRequest;
30 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.AggregationEntries;
31 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.Entity;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.EntityInput;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class DataObjectAcessorPm<T extends DataObject> extends DataObjectAcessor<T> {
39
40     private final Logger LOG = LoggerFactory.getLogger(DataObjectAcessorPm.class);
41
42     private static final String UUID_KEY = "uuid-interface";
43     private static final String NODE_KEY = "node-name";
44     private static final String KEY = "node-name";
45
46
47     public enum Intervall {
48         PMDATA15M("historicalperformance15min", "historicalperformance15min"), PMDATA24H("historicalperformance24h",
49                 "historicalperformance24h");
50
51         String index;
52         String type;
53
54         Intervall(String index, String type) {
55             this.index = index;
56             this.type = type;
57         }
58
59         public String getIndex() {
60             return index;
61         }
62
63         public String getType() {
64             return type;
65         }
66     }
67
68     private ExtRestClient dbClient;
69     private Intervall mode;
70
71     public DataObjectAcessorPm(HtDatabaseClient dbClient, Intervall mode, Entity entity, Class<T> clazz)
72             throws ClassNotFoundException {
73         super(dbClient, entity, clazz);
74         LOG.info("DataObjectAcessorPm");
75         this.dbClient = dbClient;
76         this.mode = mode;
77     }
78
79     /**
80      * get aggregated list of ltps for filter NODE_KEY
81      * 
82      * @param input
83      * @return
84      * @throws IOException
85      */
86     public QueryResult<String> getDataLtpList(EntityInput input) throws IOException {
87
88         QueryByFilter queryByFilter = new QueryByFilter(input);
89         SearchRequest request =
90                 queryByFilter.getSearchRequestByFilter(NODE_KEY, UUID_KEY, mode.getIndex(), mode.getType());
91         try {
92             SearchResponse response = this.dbClient.search(request);
93             AggregationEntries aggs = response.getAggregations(UUID_KEY);
94             String[] uuids =
95                     aggs.getKeysAsPagedStringList(queryByFilter.getPageSize(), queryByFilter.getPageStartIndex());
96             long totalSize = aggs.size();
97             return new QueryResult<String>(queryByFilter.getPage(), queryByFilter.getPageSize(),
98                     new SearchResult<String>(uuids, totalSize));
99         } catch (IOException e) {
100             throw new IOException("problem reading ltps for req=" + request, e);
101         }
102     }
103
104     /**
105      * get aggregated devices list
106      * 
107      * @param input filter should be empty/no filter handled, only sortorder for KEY ('node-name')
108      * @return
109      * @throws IOException
110      */
111     public QueryResult<String> getDataDeviceList(EntityInput input) throws IOException {
112
113         QueryByFilter queryByFilter = new QueryByFilter(input);
114         SearchRequest request =
115                 queryByFilter.getSearchRequestBySortOrder(NODE_KEY, UUID_KEY, mode.getIndex(), mode.getType());
116         try {
117             SearchResponse response = this.dbClient.search(request);
118             AggregationEntries aggs = response.getAggregations(KEY);
119             String[] uuids =
120                     aggs.getKeysAsPagedStringList(queryByFilter.getPageSize(), queryByFilter.getPageStartIndex());
121             long totalSize = aggs.size();
122             return new QueryResult<String>(queryByFilter.getPage(), queryByFilter.getPageSize(),
123                     new SearchResult<String>(uuids, totalSize));
124         } catch (IOException e) {
125             throw new IOException("problem reading nodes for req=" + request, e);
126         }
127
128     }
129
130 }