Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / ves / VESNotificationSender.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.mapper.ves;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.commscope.tr069adapter.acs.common.DeviceInform;
25 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
26 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
27 import org.commscope.tr069adapter.acs.common.ParameterDTO;
28 import org.commscope.tr069adapter.mapper.MapperConfigProperties;
29 import org.commscope.tr069adapter.mapper.acs.impl.PnPPreProvisioningHandler;
30 import org.commscope.tr069adapter.mapper.model.NetConfServerDetails;
31 import org.commscope.tr069adapter.mapper.model.VESNotification;
32 import org.commscope.tr069adapter.mapper.model.VESNotificationResponse;
33 import org.commscope.tr069adapter.mapper.util.MOMetaDataUtil;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38 import org.springframework.web.client.RestTemplate;
39
40 @Component
41 public class VESNotificationSender {
42
43   private static final Logger LOG = LoggerFactory.getLogger(VESNotificationSender.class);
44
45   @Autowired
46   MapperConfigProperties config;
47
48   @Autowired
49   PnPPreProvisioningHandler pnpPreProvisioningHandler;
50
51   @Autowired
52   VESNotificationSender vesnotiSender;
53
54   @Autowired
55   RestTemplate restTemplate;
56
57   @Autowired
58   MOMetaDataUtil metaDataUtil;
59
60   public VESNotificationResponse sendNotification(DeviceInform deviceInform,
61       NetConfServerDetails serverInfo) {
62     final String uri = getUri();
63     LOG.debug("Posting ves event to ves notifier {}", uri);
64
65     VESNotification vesNotifi = new VESNotification();
66     if (deviceInform != null) {
67       // Replace TR-069 parameter names with NETCONF parameter names
68       List<ParameterDTO> parameters = deviceInform.getParameters();
69       if (parameters != null && !parameters.isEmpty()) {
70         List<ParameterDTO> netconfParameters = new ArrayList<>();
71         for (ParameterDTO param : parameters) {
72           String paramXPath = metaDataUtil.getNetconfXPathNameByTR69NameWithIndexes(
73               param.getParamName(), deviceInform.getDeviceDetails().getSoftwareVersion(),
74               deviceInform.getDeviceDetails().getHardwareVersion());
75           if (paramXPath != null)
76             netconfParameters
77                 .add(new ParameterDTO(paramXPath, param.getParamValue(), param.getDataType()));
78           else {
79             LOG.warn("Skipping param {}, as it is not present in mapping configuration",
80                 param.getParamName());
81           }
82         }
83         vesNotifi.setNetconfParameters(netconfParameters);
84       }
85       vesNotifi.seteNodeBName(
86           pnpPreProvisioningHandler.getEnodeBName(deviceInform.getDeviceDetails().getDeviceId(),
87               deviceInform.getDeviceDetails().getSoftwareVersion(),
88               deviceInform.getDeviceDetails().getHardwareVersion()));
89     } else {
90       vesNotifi.seteNodeBName(serverInfo.getEnodeBName());
91     }
92     vesNotifi.setNetconfDetails(serverInfo);
93     vesNotifi.setDevnotification(deviceInform);
94
95     return restTemplate.postForObject(uri, vesNotifi, VESNotificationResponse.class);
96   }
97
98   public DeviceRPCResponse sendEditConfigNotification(DeviceRPCRequest deviceRPCRequest) {
99     final String uri = config.getVerConfigUri() + "/editConfig";
100     LOG.debug("Posting edit config request to ves agent {}", uri);
101     return restTemplate.postForObject(uri, deviceRPCRequest, DeviceRPCResponse.class);
102   }
103
104   public DeviceRPCResponse sendGetConfigNotification(DeviceRPCRequest deviceRPCRequest) {
105     final String uri = config.getVerConfigUri() + "/getConfig";
106     LOG.debug("Posting get config request to ves agent {}", uri);
107     return restTemplate.postForObject(uri, deviceRPCRequest, DeviceRPCResponse.class);
108   }
109
110
111   private String getUri() {
112     return config.getVesUri();
113   }
114
115 }