sonar code issues addressed
[oam/tr069-adapter.git] / acs / nbi / src / main / java / org / commscope / tr069adapter / acs / nbi / impl / ACSServiceAPIImpl.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.nbi.impl;
20
21 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.MAPPER_SERVICE_QUALILFIER;
22 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.TR069_NBI_REQUEST_Q;
23
24 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
25 import org.commscope.tr069adapter.acs.common.OperationCode;
26 import org.commscope.tr069adapter.acs.common.exception.MapperServiceException;
27 import org.commscope.tr069adapter.acs.common.mapper.ACSServiceAPI;
28 import org.commscope.tr069adapter.acs.common.utils.OperationIdGenerator;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.slf4j.MDC;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.jms.core.JmsTemplate;
34 import org.springframework.stereotype.Component;
35
36 @Component(MAPPER_SERVICE_QUALILFIER)
37 public class ACSServiceAPIImpl implements ACSServiceAPI {
38
39   private static final Logger logger = LoggerFactory.getLogger(ACSServiceAPIImpl.class);
40
41   private static final String CLIENT_STR = "client";
42
43   @Autowired
44   private OperationIdGenerator opIdGenerator;
45
46   @Autowired
47   private JmsTemplate jmsTemplate;
48
49   /*
50    * (non-Javadoc)
51    * 
52    * @see org.commscope.tr069adapter.acs.mapper.ACSServiceAPI#performDeviceOperation(org.commscope.
53    * tr069adapter.acs.DeviceRPCRequest)
54    */
55   @Override
56   public long performDeviceOperation(DeviceRPCRequest deviceRPCRequest)
57       throws MapperServiceException {
58
59     Long opId = 0l;
60     try {
61       if (deviceRPCRequest != null && deviceRPCRequest.getDeviceDetails() != null) {
62         MDC.put(CLIENT_STR, deviceRPCRequest.getDeviceDetails().getDeviceId());
63       }
64
65       // validate the request and reject if not valid.
66       if (null == deviceRPCRequest) {
67         logger.error("Received null Mapper Request.");
68         throw new MapperServiceException("Received null Mapper Request.");
69       } else if (null == deviceRPCRequest.getOpDetails()) {
70         logger.error("Received null operation details.");
71         throw new MapperServiceException("Received null operation details.");
72       } else if (null == deviceRPCRequest.getOpDetails().getOpCode()) {
73         logger.error("Received null operation code.");
74         throw new MapperServiceException("Received null operation code.");
75       }
76       OperationCode opCode = deviceRPCRequest.getOpDetails().getOpCode();
77       logger.info("Received request to perform device operation. OperationCode: {}", opCode);
78       opId = opIdGenerator.generateOpId();
79       logger.debug("The operation ID generated for processing the Device RPC request is - {}",
80           opId);
81       // set opId and forward the request
82       deviceRPCRequest.setOperationId(opId);
83       jmsTemplate.convertAndSend(TR069_NBI_REQUEST_Q, deviceRPCRequest);
84
85       logger.debug(
86           "Successfully posted the Mapper Request to Queue with OperationId : {} OperationCode : {}",
87           deviceRPCRequest.getOperationId(), deviceRPCRequest.getOpDetails().getOpCode());
88     } catch (Exception ex) {
89       MapperServiceException mapperEx =
90           new MapperServiceException("ACS Internal Error. Unknown Exception. Details :", ex);
91       logger.error(mapperEx.getMessage());
92       throw mapperEx;
93     } finally {
94       MDC.remove(CLIENT_STR);
95     }
96
97     return opId;
98   }
99
100 }