sonar code issues addressed
[oam/tr069-adapter.git] / acs / requestprocessor / src / main / java / org / commscope / tr069adapter / acs / requestprocessor / impl / TR069EventNotificationService.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 static org.commscope.tr069adapter.acs.common.utils.AcsConstants.NBI_NOTIFICATION_Q;
22 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.NBI_OP_RESULT_Q;
23
24 import org.commscope.tr069adapter.acs.common.DeviceInform;
25 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
26 import org.commscope.tr069adapter.acs.common.dto.CustomOperationCode;
27 import org.commscope.tr069adapter.acs.common.dto.TR069InformType;
28 import org.commscope.tr069adapter.acs.common.dto.TR069OperationCode;
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.context.annotation.Profile;
34 import org.springframework.jms.core.JmsTemplate;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 //@Profile(value="default")
39 public class TR069EventNotificationService {
40
41   private static final Logger logger = LoggerFactory.getLogger(TR069EventNotificationService.class);
42
43   private static final String CLIENT_STR = "client";
44
45   @Autowired
46   private JmsTemplate jmsTemplate;
47
48   /**
49    * @param deviceNotification
50    */
51   public void sendDeviceInformToNBI(DeviceInform deviceNotification) {
52     String deviceId = deviceNotification.getDeviceDetails().getDeviceId();
53     try {
54       MDC.put(CLIENT_STR, deviceId);
55
56       TR069InformType notificationType = (TR069InformType) deviceNotification.getInformType();
57
58       logger.debug("Device Inform Event received: '{}'", notificationType.getNotificationCode());
59       jmsTemplate.convertAndSend(NBI_NOTIFICATION_Q, deviceNotification);
60       logger.debug("Successfully posted the device inform event to DM to forward to NBI");
61     } catch (Exception e) {
62       logger.error("Posting Device Inform event to mapper failed, Reason: {}", e.getMessage());
63     } finally {
64       MDC.remove(CLIENT_STR);
65     }
66   }
67
68   /**
69    * @param deviceRPCResponse
70    */
71   public void sendOperationResultToNBI(DeviceRPCResponse deviceRPCResponse) {
72     String deviceId = deviceRPCResponse.getDeviceDetails().getDeviceId();
73     try {
74       MDC.put(CLIENT_STR, deviceId);
75       String opercode;
76       if (deviceRPCResponse.getOperationResponse()
77           .getOperationCode() instanceof TR069OperationCode) {
78         TR069OperationCode operCode =
79             (TR069OperationCode) deviceRPCResponse.getOperationResponse().getOperationCode();
80         opercode = operCode.name();
81         logger.debug("Device RPC Response received for operation: {}  with operation ID: {}",
82             opercode, deviceRPCResponse.getOperationId());
83
84       } else if (deviceRPCResponse.getOperationResponse()
85           .getOperationCode() instanceof CustomOperationCode) {
86         CustomOperationCode operCode =
87             (CustomOperationCode) deviceRPCResponse.getOperationResponse().getOperationCode();
88         opercode = operCode.name();
89         logger.debug("Device RPC Response received for operation: {}  with operation ID: {}",
90             opercode, deviceRPCResponse.getOperationId());
91       }
92       jmsTemplate.convertAndSend(NBI_OP_RESULT_Q, deviceRPCResponse);
93       logger.debug("Successfully posted the operation result event to DM to forward to NBI");
94     } catch (Exception e) {
95       logger.error("Posting Device RPC response event to mapper failed, Reason: {}",
96           e.getMessage());
97     } finally {
98       MDC.remove(CLIENT_STR);
99     }
100   }
101
102 }