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