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