19e2a5c37168bce4e8c4cb99a94b022a516f78fd
[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 org.onap.appc.dao.util.DBUtils;
28 import org.onap.appc.rankingframework.AbstractRankedAttributesResolverFactory;
29 import org.onap.appc.rankingframework.ConfigurationEntry;
30 import org.onap.appc.rankingframework.ConfigurationSet;
31 import org.onap.appc.rankingframework.RankedAttributesResolver;
32
33 import java.sql.Connection;
34 import java.sql.PreparedStatement;
35 import java.sql.ResultSet;
36 import java.sql.SQLException;
37 import java.util.Collection;
38 import java.util.Iterator;
39
40 abstract class AbstractResolverDataReader {
41
42     private  class ConfigurationSetAdaptor implements ConfigurationSet<FlowKey> {
43
44         private final ResultSet resultSet;
45
46         private class ResultSetIterator implements Iterator<ConfigurationEntry<FlowKey>>, ConfigurationEntry<FlowKey> {
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"), resultSet.getString("dg_module"));
79                 } catch (SQLException e) {
80                     throw new DataReaderException(e);
81                 }
82             }
83         }
84
85         ConfigurationSetAdaptor(ResultSet resultSet) {
86             this.resultSet = resultSet;
87         }
88
89         @Override
90         public Iterable<ConfigurationEntry<FlowKey>> getEntries() {
91
92             return ResultSetIterator::new;
93         }
94
95         @Override
96         public Collection<String> getRankedAttributeNames() {
97             return getAttributeNames();
98         }
99     }
100
101     protected abstract Collection<String> getAttributeNames();
102     protected abstract String getQueryStmt();
103
104
105
106     RankedAttributesResolver<FlowKey> read() {
107         try {
108             try (Connection conn = DBUtils.getConnection("sdnctl")) {
109                 return getFlowKeyRankedAttributesResolver(conn);
110             }
111         } catch (SQLException e) {
112             throw new DataReaderException(e);
113         }
114     }
115
116     private RankedAttributesResolver<FlowKey> getFlowKeyRankedAttributesResolver(Connection conn) throws SQLException {
117         try (PreparedStatement stmt = conn.prepareStatement(getQueryStmt())) {
118             try (ResultSet res = stmt.executeQuery()) {
119                 if (res.next()) {
120                     res.beforeFirst();
121                     ConfigurationSet<FlowKey> resolverConfig = new ConfigurationSetAdaptor(res);
122                     return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
123                 } else {
124                     throw new IllegalStateException();
125                 }
126             }
127         }
128     }
129 }