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