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