2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.netconf.odlconnector;
26 import org.apache.http.HttpStatus;
27 import org.onap.appc.adapter.netconf.NetconfClient;
28 import org.onap.appc.adapter.netconf.NetconfClientRestconf;
29 import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
30 import org.onap.appc.adapter.netconf.util.Constants;
31 import org.onap.appc.exceptions.APPCException;
32 import org.onap.appc.util.httpClient;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
36 import java.util.Properties;
38 public class NetconfClientRestconfImpl implements NetconfClient, NetconfClientRestconf {
40 private EELFLogger logger = EELFManager.getInstance().getLogger(NetconfClientRestconfImpl.class);
42 private NetconfConnectionDetails connectionDetails;
44 public NetconfClientRestconfImpl(){
48 //restconf client impl
50 @SuppressWarnings("deprecation")
52 public void configure(String configuration, String deviceMountPointName, String moduleName, String nodeName) throws APPCException {
54 logger.info("Configuring device "+deviceMountPointName+" with configuration "+configuration);
56 int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),configuration,"application/json");
58 if (httpCode != HttpStatus.SC_OK) {
59 logger.error("Configuration request failed. throwing Exception !");
60 throw new APPCException("Error configuring node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
65 public void connect(String deviceMountPointName, String payload) throws APPCException{
67 logger.info("Connecting device "+deviceMountPointName);
69 int httpCode = httpClient.postMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getConnectPath(),payload,"application/json");
71 if(httpCode != HttpStatus.SC_NO_CONTENT){
72 logger.error("Connect request failed with code "+httpCode+". throwing Exception !");
73 throw new APPCException("Error connecting device :" + deviceMountPointName);
78 public boolean checkConnection(String deviceMountPointName) throws APPCException {
79 logger.info("Checking device "+deviceMountPointName+" connectivity");
81 String result = httpClient.getMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getCheckConnectivityPath(deviceMountPointName),"application/json");
83 return result != null;
87 public void disconnect(String deviceMountPointName) throws APPCException {
88 logger.info("Disconnecting "+deviceMountPointName);
90 int httpCode = httpClient.deleteMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getDisconnectPath(deviceMountPointName),"application/json");
92 if(httpCode != HttpStatus.SC_OK){
93 logger.error("Disconnection of device "+deviceMountPointName+" failed!");
94 throw new APPCException("Disconnection of device "+deviceMountPointName+" failed!");
99 public String getConfiguration(String deviceMountPointName, String moduleName, String nodeName) throws APPCException{
100 logger.info("Getting configuration of device "+deviceMountPointName);
102 String result = httpClient.getMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),"application/json");
104 if (result == null) {
105 logger.error("Configuration request failed. throwing Exception !");
106 throw new APPCException("Error getting configuration of node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
112 //netconf client impl
115 public void connect(NetconfConnectionDetails connectionDetails) throws APPCException {
116 if(connectionDetails == null){
117 throw new APPCException("Invalid connection details - null value");
119 this.connectionDetails = connectionDetails;
120 this.connect(connectionDetails.getHost(),getPayload());
124 public String exchangeMessage(String message) throws APPCException {
130 public void configure(String configuration) throws APPCException {
131 if(connectionDetails == null){
132 throw new APPCException("Invalid connection details - null value");
135 Properties props = connectionDetails.getAdditionalProperties();
136 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")){
137 throw new APPCException("Invalid properties!");
140 String moduleName = props.getProperty("module.name");
141 String nodeName = props.getProperty("node.name");
142 String deviceMountPointName = connectionDetails.getHost();
144 int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),configuration,"application/xml");
146 if (httpCode != HttpStatus.SC_OK) {
147 logger.error("Configuration request failed. throwing Exception !");
148 throw new APPCException("Error configuring node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
153 public String getConfiguration() throws APPCException {
154 if(connectionDetails == null){
155 throw new APPCException("Invalid connection details - null value");
158 Properties props = connectionDetails.getAdditionalProperties();
159 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")){
160 throw new APPCException("Invalid properties!");
163 return this.getConfiguration(connectionDetails.getHost(),props.getProperty("module.name"),props.getProperty("node.name"));
167 public void disconnect() throws APPCException {
168 if(connectionDetails == null){
169 throw new APPCException("Invalid connection details - null value");
171 this.disconnect(connectionDetails.getHost());
175 private String getModuleConfigurePath(String deviceMountPointName, String moduleName, String nodeName){
178 String deviceSpecificPath = deviceMountPointName + "/yang-ext:mount/" + moduleName + ":" + nodeName;
180 return Constants.CONFIGURE_PATH + deviceSpecificPath;
183 private String getConnectPath(){
185 return Constants.CONNECT_PATH;
188 private String getCheckConnectivityPath(String deviceMountPointName) {
189 return Constants.CHECK_CONNECTION_PATH + deviceMountPointName;
192 private String getDisconnectPath(String deviceMountPointName) {
193 return Constants.DISCONNECT_PATH + deviceMountPointName;
196 private String getPayload() {
198 " \"config:module\":\n" +
200 " \"type\":\"odl-sal-netconf-connector-cfg:sal-netconf-connector\",\n" +
201 " \"netconf-northbound-ssh\\odl-sal-netconf-connector-cfg:name\":"+connectionDetails.getHost()+",\n" +
202 " \"odl-sal-netconf-connector-cfg:address\":"+connectionDetails.getHost()+",\n" +
203 " \"odl-sal-netconf-connector-cfg:port\":"+connectionDetails.getPort()+",\n" +
204 " \"odl-sal-netconf-connector-cfg:username\":"+connectionDetails.getUsername()+",\n" +
205 " \"odl-sal-netconf-connector-cfg:password\":"+connectionDetails.getPassword()+",\n" +
206 " \"tcp-only\":\"false\",\n" +
207 " \"odl-sal-netconf-connector-cfg:event-executor\":\n" +
209 " \"type\":\"netty:netty-event-executor\",\n" +
210 " \"name\":\"global-event-executor\"\n" +
212 " \"odl-sal-netconf-connector-cfg:binding-registry\":\n" +
214 " \"type\":\"opendaylight-md-sal-binding:binding-broker-osgi-registry\",\n" +
215 " \"name\":\"binding-osgi-broker\"\n" +
217 " \"odl-sal-netconf-connector-cfg:dom-registry\":\n" +
219 " \"type\":\"opendaylight-md-sal-dom:dom-broker-osgi-registry\",\n" +
220 " \"name\":\"dom-broker\"\n" +
222 " \"odl-sal-netconf-connector-cfg:client-dispatcher\":\n" +
224 " \"type\":\"odl-netconf-cfg:netconf-client-dispatcher\",\n" +
225 " \"name\":\"global-netconf-dispatcher\"\n" +
227 " \"odl-sal-netconf-connector-cfg:processing-executor\":\n" +
229 " \"type\":\"threadpool:threadpool\",\n" +
230 " \"name\":\"global-netconf-processing-executor\"\n" +