Merge of new rebased code
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / openecomp / appc / dg / common / impl / AbstractResolverDataReader.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              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.openecomp.appc.dg.common.impl;
23
24 import org.openecomp.appc.dao.util.DBUtils;
25 import org.openecomp.appc.rankingframework.AbstractRankedAttributesResolverFactory;
26 import org.openecomp.appc.rankingframework.ConfigurationEntry;
27 import org.openecomp.appc.rankingframework.ConfigurationSet;
28 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
29
30 import java.sql.Connection;
31 import java.sql.PreparedStatement;
32 import java.sql.ResultSet;
33 import java.sql.SQLException;
34 import java.util.Collection;
35 import java.util.Iterator;
36
37 abstract class AbstractResolverDataReader {
38
39     private  class ConfigurationSetAdaptor implements ConfigurationSet<FlowKey> {
40
41         private final ResultSet resultSet;
42
43         private class ResultSetIterator implements Iterator<ConfigurationEntry<FlowKey>>, ConfigurationEntry<FlowKey> {
44             @Override
45             public boolean hasNext() {
46                 try {
47                     return resultSet.next();
48                 } catch (SQLException e) {
49                     throw new RuntimeException(e);
50                 }
51             }
52
53             @Override
54             public ConfigurationEntry<FlowKey> next() {
55                 return this;
56             }
57
58             @Override
59             public void remove() {
60                 throw new UnsupportedOperationException();
61             }
62
63             @Override
64             public Object getAttributeValue(String name) {
65                 try {
66                     return resultSet.getObject(name);
67                 } catch (SQLException e) {
68                     throw new RuntimeException(e);
69                 }
70             }
71
72             @Override
73             public FlowKey getResult() {
74                 try {
75                     return new FlowKey(resultSet.getString("dg_name"), resultSet.getString("dg_version"), resultSet.getString("dg_module"));
76                 } catch (SQLException e) {
77                     throw new RuntimeException(e);
78                 }
79             }
80         }
81
82         ConfigurationSetAdaptor(ResultSet resultSet) {
83             this.resultSet = resultSet;
84         }
85
86         @Override
87         public Iterable<ConfigurationEntry<FlowKey>> getEntries() {
88             return new Iterable<ConfigurationEntry<FlowKey>>() {
89
90                 @Override
91                 public Iterator<ConfigurationEntry<FlowKey>> iterator() {
92                     return new ResultSetIterator();
93                 }
94             };
95         }
96
97         @Override
98         public Collection<String> getRankedAttributeNames() {
99             return getAttributeNames();
100         }
101     }
102
103     protected abstract Collection<String> getAttributeNames();
104     protected abstract String getQueryStmt();
105
106
107
108     RankedAttributesResolver<FlowKey> read() {
109         try {
110             try (Connection conn = DBUtils.getConnection("sdnctl")) {
111                 try (PreparedStatement stmt = conn.prepareStatement(getQueryStmt())) {
112                     try (ResultSet res = stmt.executeQuery()) {
113                         if (res.next()) {
114                             res.beforeFirst();
115                             ConfigurationSet<FlowKey> resolverConfig = new ConfigurationSetAdaptor(res);
116                             return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
117                         } else {
118                             throw new IllegalStateException();
119                         }
120                     }
121                 }
122             }
123         } catch (SQLException e) {
124             throw new RuntimeException(e);
125         }
126     }
127 }