2 * ============LICENSE_START========================================================================
3 * ONAP : tr-069-adapter
4 * =================================================================================================
5 * Copyright (C) 2020 CommScope Inc Intellectual Property.
6 * =================================================================================================
7 * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9 * may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14 * either express or implied. See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ===============LICENSE_END=======================================================================
19 package org.commscope.tr069adapter.acs.requestprocessor.impl;
21 import com.fasterxml.uuid.EthernetAddress;
22 import com.fasterxml.uuid.Generators;
23 import com.fasterxml.uuid.impl.TimeBasedGenerator;
25 import java.util.Optional;
26 import java.util.UUID;
28 import org.commscope.tr069adapter.acs.common.exception.SessionConcurrentAccessException;
29 import org.commscope.tr069adapter.acs.common.exception.SessionManagerException;
30 import org.commscope.tr069adapter.acs.common.utils.ErrorCode;
31 import org.commscope.tr069adapter.acs.requestprocessor.dao.SessionRepository;
32 import org.commscope.tr069adapter.acs.requestprocessor.dto.SessionDTO;
33 import org.commscope.tr069adapter.acs.requestprocessor.dto.SessionState;
34 import org.commscope.tr069adapter.acs.requestprocessor.entity.SessionManagerEntity;
35 import org.commscope.tr069adapter.acs.requestprocessor.util.SessionManagerUtility;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
42 public class SessionManager {
44 private static final Logger logger = LoggerFactory.getLogger(SessionManager.class);
47 SessionRepository sessionRepository;
52 * @throws SessionManagerException
54 public SessionDTO getSession(String deviceId) {
55 SessionManagerEntity session = null;
56 Optional<SessionManagerEntity> optionalSessionData = sessionRepository.findById(deviceId);
57 if (optionalSessionData.isPresent()) {
58 session = optionalSessionData.get();
60 return SessionManagerUtility.convertToDTO(session);
66 * @throws SessionManagerException
68 public SessionDTO getSessionBySessionId(String sessionId) throws SessionManagerException {
70 SessionManagerEntity sessionManagerEntity = sessionRepository.findBySessionId(sessionId);
71 if (sessionManagerEntity == null || SessionState.TERMINATED
72 .equals(SessionState.getByValue(sessionManagerEntity.getState()))) {
73 throw new SessionManagerException(ErrorCode.SESSION_EXPIRED, sessionId);
75 return SessionManagerUtility.convertToDTO(sessionManagerEntity);
81 * @throws SessionManagerException
83 public SessionDTO createSession(SessionDTO session) throws SessionManagerException {
84 if (session == null) {
85 SessionManagerException sme = new SessionManagerException(ErrorCode.SESSION_CREATION_ERROR,
86 "Session object cannot be null");
87 String smeMessage = sme.getMessage().replaceAll("[\n|\r|\t]", "_");
88 logger.error(smeMessage);
91 logger.debug("Creating a new session for the device");
92 return SessionManagerUtility
93 .convertToDTO(sessionRepository.save(SessionManagerUtility.convertToEntity(session)));
99 * @throws SessionManagerException
101 public SessionDTO updateSession(SessionDTO dto) {
102 return SessionManagerUtility
103 .convertToDTO(sessionRepository.save(SessionManagerUtility.convertToEntity(dto)));
108 * @throws SessionConcurrentAccessException
110 public void deleteSession(String deviceId) {
111 sessionRepository.deleteById(deviceId);
117 * @throws SessionManagerException
119 public SessionDTO getLockedSession(String deviceId) {
120 logger.debug("Acquiring the session lock for the device");
121 return SessionManagerUtility.convertToDTO(sessionRepository.findByDeviceId(deviceId));
127 public String generateUniqueSessionID() {
128 EthernetAddress addr = EthernetAddress.fromInterface();
129 TimeBasedGenerator uuidGenerator = Generators.timeBasedGenerator(addr);
130 UUID uuid = uuidGenerator.generate();
131 return uuid.toString();