2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.adapter.netconf.odlconnector;
25 import org.apache.http.HttpStatus;
26 import org.openecomp.appc.adapter.netconf.NetconfClient;
27 import org.openecomp.appc.adapter.netconf.NetconfClientRestconf;
28 import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
29 import org.openecomp.appc.adapter.netconf.util.Constants;
30 import org.openecomp.appc.exceptions.APPCException;
31 import org.openecomp.appc.util.httpClient;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
35 import java.util.Properties;
37 public class NetconfClientRestconfImpl implements NetconfClient, NetconfClientRestconf {
39 private EELFLogger logger = EELFManager.getInstance().getLogger(NetconfClientRestconfImpl.class);
41 private NetconfConnectionDetails connectionDetails;
44 public NetconfClientRestconfImpl(){
47 //restconf client impl
49 @SuppressWarnings("deprecation")
51 public void configure(String configuration, String deviceMountPointName, String moduleName, String nodeName) throws APPCException {
53 logger.info("Configuring device "+deviceMountPointName+" with configuration "+configuration);
55 int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),configuration,"application/json");
57 if (httpCode != HttpStatus.SC_OK) {
58 logger.error("Configuration request failed. throwing Exception !");
59 throw new APPCException("Error configuring node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
64 public void connect(String deviceMountPointName, String payload) throws APPCException{
66 logger.info("Connecting device "+deviceMountPointName);
68 int httpCode = httpClient.postMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getConnectPath(),payload,"application/json");
70 if(httpCode != HttpStatus.SC_NO_CONTENT){
71 logger.error("Connect request failed with code "+httpCode+". throwing Exception !");
72 throw new APPCException("Error connecting device :" + deviceMountPointName);
77 public boolean checkConnection(String deviceMountPointName) throws APPCException {
78 logger.info("Checking device "+deviceMountPointName+" connectivity");
80 String result = httpClient.getMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getCheckConnectivityPath(deviceMountPointName),"application/json");
82 return result != null;
86 public void disconnect(String deviceMountPointName) throws APPCException {
87 logger.info("Disconnecting "+deviceMountPointName);
89 int httpCode = httpClient.deleteMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getDisconnectPath(deviceMountPointName),"application/json");
91 if(httpCode != HttpStatus.SC_OK){
92 logger.error("Disconnection of device "+deviceMountPointName+" failed!");
93 throw new APPCException("Disconnection of device "+deviceMountPointName+" failed!");
98 public String getConfiguration(String deviceMountPointName, String moduleName, String nodeName) throws APPCException{
99 logger.info("Getting configuration of device "+deviceMountPointName);
101 String result = httpClient.getMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),"application/json");
103 if (result == null) {
104 logger.error("Configuration request failed. throwing Exception !");
105 throw new APPCException("Error getting configuration of node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
111 //netconf client impl
114 public void connect(NetconfConnectionDetails connectionDetails) throws APPCException {
115 if(connectionDetails == null){
116 throw new APPCException("Invalid connection details - null value");
118 this.connectionDetails = connectionDetails;
119 this.connect(connectionDetails.getHost(),getPayload());
123 public String exchangeMessage(String message) throws APPCException {
129 public void configure(String configuration) throws APPCException {
130 if(connectionDetails == null){
131 throw new APPCException("Invalid connection details - null value");
134 Properties props = connectionDetails.getAdditionalProperties();
135 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")){
136 throw new APPCException("Invalid properties!");
139 String moduleName = props.getProperty("module.name");
140 String nodeName = props.getProperty("node.name");
141 String deviceMountPointName = connectionDetails.getHost();
143 int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),configuration,"application/xml");
145 if (httpCode != HttpStatus.SC_OK) {
146 logger.error("Configuration request failed. throwing Exception !");
147 throw new APPCException("Error configuring node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
152 public String getConfiguration() throws APPCException {
153 if(connectionDetails == null){
154 throw new APPCException("Invalid connection details - null value");
157 Properties props = connectionDetails.getAdditionalProperties();
158 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")){
159 throw new APPCException("Invalid properties!");
162 return this.getConfiguration(connectionDetails.getHost(),props.getProperty("module.name"),props.getProperty("node.name"));
166 public void disconnect() throws APPCException {
167 if(connectionDetails == null){
168 throw new APPCException("Invalid connection details - null value");
170 this.disconnect(connectionDetails.getHost());
174 private String getModuleConfigurePath(String deviceMountPointName, String moduleName, String nodeName){
177 String deviceSpecificPath = deviceMountPointName + "/yang-ext:mount/" + moduleName + ":" + nodeName;
179 return Constants.CONFIGURE_PATH + deviceSpecificPath;
182 private String getConnectPath(){
184 return Constants.CONNECT_PATH;
187 private String getCheckConnectivityPath(String deviceMountPointName) {
188 return Constants.CHECK_CONNECTION_PATH + deviceMountPointName;
191 private String getDisconnectPath(String deviceMountPointName) {
192 return Constants.DISCONNECT_PATH + deviceMountPointName;
195 private String getPayload() {
197 " \"config:module\":\n" +
199 " \"type\":\"odl-sal-netconf-connector-cfg:sal-netconf-connector\",\n" +
200 " \"netconf-northbound-ssh\\odl-sal-netconf-connector-cfg:name\":"+connectionDetails.getHost()+",\n" +
201 " \"odl-sal-netconf-connector-cfg:address\":"+connectionDetails.getHost()+",\n" +
202 " \"odl-sal-netconf-connector-cfg:port\":"+connectionDetails.getPort()+",\n" +
203 " \"odl-sal-netconf-connector-cfg:username\":"+connectionDetails.getUsername()+",\n" +
204 " \"odl-sal-netconf-connector-cfg:password\":"+connectionDetails.getPassword()+",\n" +
205 " \"tcp-only\":\"false\",\n" +
206 " \"odl-sal-netconf-connector-cfg:event-executor\":\n" +
208 " \"type\":\"netty:netty-event-executor\",\n" +
209 " \"name\":\"global-event-executor\"\n" +
211 " \"odl-sal-netconf-connector-cfg:binding-registry\":\n" +
213 " \"type\":\"opendaylight-md-sal-binding:binding-broker-osgi-registry\",\n" +
214 " \"name\":\"binding-osgi-broker\"\n" +
216 " \"odl-sal-netconf-connector-cfg:dom-registry\":\n" +
218 " \"type\":\"opendaylight-md-sal-dom:dom-broker-osgi-registry\",\n" +
219 " \"name\":\"dom-broker\"\n" +
221 " \"odl-sal-netconf-connector-cfg:client-dispatcher\":\n" +
223 " \"type\":\"odl-netconf-cfg:netconf-client-dispatcher\",\n" +
224 " \"name\":\"global-netconf-dispatcher\"\n" +
226 " \"odl-sal-netconf-connector-cfg:processing-executor\":\n" +
228 " \"type\":\"threadpool:threadpool\",\n" +
229 " \"name\":\"global-netconf-processing-executor\"\n" +