First part of onap rename
[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  * 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 RuntimeException(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 RuntimeException(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 RuntimeException(e);
81                 }
82             }
83         }
84
85         ConfigurationSetAdaptor(ResultSet resultSet) {
86             this.resultSet = resultSet;
87         }
88
89         @Override
90         public Iterable<ConfigurationEntry<FlowKey>> getEntries() {
91             return new Iterable<ConfigurationEntry<FlowKey>>() {
92
93                 @Override
94                 public Iterator<ConfigurationEntry<FlowKey>> iterator() {
95                     return new ResultSetIterator();
96                 }
97             };
98         }
99
100         @Override
101         public Collection<String> getRankedAttributeNames() {
102             return getAttributeNames();
103         }
104     }
105
106     protected abstract Collection<String> getAttributeNames();
107     protected abstract String getQueryStmt();
108
109
110
111     RankedAttributesResolver<FlowKey> read() {
112         try {
113             try (Connection conn = DBUtils.getConnection("sdnctl")) {
114                 try (PreparedStatement stmt = conn.prepareStatement(getQueryStmt())) {
115                     try (ResultSet res = stmt.executeQuery()) {
116                         if (res.next()) {
117                             res.beforeFirst();
118                             ConfigurationSet<FlowKey> resolverConfig = new ConfigurationSetAdaptor(res);
119                             return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
120                         } else {
121                             throw new IllegalStateException();
122                         }
123                     }
124                 }
125             }
126         } catch (SQLException e) {
127             throw new RuntimeException(e);
128         }
129     }
130 }