diff options
author | 2022-03-23 11:02:03 +0000 | |
---|---|---|
committer | 2022-03-25 16:13:01 +0000 | |
commit | c9275522f95f6cd94f28286f5e86f86dd451a19b (patch) | |
tree | 58c970ae97fc5d9b9baa935696aeb7ee165644ca /cps-ri/src/main/java/org | |
parent | 81f91b6558e9f385c646005c653937ffe819ad0d (diff) |
Add Start and Stop sessions on JAVA API
Issue-ID: CPS-899
Signed-off-by: emaclee <lee.anjella.macabuhay@est.tech>
Change-Id: Idbeb922790824b1ca601d6d4798df45efa57d685
Diffstat (limited to 'cps-ri/src/main/java/org')
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java | 13 | ||||
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/utils/SessionManager.java | 86 |
2 files changed, 99 insertions, 0 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index 78862d723..bb3c2d07d 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -56,6 +56,7 @@ import org.onap.cps.spi.model.DataNodeBuilder; import org.onap.cps.spi.repository.AnchorRepository; import org.onap.cps.spi.repository.DataspaceRepository; import org.onap.cps.spi.repository.FragmentRepository; +import org.onap.cps.spi.utils.SessionManager; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; @@ -73,6 +74,8 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService private final JsonObjectMapper jsonObjectMapper; + private final SessionManager sessionManager; + private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@[\\s\\S]+?]){0,1})"; private static final Pattern REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE = Pattern.compile("\\[(\\@([^\\/]{0,9999}))\\]$"); @@ -199,6 +202,16 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService .collect(Collectors.toUnmodifiableList()); } + @Override + public String startSession() { + return sessionManager.startSession(); + } + + @Override + public void closeSession(final String sessionId) { + sessionManager.closeSession(sessionId); + } + private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities, final CpsPathQuery cpsPathQuery) { final Set<String> ancestorXpath = new HashSet<>(); diff --git a/cps-ri/src/main/java/org/onap/cps/spi/utils/SessionManager.java b/cps-ri/src/main/java/org/onap/cps/spi/utils/SessionManager.java new file mode 100644 index 000000000..eb535ecc3 --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/utils/SessionManager.java @@ -0,0 +1,86 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021-2022 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.spi.utils; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.SessionException; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; +import org.onap.cps.spi.entities.AnchorEntity; +import org.onap.cps.spi.entities.DataspaceEntity; +import org.onap.cps.spi.entities.SchemaSetEntity; +import org.onap.cps.spi.entities.YangResourceEntity; +import org.springframework.stereotype.Component; + +@Component +public class SessionManager { + + private static SessionFactory sessionFactory; + private static Map<String, Session> sessionMap = new HashMap<>(); + + private synchronized void buildSessionFactory() { + if (sessionFactory == null) { + sessionFactory = new Configuration().configure("hibernate.cfg.xml") + .addAnnotatedClass(AnchorEntity.class) + .addAnnotatedClass(DataspaceEntity.class) + .addAnnotatedClass(SchemaSetEntity.class) + .addAnnotatedClass(YangResourceEntity.class) + .buildSessionFactory(); + } + } + + /** + * Starts a session which allows use of locks and batch interaction with the persistence service. + * + * @return Session ID string + */ + public String startSession() { + buildSessionFactory(); + final Session session = sessionFactory.openSession(); + final String sessionId = UUID.randomUUID().toString(); + sessionMap.put(sessionId, session); + session.beginTransaction(); + return sessionId; + } + + /** + * Close session. + * + * @param sessionId session ID + */ + public void closeSession(final String sessionId) { + try { + final Session currentSession = sessionMap.get(sessionId); + currentSession.getTransaction().commit(); + currentSession.close(); + } catch (final NullPointerException e) { + throw new SessionException(String.format("Session with session ID %s does not exist", sessionId)); + } catch (final HibernateException e) { + throw new SessionException(String.format("Unable to close session with session ID %s", sessionId)); + } + sessionMap.remove(sessionId); + } + +}
\ No newline at end of file |