Change nexus values to properties
[appc.git] / app-c / appc / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / main / java / org / openecomp / appc / adapter / netconf / odlconnector / NetconfClientRestconfImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.adapter.netconf.odlconnector;
23
24 import org.apache.http.HttpStatus;
25 import org.openecomp.appc.adapter.netconf.NetconfClient;
26 import org.openecomp.appc.adapter.netconf.NetconfClientRestconf;
27 import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
28 import org.openecomp.appc.adapter.netconf.util.Constants;
29 import org.openecomp.appc.exceptions.APPCException;
30 import org.openecomp.appc.util.httpClient;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33
34 import java.util.Properties;
35
36 public class NetconfClientRestconfImpl implements NetconfClient, NetconfClientRestconf {
37
38     private EELFLogger logger = EELFManager.getInstance().getLogger(NetconfClientRestconfImpl.class);
39
40     private NetconfConnectionDetails connectionDetails;
41
42     //constructor
43     public NetconfClientRestconfImpl(){
44     }
45
46     //restconf client impl
47
48     @SuppressWarnings("deprecation")
49     @Override
50     public void configure(String configuration, String deviceMountPointName, String moduleName, String nodeName) throws APPCException {
51
52         logger.info("Configuring device "+deviceMountPointName+" with configuration "+configuration);
53
54         int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),configuration,"application/json");
55
56         if (httpCode != HttpStatus.SC_OK) {
57             logger.error("Configuration request failed. throwing Exception !");
58             throw new APPCException("Error configuring node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
59         }
60     }
61
62     @Override
63     public void connect(String deviceMountPointName, String payload) throws APPCException{
64
65         logger.info("Connecting device "+deviceMountPointName);
66
67         int httpCode = httpClient.postMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getConnectPath(),payload,"application/json");
68
69         if(httpCode != HttpStatus.SC_NO_CONTENT){
70             logger.error("Connect request failed with code "+httpCode+". throwing Exception !");
71             throw new APPCException("Error connecting device :" + deviceMountPointName);
72         }
73     }
74
75     @Override
76     public boolean checkConnection(String deviceMountPointName) throws APPCException {
77         logger.info("Checking device "+deviceMountPointName+" connectivity");
78
79         String result = httpClient.getMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getCheckConnectivityPath(deviceMountPointName),"application/json");
80
81         return result != null;
82     }
83
84     @Override
85     public void disconnect(String deviceMountPointName) throws APPCException {
86         logger.info("Disconnecting "+deviceMountPointName);
87
88         int httpCode = httpClient.deleteMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getDisconnectPath(deviceMountPointName),"application/json");
89
90         if(httpCode != HttpStatus.SC_OK){
91             logger.error("Disconnection of device "+deviceMountPointName+" failed!");
92             throw new APPCException("Disconnection of device "+deviceMountPointName+" failed!");
93         }
94     }
95
96     @Override
97     public String getConfiguration(String deviceMountPointName, String moduleName, String nodeName) throws APPCException{
98         logger.info("Getting configuration of device "+deviceMountPointName);
99
100         String result = httpClient.getMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),"application/json");
101
102         if (result == null) {
103             logger.error("Configuration request failed. throwing Exception !");
104             throw new APPCException("Error getting configuration of node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
105         }
106
107         return result;
108     }
109
110     //netconf client impl
111
112     @Override
113     public void connect(NetconfConnectionDetails connectionDetails) throws APPCException {
114         if(connectionDetails == null){
115             throw new APPCException("Invalid connection details - null value");
116         }
117         this.connectionDetails = connectionDetails;
118         this.connect(connectionDetails.getHost(),getPayload());
119     }
120
121     @Override
122     public String exchangeMessage(String message) throws APPCException {
123         // TODO implement
124         return null;
125     }
126
127     @Override
128     public void configure(String configuration) throws APPCException {
129         if(connectionDetails == null){
130             throw new APPCException("Invalid connection details - null value");
131         }
132
133         Properties props = connectionDetails.getAdditionalProperties();
134         if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")){
135             throw new APPCException("Invalid properties!");
136         }
137
138         String moduleName = props.getProperty("module.name");
139         String nodeName = props.getProperty("node.name");
140         String deviceMountPointName = connectionDetails.getHost();
141
142         int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),configuration,"application/xml");
143
144         if (httpCode != HttpStatus.SC_OK) {
145             logger.error("Configuration request failed. throwing Exception !");
146             throw new APPCException("Error configuring node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
147         }
148     }
149
150     @Override
151     public String getConfiguration() throws APPCException {
152         if(connectionDetails == null){
153             throw new APPCException("Invalid connection details - null value");
154         }
155
156         Properties props = connectionDetails.getAdditionalProperties();
157         if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")){
158             throw new APPCException("Invalid properties!");
159         }
160
161         return this.getConfiguration(connectionDetails.getHost(),props.getProperty("module.name"),props.getProperty("node.name"));
162     }
163
164     @Override
165     public void disconnect() throws APPCException {
166         if(connectionDetails == null){
167             throw new APPCException("Invalid connection details - null value");
168         }
169         this.disconnect(connectionDetails.getHost());
170     }
171
172     //private methods
173     private String getModuleConfigurePath(String deviceMountPointName, String moduleName, String nodeName){
174
175
176         String deviceSpecificPath = deviceMountPointName + "/yang-ext:mount/" + moduleName + ":" + nodeName;
177
178         return Constants.CONFIGURE_PATH + deviceSpecificPath;
179     }
180
181     private String getConnectPath(){
182
183         return Constants.CONNECT_PATH;
184     }
185
186     private String getCheckConnectivityPath(String deviceMountPointName) {
187         return Constants.CHECK_CONNECTION_PATH + deviceMountPointName;
188     }
189
190     private String getDisconnectPath(String deviceMountPointName) {
191         return Constants.DISCONNECT_PATH + deviceMountPointName;
192     }
193
194     private String getPayload() {
195         return "{\n" +
196                 "    \"config:module\":\n" +
197                 "        {\n" +
198                 "        \"type\":\"odl-sal-netconf-connector-cfg:sal-netconf-connector\",\n" +
199                 "        \"netconf-northbound-ssh\\odl-sal-netconf-connector-cfg:name\":"+connectionDetails.getHost()+",\n" +
200                 "        \"odl-sal-netconf-connector-cfg:address\":"+connectionDetails.getHost()+",\n" +
201                 "        \"odl-sal-netconf-connector-cfg:port\":"+connectionDetails.getPort()+",\n" +
202                 "        \"odl-sal-netconf-connector-cfg:username\":"+connectionDetails.getUsername()+",\n" +
203                 "        \"odl-sal-netconf-connector-cfg:password\":"+connectionDetails.getPassword()+",\n" +
204                 "        \"tcp-only\":\"false\",\n" +
205                 "        \"odl-sal-netconf-connector-cfg:event-executor\":\n" +
206                 "            {\n" +
207                 "            \"type\":\"netty:netty-event-executor\",\n" +
208                 "            \"name\":\"global-event-executor\"\n" +
209                 "            },\n" +
210                 "        \"odl-sal-netconf-connector-cfg:binding-registry\":\n" +
211                 "            {\n" +
212                 "            \"type\":\"opendaylight-md-sal-binding:binding-broker-osgi-registry\",\n" +
213                 "            \"name\":\"binding-osgi-broker\"\n" +
214                 "            },\n" +
215                 "        \"odl-sal-netconf-connector-cfg:dom-registry\":\n" +
216                 "            {\n" +
217                 "            \"type\":\"opendaylight-md-sal-dom:dom-broker-osgi-registry\",\n" +
218                 "            \"name\":\"dom-broker\"\n" +
219                 "            },\n" +
220                 "        \"odl-sal-netconf-connector-cfg:client-dispatcher\":\n" +
221                 "            {\n" +
222                 "            \"type\":\"odl-netconf-cfg:netconf-client-dispatcher\",\n" +
223                 "            \"name\":\"global-netconf-dispatcher\"\n" +
224                 "            },\n" +
225                 "        \"odl-sal-netconf-connector-cfg:processing-executor\":\n" +
226                 "            {\n" +
227                 "            \"type\":\"threadpool:threadpool\",\n" +
228                 "            \"name\":\"global-netconf-processing-executor\"\n" +
229                 "        }\n" +
230                 "    }\n" +
231                 "}";
232     }
233 }