675a3eece9476e1036f03db4a8410500d50a0c73
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / netconf / NetConfServerManager.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 package org.commscope.tr069adapter.mapper.netconf;\r
20 \r
21 import org.commscope.tr069adapter.mapper.MapperConfigProperties;\r
22 import org.commscope.tr069adapter.mapper.model.NetConfServerDetails;\r
23 import org.slf4j.Logger;\r
24 import org.slf4j.LoggerFactory;\r
25 import org.springframework.beans.factory.annotation.Autowired;\r
26 import org.springframework.http.HttpEntity;\r
27 import org.springframework.http.HttpHeaders;\r
28 import org.springframework.http.MediaType;\r
29 import org.springframework.http.ResponseEntity;\r
30 import org.springframework.stereotype.Component;\r
31 import org.springframework.util.LinkedMultiValueMap;\r
32 import org.springframework.util.MultiValueMap;\r
33 import org.springframework.web.client.RestTemplate;\r
34 \r
35 @Component\r
36 public class NetConfServerManager {\r
37 \r
38   private static final Logger LOG = LoggerFactory.getLogger(NetConfServerManager.class);\r
39 \r
40   private static String createServerSVC = "createServer";\r
41 \r
42   @Autowired\r
43   MapperConfigProperties config;\r
44 \r
45   @Autowired\r
46   RestTemplate restTemplate;\r
47 \r
48   public NetConfServerDetails createNetconfServer(String deviceID, String enodeBName) {\r
49 \r
50     NetConfServerDetails result = null;\r
51     // handle exception\r
52     final String uri = getNetconfServerManagerRestUri() + "/" + createServerSVC;\r
53     LOG.debug("Sending create netconf server request for device id {}", deviceID);\r
54     try {\r
55       MultiValueMap<String, String> uriParams = new LinkedMultiValueMap<>();\r
56       uriParams.add("deviceId", deviceID);\r
57       uriParams.add("enodeBName", enodeBName);\r
58       HttpHeaders headers = new HttpHeaders();\r
59       headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);\r
60       final HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(uriParams, headers);\r
61       ResponseEntity<NetConfServerDetails> res =\r
62           restTemplate.postForEntity(uri, entity, NetConfServerDetails.class);\r
63       result = res.getBody();\r
64       LOG.debug("Successfully created netconf server for device id. {} , response= {}", deviceID,\r
65           result);\r
66     } catch (Exception e) {\r
67       LOG.error("Exception while creating netconf server request for device id {}", deviceID, e);\r
68     }\r
69     return result;\r
70   }\r
71 \r
72   private String getNetconfServerManagerRestUri() {\r
73     return config.getNbiServerManagerUri();\r
74   }\r
75 \r
76 }\r