a2b1c007cbd175d71683a3c8bc96199576cc5960
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dg.common.impl;
26
27 import java.sql.Connection;
28 import java.sql.PreparedStatement;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.util.Collection;
32 import java.util.Iterator;
33 import org.onap.appc.dao.util.DBUtils;
34 import org.onap.appc.rankingframework.AbstractRankedAttributesResolverFactory;
35 import org.onap.appc.rankingframework.ConfigurationEntry;
36 import org.onap.appc.rankingframework.ConfigurationSet;
37 import org.onap.appc.rankingframework.RankedAttributesResolver;
38
39 abstract class AbstractResolverDataReader {
40
41     private class ConfigurationSetAdaptor implements ConfigurationSet<FlowKey> {
42
43         private final ResultSet resultSet;
44
45         private class ResultSetIterator implements Iterator<ConfigurationEntry<FlowKey>>, ConfigurationEntry<FlowKey> {
46
47             @Override
48             public boolean hasNext() {
49                 try {
50                     return resultSet.next();
51                 } catch (SQLException e) {
52                     throw new DataReaderException(e);
53                 }
54             }
55
56             @Override
57             public ConfigurationEntry<FlowKey> next() {
58                 return this;
59             }
60
61             @Override
62             public void remove() {
63                 throw new UnsupportedOperationException();
64             }
65
66             @Override
67             public Object getAttributeValue(String name) {
68                 try {
69                     return resultSet.getObject(name);
70                 } catch (SQLException e) {
71                     throw new DataReaderException(e);
72                 }
73             }
74
75             @Override
76             public FlowKey getResult() {
77                 try {
78                     return new FlowKey(resultSet.getString("dg_name"), resultSet.getString("dg_version"),
79                         resultSet.getString("dg_module"));
80                 } catch (SQLException e) {
81                     throw new DataReaderException(e);
82                 }
83             }
84         }
85
86         ConfigurationSetAdaptor(ResultSet resultSet) {
87             this.resultSet = resultSet;
88         }
89
90         @Override
91         public Iterable<ConfigurationEntry<FlowKey>> getEntries() {
92
93             return ResultSetIterator::new;
94         }
95
96         @Override
97         public Collection<String> getRankedAttributeNames() {
98             return getAttributeNames();
99         }
100     }
101
102     protected abstract Collection<String> getAttributeNames();
103
104     protected abstract String getQueryStmt();
105
106
107     RankedAttributesResolver<FlowKey> read() {
108         try {
109             try (Connection conn = DBUtils.getConnection("sdnctl")) {
110                 return getFlowKeyRankedAttributesResolver(conn);
111             }
112         } catch (SQLException e) {
113             throw new DataReaderException(e);
114         }
115     }
116
117     private RankedAttributesResolver<FlowKey> getFlowKeyRankedAttributesResolver(Connection conn) throws SQLException {
118         try (PreparedStatement stmt = conn.prepareStatement(getQueryStmt())) {
119             try (ResultSet res = stmt.executeQuery()) {
120                 if (res.next()) {
121                     res.beforeFirst();
122                     ConfigurationSet<FlowKey> resolverConfig = new ConfigurationSetAdaptor(res);
123                     return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
124                 } else {
125                     throw new IllegalStateException();
126                 }
127             }
128         }
129     }
130 }