summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openecomp/dcae/inventory/daos
diff options
context:
space:
mode:
authorMichael Hwang <mhwang@research.att.com>2017-02-14 15:10:16 +0000
committerMichael Hwang <mhwang@research.att.com>2017-08-23 13:18:27 -0400
commitac853c1e7186b10e34e392918af31e4ac62b45c2 (patch)
tree5b0acebf2704317cd9fe86545313e68771bad04f /src/main/java/org/openecomp/dcae/inventory/daos
parent19488409d66aa5bbbc2856022f1187ce3f563b83 (diff)
Make first commit
Transferring from original dcae project Issue-Id: DCAEGEN2-45 Change-Id: If8500f4a120ec2d27a714c8917f163beb57ee64c Signed-off-by: Michael Hwang <mhwang@research.att.com>
Diffstat (limited to 'src/main/java/org/openecomp/dcae/inventory/daos')
-rw-r--r--src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java62
-rw-r--r--src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java158
-rw-r--r--src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java90
-rw-r--r--src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java50
-rw-r--r--src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java76
-rw-r--r--src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAO.java29
-rw-r--r--src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java181
7 files changed, 646 insertions, 0 deletions
diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java
new file mode 100644
index 0000000..9a674c9
--- /dev/null
+++ b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java
@@ -0,0 +1,62 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.dcae.inventory.daos;
+
+import org.openecomp.dcae.inventory.dbthings.mappers.DCAEServiceComponentObjectMapper;
+import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceComponentObject;
+import org.skife.jdbi.v2.sqlobject.Bind;
+import org.skife.jdbi.v2.sqlobject.BindBean;
+import org.skife.jdbi.v2.sqlobject.SqlQuery;
+import org.skife.jdbi.v2.sqlobject.SqlUpdate;
+import org.skife.jdbi.v2.sqlobject.customizers.Mapper;
+
+import java.util.List;
+
+/**
+ * Created by mhwang on 4/19/16.
+ */
+public interface DCAEServiceComponentsDAO extends InventoryDAO {
+
+ @SqlQuery("select exists (select * from information_schema.tables where table_name = \'dcae_service_components\')")
+ Boolean checkIfTableExists();
+
+ @SqlUpdate("create table dcae_service_components (component_id varchar not null primary key, component_type varchar not null, " +
+ "component_source varchar not null, shareable integer default 0, created timestamp not null, modified timestamp not null)")
+ void createTable();
+
+ @SqlUpdate("insert into dcae_service_components (component_id, component_type, component_source, shareable, created, modified) " +
+ "values (:componentId, :componentType, :componentSource, :shareable, :created, :modified)")
+ void insert(@BindBean DCAEServiceComponentObject componentObject);
+
+ @SqlUpdate("update dcae_service_components set component_type = :componentType, component_source = :componentSource, " +
+ "shareable = :shareable, modified = :modified where component_id = :componentId")
+ void update(@BindBean DCAEServiceComponentObject componentObject);
+
+ @Mapper(DCAEServiceComponentObjectMapper.class)
+ @SqlQuery("select c.* from dcae_services_components_maps m join dcae_service_components c " +
+ "on m.component_id = c.component_id where m.service_id = :it")
+ List<DCAEServiceComponentObject> getByServiceId(@Bind String serviceId);
+
+ @Mapper(DCAEServiceComponentObjectMapper.class)
+ @SqlQuery("select c.* from dcae_service_components c where c.component_id = :it")
+ DCAEServiceComponentObject getByComponentId(@Bind String componentId);
+
+}
diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java
new file mode 100644
index 0000000..f0fca86
--- /dev/null
+++ b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java
@@ -0,0 +1,158 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.dcae.inventory.daos;
+
+import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceComponentObject;
+import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject;
+import org.joda.time.DateTime;
+import org.skife.jdbi.v2.sqlobject.CreateSqlObject;
+import org.skife.jdbi.v2.sqlobject.Transaction;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This is based-off of this blog post.
+ * http://manikandan-k.github.io/2015/05/10/Transactions_in_jdbi.html
+ *
+ * Created by mhwang on 4/21/16.
+ */
+public abstract class DCAEServiceTransactionDAO {
+
+ public static class DCAEServiceTransactionContext {
+
+ private final String serviceId;
+ private final DateTime modified;
+ private DCAEServiceObject serviceObjectToInsert;
+ private DCAEServiceObject serviceObjectToUpdate;
+ private List<DCAEServiceComponentObject> componentObjectsToInsert;
+ private List<DCAEServiceComponentObject> componentObjectsToUpdate;
+ private List<String> mappingsToInsert;
+ private List<String> mappingsToDelete;
+
+ public String getServiceId() {
+ return serviceId;
+ }
+
+ public DateTime getModified() {
+ return modified;
+ }
+
+ public DCAEServiceObject getServiceObjectToInsert() {
+ return serviceObjectToInsert;
+ }
+
+ public void setServiceObjectToInsert(DCAEServiceObject serviceObjectToInsert) {
+ this.serviceObjectToInsert = serviceObjectToInsert;
+ }
+
+ public DCAEServiceObject getServiceObjectToUpdate() {
+ return serviceObjectToUpdate;
+ }
+
+ public void setServiceObjectToUpdate(DCAEServiceObject serviceObjectToUpdate) {
+ this.serviceObjectToUpdate = serviceObjectToUpdate;
+ }
+
+ public List<DCAEServiceComponentObject> getComponentObjectsToInsert() {
+ return componentObjectsToInsert;
+ }
+
+ public List<DCAEServiceComponentObject> addComponentObjectToInsert(DCAEServiceComponentObject componentObject) {
+ this.componentObjectsToInsert.add(componentObject);
+ return this.componentObjectsToInsert;
+ }
+
+ public List<DCAEServiceComponentObject> getComponentObjectsToUpdate() {
+ return componentObjectsToUpdate;
+ }
+
+ public List<DCAEServiceComponentObject> addComponentObjectToUpdate(DCAEServiceComponentObject componentObject) {
+ this.componentObjectsToUpdate.add(componentObject);
+ return this.componentObjectsToUpdate;
+ }
+
+ public List<String> getMappingsToInsert() {
+ return mappingsToInsert;
+ }
+
+ public List<String> addMappingsToInsert(String componentId) {
+ this.mappingsToInsert.add(componentId);
+ return this.mappingsToInsert;
+ }
+
+ public List<String> getMappingsToDelete() {
+ return mappingsToDelete;
+ }
+
+ public List<String> addMappingsToDelete(String componentId) {
+ this.mappingsToDelete.add(componentId);
+ return this.mappingsToDelete;
+ }
+
+ public DCAEServiceTransactionContext(String serviceId, DateTime modified) {
+ this.serviceId = serviceId;
+ this.modified = modified;
+ this.componentObjectsToInsert = new ArrayList<>();
+ this.componentObjectsToUpdate = new ArrayList<>();
+ this.mappingsToInsert = new ArrayList<>();
+ this.mappingsToDelete = new ArrayList<>();
+ }
+
+ }
+
+ @CreateSqlObject
+ abstract DCAEServicesDAO getServicesDAO();
+
+ @CreateSqlObject
+ abstract DCAEServicesComponentsMapsDAO getServicesComponentsMappingDAO();
+
+ @CreateSqlObject
+ abstract DCAEServiceComponentsDAO getComponentsDAO();
+
+ @Transaction
+ public void insert(DCAEServiceTransactionContext context) {
+ if (context.getServiceObjectToInsert() != null) {
+ this.getServicesDAO().insert(context.getServiceObjectToInsert());
+ }
+
+ if (context.getServiceObjectToUpdate() != null) {
+ this.getServicesDAO().update(context.getServiceObjectToUpdate());
+ }
+
+ for (DCAEServiceComponentObject sco : context.getComponentObjectsToInsert()) {
+ this.getComponentsDAO().insert(sco);
+ }
+
+ for (DCAEServiceComponentObject sco : context.getComponentObjectsToUpdate()) {
+ this.getComponentsDAO().update(sco);
+ }
+
+ for (String componentId : context.getMappingsToInsert()) {
+ this.getServicesComponentsMappingDAO().insert(context.getServiceId(), componentId, context.getModified());
+ }
+
+ for (String componentId : context.getMappingsToDelete()) {
+ this.getServicesComponentsMappingDAO().delete(context.serviceId, componentId);
+ }
+ }
+
+}
diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java
new file mode 100644
index 0000000..50b79b2
--- /dev/null
+++ b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java
@@ -0,0 +1,90 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.dcae.inventory.daos;
+
+import org.openecomp.dcae.inventory.dbthings.mappers.DCAEServiceTypeObjectMapper;
+
+import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceTypeObject;
+import io.swagger.model.DCAEServiceTypeRequest;
+import org.skife.jdbi.v2.sqlobject.Bind;
+import org.skife.jdbi.v2.sqlobject.BindBean;
+import org.skife.jdbi.v2.sqlobject.SqlQuery;
+import org.skife.jdbi.v2.sqlobject.SqlUpdate;
+import org.skife.jdbi.v2.sqlobject.customizers.Mapper;
+
+/**
+ * DCAE service type records are no longer treated immutable because versioning is handled by clients
+ * to DCAE inventory i.e. ASDC. There is field "deactivated" that determines if a service type is active.
+ * Inserts and updates automatically activates a service type while deleting deactivates a service type.
+ *
+ * Created by mhwang on 4/19/16.
+ */
+public interface DCAEServiceTypesDAO extends InventoryDAO {
+
+ @SqlQuery("select exists (select * from information_schema.tables where table_name = \'dcae_service_types\')")
+ Boolean checkIfTableExists();
+
+ /**
+ * Note that service_ids and service_locations are nullable fields. This might not be the right decision but because
+ * the resource model allows for nulls, thought it should consistent.
+ */
+ @SqlUpdate("create table dcae_service_types (type_id varchar not null, type_version integer not null, " +
+ "type_name varchar not null, owner varchar not null, blueprint_template text not null, " +
+ "vnf_types varchar[] not null, service_ids varchar[], service_locations varchar[], " +
+ "asdc_service_id varchar, asdc_resource_id varchar, " +
+ "created timestamp not null, deactivated timestamp, constraint pk_type_created primary key (type_id))")
+ void createTable();
+
+ // REVIEW: asdcServiceId and asdcResourceId is implicitly part of the unique key and thus shouldn't be updated.
+ @SqlUpdate("insert into dcae_service_types(type_id, type_version, type_name, owner, blueprint_template, vnf_types, " +
+ "service_ids, service_locations, asdc_service_id, asdc_resource_id, created, deactivated) " +
+ "values (:typeId, :typeVersion, :typeName, :owner, :blueprintTemplate, :vnfTypes, :serviceIds, " +
+ ":serviceLocations, :asdcServiceId, :asdcResourceId, :created, null)")
+ void insert(@BindBean DCAEServiceTypeObject serviceObject);
+
+ @SqlUpdate("update dcae_service_types set " +
+ "owner = :owner, blueprint_template = :blueprintTemplate, vnf_types = :vnfTypes, " +
+ "service_ids = :serviceIds, service_locations = :serviceLocations, created = :created, " +
+ "deactivated = null where type_id = :typeId")
+ void update(@BindBean DCAEServiceTypeObject serviceObject);
+
+ @SqlUpdate("update dcae_service_types set deactivated = (now() at time zone 'utc') where type_id = :typeId")
+ void deactivateExisting(@Bind("typeId") String typeId);
+
+ @Mapper(DCAEServiceTypeObjectMapper.class)
+ @SqlQuery("select * from dcae_service_types where type_id = :it")
+ DCAEServiceTypeObject getByTypeId(@Bind String typeId);
+
+ @Mapper(DCAEServiceTypeObjectMapper.class)
+ @SqlQuery("select * from dcae_service_types where deactivated is null and type_id = :it")
+ DCAEServiceTypeObject getByTypeIdActiveOnly(@Bind String typeId);
+
+ @Mapper(DCAEServiceTypeObjectMapper.class)
+ @SqlQuery("select * from dcae_service_types where type_name = :typeName and type_version = :typeVersion " +
+ "and asdc_service_id is null and asdc_resource_id is null")
+ DCAEServiceTypeObject getByRequestFromNotASDC(@BindBean DCAEServiceTypeRequest serviceTypeObject);
+
+ @Mapper(DCAEServiceTypeObjectMapper.class)
+ @SqlQuery("select * from dcae_service_types where type_name = :typeName and type_version = :typeVersion " +
+ "and asdc_service_id = :asdcServiceId and asdc_resource_id = :asdcResourceId")
+ DCAEServiceTypeObject getByRequestFromASDC(@BindBean DCAEServiceTypeRequest serviceTypeObject);
+
+}
diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java
new file mode 100644
index 0000000..5bfd3cd
--- /dev/null
+++ b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java
@@ -0,0 +1,50 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.dcae.inventory.daos;
+
+import org.joda.time.DateTime;
+import org.skife.jdbi.v2.sqlobject.Bind;
+import org.skife.jdbi.v2.sqlobject.SqlQuery;
+import org.skife.jdbi.v2.sqlobject.SqlUpdate;
+
+/**
+ * This class manages the joining table called "dcae_services_components_maps" which maps the many-to-many relationship
+ * between DCAE services and DCAE service components.
+ *
+ * Created by mhwang on 4/19/16.
+ */
+public interface DCAEServicesComponentsMapsDAO extends InventoryDAO {
+
+ @SqlQuery("select exists (select * from information_schema.tables where table_name = \'dcae_services_components_maps\')")
+ Boolean checkIfTableExists();
+
+ @SqlUpdate("create table dcae_services_components_maps (service_id varchar not null references dcae_services (service_id), " +
+ "component_id varchar not null references dcae_service_components (component_id), " +
+ "created timestamp not null, primary key (service_id, component_id))")
+ void createTable();
+
+ @SqlUpdate("insert into dcae_services_components_maps (service_id, component_id, created) values (:serviceId, :componentId, :created)")
+ void insert(@Bind("serviceId") String serviceId, @Bind("componentId") String componentId, @Bind("created") DateTime created);
+
+ @SqlUpdate("delete from dcae_services_components_maps where service_id = :serviceId and component_id = :componentId")
+ void delete(@Bind("serviceId") String serviceId, @Bind("componentId") String componentId);
+
+}
diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java
new file mode 100644
index 0000000..9200677
--- /dev/null
+++ b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java
@@ -0,0 +1,76 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.dcae.inventory.daos;
+
+import org.openecomp.dcae.inventory.dbthings.mappers.DCAEServiceObjectMapper;
+import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject;
+import org.joda.time.DateTime;
+import org.skife.jdbi.v2.sqlobject.Bind;
+import org.skife.jdbi.v2.sqlobject.BindBean;
+import org.skife.jdbi.v2.sqlobject.SqlQuery;
+import org.skife.jdbi.v2.sqlobject.SqlUpdate;
+import org.skife.jdbi.v2.sqlobject.customizers.Mapper;
+import org.skife.jdbi.v2.util.IntegerMapper;
+
+
+/**
+ * Created by mhwang on 4/19/16.
+ */
+public interface DCAEServicesDAO extends InventoryDAO {
+
+ @SqlQuery("select exists (select * from information_schema.tables where table_name = \'dcae_services\')")
+ Boolean checkIfTableExists();
+
+ @SqlUpdate("create table dcae_services (service_id varchar not null primary key, type_id varchar not null, " +
+ "vnf_id varchar not null, vnf_type varchar not null, vnf_location varchar not null, deployment_ref varchar, " +
+ "created timestamp not null, modified timestamp not null, status varchar not null)")
+ void createTable();
+
+ @SqlUpdate("insert into dcae_services(service_id, type_id, vnf_id, vnf_type, vnf_location, deployment_ref, " +
+ "created, modified, status) values (:serviceId, :typeId, :vnfId, :vnfType, :vnfLocation, :deploymentRef, " +
+ ":created, :modified, :status)")
+ void insert(@BindBean DCAEServiceObject serviceObject);
+
+ @SqlUpdate("update dcae_services set type_id = :typeId, vnf_id = :vnfId, vnf_type = :vnfType, " +
+ "vnf_location = :vnfLocation, deployment_ref = :deploymentRef, modified = :modified, status = :status " +
+ "where service_id = :serviceId")
+ void update(@BindBean DCAEServiceObject serviceObject);
+
+ @Mapper(DCAEServiceObjectMapper.class)
+ @SqlQuery("select * from dcae_services where status = :status and service_id = :serviceId")
+ DCAEServiceObject getByServiceId(@Bind("status") DCAEServiceObject.DCAEServiceStatus status, @Bind("serviceId") String serviceId);
+
+ @Mapper(DCAEServiceObjectMapper.class)
+ @SqlQuery("select * from dcae_services where service_id = :serviceId")
+ DCAEServiceObject getByServiceId(@Bind("serviceId") String serviceId);
+
+ @SqlUpdate("update dcae_services set modified = :modified, status = :status where service_id = :serviceId")
+ void updateStatusByServiceId(@Bind("modified") DateTime modified,
+ @Bind("status") DCAEServiceObject.DCAEServiceStatus status,
+ @Bind("serviceId") String serviceId);
+
+ @Mapper(IntegerMapper.class)
+ @SqlQuery("select count(1) from dcae_services where status = :status and type_id = :typeId")
+ Integer countByType(@Bind("status") DCAEServiceObject.DCAEServiceStatus status, @Bind("typeId") String typeId);
+
+}
+
+
diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAO.java
new file mode 100644
index 0000000..ef2fb8d
--- /dev/null
+++ b/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAO.java
@@ -0,0 +1,29 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.dcae.inventory.daos;
+
+/**
+ * Created by mhwang on 4/19/16.
+ */
+public interface InventoryDAO {
+ Boolean checkIfTableExists();
+ void createTable();
+}
diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java b/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java
new file mode 100644
index 0000000..77611b5
--- /dev/null
+++ b/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java
@@ -0,0 +1,181 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.dcae.inventory.daos;
+
+import org.openecomp.dcae.inventory.InventoryConfiguration;
+import org.openecomp.dcae.inventory.dbthings.StringListArgument;
+import io.dropwizard.jdbi.DBIFactory;
+import io.dropwizard.setup.Environment;
+import org.skife.jdbi.v2.DBI;
+import org.skife.jdbi.v2.Handle;
+import org.skife.jdbi.v2.util.BooleanMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Reluctantly made this into a singleton in order to have access to the DAOs in the request handling code. Didn't
+ * want to change the interface on the handlers because they are generated by Swagger and I wanted to flexibility
+ * to swap in changes easily. This meant sacrificing dependency injection which is preferred.
+ *
+ * Created by mhwang on 4/19/16.
+ */
+public final class InventoryDAOManager {
+
+ private static InventoryDAOManager instance;
+
+ public static InventoryDAOManager getInstance() {
+ if (instance == null) {
+ instance = new InventoryDAOManager();
+ }
+
+ return instance;
+ }
+
+ public static class InventoryDAOManagerSetupException extends RuntimeException {
+
+ public InventoryDAOManagerSetupException(String message) {
+ super(message);
+ }
+
+ }
+
+ private final static Logger LOG = LoggerFactory.getLogger(InventoryDAOManager.class);
+ // WATCH! Table creation order matters where mapping tables refer to other tables for foreign keys.
+ private final static List<Class> DAO_CLASSES = Arrays.asList(DCAEServiceTypesDAO.class, DCAEServicesDAO.class,
+ DCAEServiceComponentsDAO.class, DCAEServicesComponentsMapsDAO.class);
+
+ private DBI jdbi;
+ private Environment environment;
+ private InventoryConfiguration configuration;
+
+ private InventoryDAOManager() {
+ }
+
+ /**
+ * Setup the manager
+ *
+ * Saving the Dropwizard environment and configuration which are used to construct the DBI object in a later
+ * initialize call. This method can only be called once to be safe and to avoid runtime problems that could be
+ * caused if the global instance of this class gets into a weird state (Couldn't use Java's `final` qualifier).
+ *
+ * @param environment
+ * @param inventoryConfiguration
+ */
+ public void setup(Environment environment, InventoryConfiguration inventoryConfiguration) {
+ if (this.environment == null && this.configuration == null) {
+ this.environment = environment;
+ this.configuration = inventoryConfiguration;
+ } else {
+ throw new InventoryDAOManagerSetupException("InventoryDAOManager setup can only be called once.");
+ }
+ }
+
+ /**
+ * Initialize the manager
+ *
+ * Create the underlying validated DBI object that is used to manage database connections
+ */
+ public void initialize() {
+ final DBIFactory factory = new DBIFactory();
+ final DBI jdbi = factory.build(this.environment, this.configuration.getDataSourceFactory(), "dcae-database");
+ jdbi.registerArgumentFactory(new StringListArgument());
+
+ for (Class<? extends InventoryDAO> daoClass : DAO_CLASSES) {
+ final InventoryDAO dao = jdbi.onDemand(daoClass);
+
+ if (dao.checkIfTableExists()) {
+ LOG.info(String.format("Sql table exists: %s", daoClass.getSimpleName()));
+ } else {
+ dao.createTable();
+ LOG.info(String.format("Sql table created: %s", daoClass.getSimpleName()));
+ }
+ }
+
+ // CREATE VIEWS
+ // TODO: This doesn't belong here and is not consistent with the above approach. Make it better.
+ try (Handle jdbiHandle = jdbi.open()) {
+ String viewName = "dcae_service_types_latest";
+ String checkQuery = String.format("select exists (select * from information_schema.tables where table_name = '%s')",
+ viewName);
+
+ if (jdbiHandle.createQuery(checkQuery).map(BooleanMapper.FIRST).first()) {
+ LOG.info(String.format("Sql view exists: %s", viewName));
+ } else {
+ StringBuilder sb = new StringBuilder(String.format("create view %s as ", viewName));
+ sb.append("select s.* from dcae_service_types s ");
+ sb.append("join (select type_name, max(type_version) as max_version from dcae_service_types group by type_name) as f ");
+ sb.append("on s.type_name = f.type_name and s.type_version = f.max_version");
+
+ jdbiHandle.execute(sb.toString());
+ LOG.info(String.format("Sql view created: %s", viewName));
+ }
+ } catch (Exception e) {
+ throw new RuntimeException("", e);
+ }
+
+ // Do this assignment at the end after performing table checks to ensure that connection is good
+ this.jdbi = jdbi;
+ }
+
+ private InventoryDAO getDAO(Class<? extends InventoryDAO> klass) {
+ if (jdbi == null) {
+ throw new RuntimeException("InventoryDAOManager has not been initialized!");
+ }
+
+ // Using this approach to constructing the DAO, the client is not responsible for closing the handle.
+ // http://jdbi.org/sql_object_overview/
+ // > In this case we do not need to (and in fact shouldn’t) ever take action to close the handle the sql object uses.
+ return jdbi.onDemand(klass);
+ }
+
+ public DCAEServicesDAO getDCAEServicesDAO() {
+ return (DCAEServicesDAO) this.getDAO(DCAEServicesDAO.class);
+ }
+
+ public DCAEServiceComponentsDAO getDCAEServiceComponentsDAO() {
+ return (DCAEServiceComponentsDAO) this.getDAO(DCAEServiceComponentsDAO.class);
+ }
+
+ public DCAEServicesComponentsMapsDAO getDCAEServicesComponentsDAO() {
+ return (DCAEServicesComponentsMapsDAO) this.getDAO(DCAEServicesComponentsMapsDAO.class);
+ }
+
+ public DCAEServiceTransactionDAO getDCAEServiceTransactionDAO() {
+ return jdbi.onDemand(DCAEServiceTransactionDAO.class);
+ }
+
+ public DCAEServiceTypesDAO getDCAEServiceTypesDAO() {
+ return (DCAEServiceTypesDAO) this.getDAO(DCAEServiceTypesDAO.class);
+ }
+
+ /**
+ * Must close the handle that is returned here. It is AutoCloseable so just use it as a try-with-resource.
+ *
+ * @return
+ */
+ public Handle getHandle() {
+ return this.jdbi.open();
+ }
+
+}