Initial OpenECOMP APPC commit
[appc.git] / app-c / appc / appc-dispatcher / appc-workflow-management / appc-workflow-management-core / src / main / java / org / openecomp / appc / workflow / impl / WorkflowResolverDataReader.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.workflow.impl;
23
24 import java.sql.Connection;
25 import java.sql.PreparedStatement;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Iterator;
31
32 import org.openecomp.appc.dao.util.DBUtils;
33 import org.openecomp.appc.rankingframework.AbstractRankedAttributesResolverFactory;
34 import org.openecomp.appc.rankingframework.ConfigurationEntry;
35 import org.openecomp.appc.rankingframework.ConfigurationSet;
36 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
37
38 class WorkflowResolverDataReader {
39
40     private static final String QUERY_STMT = "SELECT action,api_version,vnf_type,vnf_version,dg_name,dg_version,dg_module FROM VNF_DG_MAPPING";
41
42     private static final Collection<String> ATTRIBUTE_NAMES = Arrays.asList("action","api_version", "vnf_type", "vnf_version");
43
44     private static class ConfigurationSetAdaptor implements ConfigurationSet<WorkflowKey> {
45
46         private final ResultSet resultSet;
47
48         private class ResultSetIterator implements Iterator<ConfigurationEntry<WorkflowKey>>, ConfigurationEntry<WorkflowKey> {
49             @Override
50             public boolean hasNext() {
51                 try {
52                     return resultSet.next();
53                 } catch (SQLException e) {
54                     throw new RuntimeException(e);
55                 }
56             }
57
58             @Override
59             public ConfigurationEntry<WorkflowKey> next() {
60                 return this;
61             }
62
63             @Override
64             public void remove() {
65                 throw new UnsupportedOperationException();
66             }
67
68             @Override
69             public Object getAttributeValue(String name) {
70                 try {
71                     return resultSet.getObject(name);
72                 } catch (SQLException e) {
73                     throw new RuntimeException(e);
74                 }
75             }
76
77             @Override
78             public WorkflowKey getResult() {
79                 try {
80                     return new WorkflowKey(resultSet.getString("dg_name"), resultSet.getString("dg_version"), resultSet.getString("dg_module"));
81                 } catch (SQLException e) {
82                     throw new RuntimeException(e);
83                 }
84             }
85         }
86
87         ConfigurationSetAdaptor(ResultSet resultSet) {
88             this.resultSet = resultSet;
89         }
90
91         @Override
92         public Iterable<ConfigurationEntry<WorkflowKey>> getEntries() {
93             return new Iterable<ConfigurationEntry<WorkflowKey>>() {
94
95                 @Override
96                 public Iterator<ConfigurationEntry<WorkflowKey>> iterator() {
97                     return new ResultSetIterator();
98                 }
99             };
100         }
101
102         @Override
103         public Collection<String> getRankedAttributeNames() {
104             return ATTRIBUTE_NAMES;
105         }
106     }
107
108     RankedAttributesResolver<WorkflowKey> read() {
109         try {
110             try (Connection conn = DBUtils.getConnection("sdnctl")) {
111                 try (PreparedStatement stmt = conn.prepareStatement(QUERY_STMT)) {
112                     try (ResultSet res = stmt.executeQuery()) {
113                         if (res.next()) {
114                             res.beforeFirst();
115                             ConfigurationSet<WorkflowKey> resolverConfig = new ConfigurationSetAdaptor(res);
116                             return AbstractRankedAttributesResolverFactory.getInstance().create(resolverConfig);
117                         } else {
118                             // TODO: Return empty object
119                             throw new IllegalStateException();
120                         }
121                     }
122                 }
123             }
124         } catch (SQLException e) {
125             throw new RuntimeException(e);
126         }
127     }
128 }