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