835f24c58943589acc9b88d13f15d16b02a328a4
[oam/tr069-adapter.git] / acs / nbi / src / main / java / org / commscope / tr069adapter / acs / nbi / impl / ACSServiceAPIImpl.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 package org.commscope.tr069adapter.acs.nbi.impl;\r
20 \r
21 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.MAPPER_SERVICE_QUALILFIER;\r
22 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.TR069_NBI_REQUEST_Q;\r
23 \r
24 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;\r
25 import org.commscope.tr069adapter.acs.common.exception.MapperServiceException;\r
26 import org.commscope.tr069adapter.acs.common.mapper.ACSServiceAPI;\r
27 import org.commscope.tr069adapter.acs.nbi.util.OperationIdGenerator;\r
28 import org.slf4j.Logger;\r
29 import org.slf4j.LoggerFactory;\r
30 import org.slf4j.MDC;\r
31 import org.springframework.beans.factory.annotation.Autowired;\r
32 import org.springframework.jms.core.JmsTemplate;\r
33 import org.springframework.stereotype.Component;\r
34 \r
35 @Component(MAPPER_SERVICE_QUALILFIER)\r
36 public class ACSServiceAPIImpl implements ACSServiceAPI {\r
37 \r
38   private static final Logger logger = LoggerFactory.getLogger(ACSServiceAPIImpl.class);\r
39 \r
40   private static final String CLIENT_STR = "client";\r
41 \r
42   @Autowired\r
43   private OperationIdGenerator opIdGenerator;\r
44 \r
45   @Autowired\r
46   private JmsTemplate jmsTemplate;\r
47 \r
48   /*\r
49    * (non-Javadoc)\r
50    * \r
51    * @see org.commscope.tr069adapter.acs.mapper.ACSServiceAPI#performDeviceOperation(org.commscope.\r
52    * tr069adapter.acs.DeviceRPCRequest)\r
53    */\r
54   @Override\r
55   public long performDeviceOperation(DeviceRPCRequest deviceRPCRequest)\r
56       throws MapperServiceException {\r
57 \r
58     Long opId = 0l;\r
59     try {\r
60       if (deviceRPCRequest != null && deviceRPCRequest.getDeviceDetails() != null) {\r
61         MDC.put(CLIENT_STR, deviceRPCRequest.getDeviceDetails().getDeviceId());\r
62       }\r
63 \r
64       // validate the request and reject if not valid.\r
65       if (null == deviceRPCRequest) {\r
66         logger.error("Received null Mapper Request.");\r
67         throw new MapperServiceException("Received null Mapper Request.");\r
68       } else if (null == deviceRPCRequest.getOpDetails()) {\r
69         logger.error("Received null operation details.");\r
70         throw new MapperServiceException("Received null operation details.");\r
71       } else if (null == deviceRPCRequest.getOpDetails().getOpCode()) {\r
72         logger.error("Received null operation code.");\r
73         throw new MapperServiceException("Received null operation code.");\r
74       }\r
75 \r
76       logger.info("Received request to perform device operation. OperationCode: {}",\r
77           deviceRPCRequest.getOpDetails().getOpCode());\r
78       opId = opIdGenerator.generateOpId();\r
79       logger.debug("The operation ID generated for processing the Device RPC request is - {}",\r
80           opId);\r
81       // set opId and forward the request\r
82       deviceRPCRequest.setOperationId(opId);\r
83       jmsTemplate.convertAndSend(TR069_NBI_REQUEST_Q, deviceRPCRequest);\r
84 \r
85       logger.debug(\r
86           "Successfully posted the Mapper Request to Queue with OperationId : {} OperationCode : {}",\r
87           deviceRPCRequest.getOperationId(), deviceRPCRequest.getOpDetails().getOpCode());\r
88     } catch (Exception ex) {\r
89       MapperServiceException mapperEx =\r
90           new MapperServiceException("ACS Internal Error. Unknown Exception. Details :", ex);\r
91       logger.error(mapperEx.getMessage());\r
92       throw mapperEx;\r
93     } finally {\r
94       MDC.remove(CLIENT_STR);\r
95     }\r
96 \r
97     return opId;\r
98   }\r
99 \r
100 }\r