Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / netconf / NetConfServerManager.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.netconf;
20
21 import org.commscope.tr069adapter.mapper.MapperConfigProperties;
22 import org.commscope.tr069adapter.mapper.model.NetConfServerDetails;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.http.HttpEntity;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.MediaType;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.stereotype.Component;
31 import org.springframework.util.LinkedMultiValueMap;
32 import org.springframework.util.MultiValueMap;
33 import org.springframework.web.client.RestTemplate;
34
35 @Component
36 public class NetConfServerManager {
37
38   private static final Logger LOG = LoggerFactory.getLogger(NetConfServerManager.class);
39
40   private static String createServerSVC = "createServer";
41
42   @Autowired
43   MapperConfigProperties config;
44
45   @Autowired
46   RestTemplate restTemplate;
47
48   public NetConfServerDetails createNetconfServer(String deviceID, String enodeBName,
49       String swVersion, String hwVersion) {
50
51     NetConfServerDetails result = null;
52     // handle exception
53     final String uri = getNetconfServerManagerRestUri() + "/" + createServerSVC;
54     LOG.debug("Sending create netconf server request for device id {}", deviceID);
55     try {
56       MultiValueMap<String, String> uriParams = new LinkedMultiValueMap<>();
57       uriParams.add("deviceId", deviceID);
58       uriParams.add("enodeBName", enodeBName);
59       uriParams.add("swVersion", swVersion);
60       uriParams.add("hwVersion", hwVersion);
61       HttpHeaders headers = new HttpHeaders();
62       headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
63       final HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(uriParams, headers);
64       ResponseEntity<NetConfServerDetails> res =
65           restTemplate.postForEntity(uri, entity, NetConfServerDetails.class);
66       result = res.getBody();
67       LOG.debug("Successfully created netconf server for device id. {} , response= {}", deviceID,
68           result);
69     } catch (Exception e) {
70       LOG.error("Exception while creating netconf server request for device id {}", deviceID, e);
71     }
72     return result;
73   }
74
75   private String getNetconfServerManagerRestUri() {
76     return config.getNbiServerManagerUri();
77   }
78
79 }