5970d5416c7d9103ea0d54bbbbc7a8b2224c6604
[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.QueryBuilders;
26 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest;
27 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.AggregationEntries;
28 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.Entity;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.read.status.output.Data;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.read.status.output.DataBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.status.entity.FaultsBuilder;
33
34 public class DataObjectAcessorStatus extends DataObjectAcessor<Data> {
35
36         final String ESDATATYPE_FAULTCURRENT_SEVERITY_KEY = "severity";
37
38         private final ExtRestClient dbClient;
39         private final Entity entity;
40
41         public DataObjectAcessorStatus(HtDatabaseClient dbClient, Entity entity)
42                         throws ClassNotFoundException {
43                 super(dbClient, entity, Data.class, false);
44                 this.dbClient = dbClient;
45                 this.entity = entity;
46         }
47
48         QueryResult<Data> getDataStatus() throws IOException {
49         SearchRequest request = getNewInstanceOfSearchRequest(entity);
50         request.setQuery(
51                         QueryBuilders.matchAllQuery().aggregations(ESDATATYPE_FAULTCURRENT_SEVERITY_KEY).size(0));
52                 SearchResponse response = this.dbClient.search(request);
53                 AggregationEntries aggs = response.getAggregations(ESDATATYPE_FAULTCURRENT_SEVERITY_KEY);
54
55                 Data[] data = { new DataBuilder().setFaults(new FaultsBuilder().
56                                                 setCriticals(aggs.getOrDefault("Critical",0L)).
57                                                 setMajors(aggs.getOrDefault("Major", 0L)).
58                                                 setMinors(aggs.getOrDefault("Minor", 0L)).
59                                                 setWarnings(aggs.getOrDefault("Warning", 0L)).
60                                                 build()).build() };
61                 long toalsize = data.length;
62                 return new QueryResult<Data>(1L, 1L, new SearchResult<Data>(data, toalsize));
63
64         }
65
66
67     private static SearchRequest getNewInstanceOfSearchRequest(Entity entity) {
68         return new SearchRequest(entity.getName(), entity.getName());
69     }
70
71
72 }