2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.Arrays;
27 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.data.rpctypehelper.QueryResult;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ConnectionLogStatus;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.status.output.Data;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.read.status.output.DataBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.status.entity.FaultsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.status.entity.NetworkElementConnectionsBuilder;
36 import org.opendaylight.yangtools.yang.common.Uint32;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class SqlDBStatusReader {
42 private final Logger LOG = LoggerFactory.getLogger(SqlDBStatusReader.class);
44 private final SqlDBClient dbClient;
45 private final String controllerId;
47 public SqlDBStatusReader(SqlDBClient dbClient, String controllerId) {
48 this.dbClient = dbClient;
49 this.controllerId = controllerId;
52 public QueryResult<Data> getDataStatus() {
53 String selectQuery = createCountQuery("severity", Entity.Faultcurrent, this.controllerId);
54 long criticalCount = 0;
57 long warningCount = 0;
60 data = this.dbClient.read(selectQuery);
63 severity = data.getString(1);
64 if (severity != null) {
65 if (severity.equals(SeverityType.Critical.getName())) {
66 criticalCount = data.getLong(2);
67 } else if (severity.equals(SeverityType.Major.getName())) {
68 majorCount = data.getLong(2);
69 } else if (severity.equals(SeverityType.Minor.getName())) {
70 minorCount = data.getLong(2);
71 } else if (severity.equals(SeverityType.Warning.getName())) {
72 warningCount = data.getLong(2);
77 try { data.close(); } catch (SQLException ignore) { }
78 } catch (SQLException e) {
79 LOG.warn("problem reading status:", e);
81 DataBuilder builder = new DataBuilder().setFaults(
82 new FaultsBuilder().setCriticals(Uint32.valueOf(criticalCount)).setMajors(Uint32.valueOf(majorCount))
83 .setMinors(Uint32.valueOf(minorCount)).setWarnings(Uint32.valueOf(warningCount)).build());
84 selectQuery = createCountQuery("status", Entity.NetworkelementConnection, this.controllerId);
85 NetworkElementConnectionsBuilder neBuilder = new NetworkElementConnectionsBuilder();
87 long connectedCount = 0, connectingCount = 0, disconnectedCount = 0, mountedCount = 0, unableToConnectCount = 0,
88 undefinedCount = 0, unmountedCount = 0;
91 data = this.dbClient.read(selectQuery);
93 state = data.getString(1);
94 cnt = data.getLong(2);
96 if (state.equals(ConnectionLogStatus.Connected.getName())) {
98 } else if (state.equals(ConnectionLogStatus.Connecting.getName())) {
99 connectingCount = cnt;
100 } else if (state.equals(ConnectionLogStatus.Disconnected.getName())) {
101 disconnectedCount = cnt;
102 } else if (state.equals(ConnectionLogStatus.Mounted.getName())) {
104 } else if (state.equals(ConnectionLogStatus.UnableToConnect.getName())) {
105 unableToConnectCount = cnt;
106 } else if (state.equals(ConnectionLogStatus.Undefined.getName())) {
107 undefinedCount = cnt;
108 } else if (state.equals(ConnectionLogStatus.Unmounted.getName())) {
109 unmountedCount = cnt;
113 } catch (SQLException e) {
114 LOG.warn("problem reading status:", e);
116 neBuilder.setConnected(Uint32.valueOf(connectedCount)).setConnecting(Uint32.valueOf(connectingCount))
117 .setDisconnected(Uint32.valueOf(disconnectedCount)).setMounted(Uint32.valueOf(mountedCount))
118 .setTotal(Uint32.valueOf(connectedCount + connectingCount + disconnectedCount + mountedCount
119 + unableToConnectCount + undefinedCount + unmountedCount))
120 .setUnableToConnect(Uint32.valueOf(unableToConnectCount)).setUndefined(Uint32.valueOf(undefinedCount))
121 .setUnmounted(Uint32.valueOf(unmountedCount));
122 builder.setNetworkElementConnections(neBuilder.build());
123 return new QueryResult<Data>(Arrays.asList(builder.build()), 1, 1, 1);
126 private static String createCountQuery(String key, Entity e, String controllerId) {
127 return String.format("SELECT `%s`, COUNT(`%s`) " + "FROM `%s` " + "%s " + "GROUP BY `%s`;", key, key,
129 controllerId != null ? String.format("WHERE `%s`='%s'", SqlDBMapper.ODLID_DBCOL, controllerId) : "",