Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / acs / requestprocessor / src / main / java / org / commscope / tr069adapter / acs / requestprocessor / impl / SessionManager.java
1 /*
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=======================================================================
17  */
18
19 package org.commscope.tr069adapter.acs.requestprocessor.impl;
20
21 import com.fasterxml.uuid.EthernetAddress;
22 import com.fasterxml.uuid.Generators;
23 import com.fasterxml.uuid.impl.TimeBasedGenerator;
24
25 import java.util.Optional;
26 import java.util.UUID;
27
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;
40
41 @Component
42 public class SessionManager {
43
44   private static final Logger logger = LoggerFactory.getLogger(SessionManager.class);
45
46   @Autowired
47   SessionRepository sessionRepository;
48
49   /**
50    * @param deviceId
51    * @return
52    * @throws SessionManagerException
53    */
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();
59     }
60     return SessionManagerUtility.convertToDTO(session);
61   }
62
63   /**
64    * @param sessionId
65    * @return
66    * @throws SessionManagerException
67    */
68   public SessionDTO getSessionBySessionId(String sessionId) throws SessionManagerException {
69
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);
74     }
75     return SessionManagerUtility.convertToDTO(sessionManagerEntity);
76   }
77
78   /**
79    * @param session
80    * @return
81    * @throws SessionManagerException
82    */
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);
89       throw sme;
90     }
91     logger.debug("Creating a new session for the device");
92     return SessionManagerUtility
93         .convertToDTO(sessionRepository.save(SessionManagerUtility.convertToEntity(session)));
94   }
95
96   /**
97    * @param dto
98    * @return
99    * @throws SessionManagerException
100    */
101   public SessionDTO updateSession(SessionDTO dto) {
102     return SessionManagerUtility
103         .convertToDTO(sessionRepository.save(SessionManagerUtility.convertToEntity(dto)));
104   }
105
106   /**
107    * @param deviceId
108    * @throws SessionConcurrentAccessException
109    */
110   public void deleteSession(String deviceId) {
111     sessionRepository.deleteById(deviceId);
112   }
113
114   /**
115    * @param deviceId
116    * @return
117    * @throws SessionManagerException
118    */
119   public SessionDTO getLockedSession(String deviceId) {
120     logger.debug("Acquiring the session lock for the device");
121     return SessionManagerUtility.convertToDTO(sessionRepository.findByDeviceId(deviceId));
122   }
123
124   /**
125    * @return
126    */
127   public String generateUniqueSessionID() {
128     EthernetAddress addr = EthernetAddress.fromInterface();
129     TimeBasedGenerator uuidGenerator = Generators.timeBasedGenerator(addr);
130     UUID uuid = uuidGenerator.generate();
131     return uuid.toString();
132   }
133
134 }