2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.adapter.netconf.odlconnector;
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;
38 import java.util.Properties;
40 public class NetconfClientRestconfImpl implements NetconfClient, NetconfClientRestconf {
42 private EELFLogger logger = EELFManager.getInstance().getLogger(NetconfClientRestconfImpl.class);
44 private NetconfConnectionDetails connectionDetails;
45 private String appFormat = "application/json";
47 public NetconfClientRestconfImpl(){
51 //restconf client impl
53 @SuppressWarnings("deprecation")
55 public void configure(String configuration, String deviceMountPointName, String moduleName, String nodeName) throws APPCException {
57 logger.info("Configuring device " + deviceMountPointName + " with configuration " + configuration);
59 int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,
60 getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), configuration, appFormat);
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);
70 public void connect(String deviceMountPointName, String payload) throws APPCException{
72 logger.info("Connecting device " + deviceMountPointName);
74 int httpCode = httpClient.postMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
75 getConnectPath(), payload, appFormat);
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);
84 public boolean checkConnection(String deviceMountPointName) throws APPCException {
85 logger.info("Checking device " + deviceMountPointName + " connectivity");
87 String result = httpClient.getMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP,
88 Constants.CONTROLLER_PORT, getCheckConnectivityPath(deviceMountPointName), appFormat);
90 return result != null;
94 public void disconnect(String deviceMountPointName) throws APPCException {
95 logger.info("Disconnecting " + deviceMountPointName);
97 int httpCode = httpClient.deleteMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
98 getDisconnectPath(deviceMountPointName), appFormat);
100 if(httpCode != HttpStatus.SC_OK){
101 logger.error("Disconnection of device " + deviceMountPointName + " failed!");
102 throw new APPCException("Disconnection of device " + deviceMountPointName + " failed!");
107 public String getConfiguration(String deviceMountPointName, String moduleName, String nodeName) throws APPCException{
108 logger.info("Getting configuration of device " + deviceMountPointName);
110 String result = httpClient.getMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
111 getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), appFormat);
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);
122 //netconf client impl
125 public void connect(NetconfConnectionDetails connectionDetails) throws APPCException {
126 if(connectionDetails == null){
127 throw new APPCException("Invalid connection details - null value");
129 this.connectionDetails = connectionDetails;
130 this.connect(connectionDetails.getHost(), getPayload());
134 public String exchangeMessage(String message) throws APPCException {
140 public void configure(String configuration) throws APPCException {
141 if(connectionDetails == null){
142 throw new APPCException("Invalid connection details - null value");
145 Properties props = connectionDetails.getAdditionalProperties();
146 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")) {
147 throw new APPCException("Invalid properties!");
150 String moduleName = props.getProperty("module.name");
151 String nodeName = props.getProperty("node.name");
152 String deviceMountPointName = connectionDetails.getHost();
154 int httpCode = httpClient.putMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
155 getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), configuration, "application/xml");
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);
165 public String getConfiguration() throws APPCException {
166 if(connectionDetails == null){
167 throw new APPCException("Invalid connection details - null value");
170 Properties props = connectionDetails.getAdditionalProperties();
171 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")) {
172 throw new APPCException("Invalid properties!");
175 return this.getConfiguration(connectionDetails.getHost(), props.getProperty("module.name"),
176 props.getProperty("node.name"));
180 public void disconnect() throws APPCException {
181 if(connectionDetails == null){
182 throw new APPCException("Invalid connection details - null value");
184 this.disconnect(connectionDetails.getHost());
188 private String getModuleConfigurePath(String deviceMountPointName, String moduleName, String nodeName){
190 String deviceSpecificPath = deviceMountPointName + "/yang-ext:mount/" + moduleName + ":" + nodeName;
192 return Constants.CONFIGURE_PATH + deviceSpecificPath;
195 private String getConnectPath(){
197 return Constants.CONNECT_PATH;
200 private String getCheckConnectivityPath(String deviceMountPointName) {
201 return Constants.CHECK_CONNECTION_PATH + deviceMountPointName;
204 private String getDisconnectPath(String deviceMountPointName) {
205 return Constants.DISCONNECT_PATH + deviceMountPointName;
208 private String getPayload() {
210 " \"config:module\":\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" +
221 " \"type\":\"netty:netty-event-executor\",\n" +
222 " \"name\":\"global-event-executor\"\n" +
224 " \"odl-sal-netconf-connector-cfg:binding-registry\":\n" +
226 " \"type\":\"opendaylight-md-sal-binding:binding-broker-osgi-registry\",\n" +
227 " \"name\":\"binding-osgi-broker\"\n" +
229 " \"odl-sal-netconf-connector-cfg:dom-registry\":\n" +
231 " \"type\":\"opendaylight-md-sal-dom:dom-broker-osgi-registry\",\n" +
232 " \"name\":\"dom-broker\"\n" +
234 " \"odl-sal-netconf-connector-cfg:client-dispatcher\":\n" +
236 " \"type\":\"odl-netconf-cfg:netconf-client-dispatcher\",\n" +
237 " \"name\":\"global-netconf-dispatcher\"\n" +
239 " \"odl-sal-netconf-connector-cfg:processing-executor\":\n" +
241 " \"type\":\"threadpool:threadpool\",\n" +
242 " \"name\":\"global-netconf-processing-executor\"\n" +