2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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=========================================================
22 package org.openecomp.appc.adapter.netconf.odlconnector;
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;
34 import java.util.Properties;
36 public class NetconfClientRestconfImpl implements NetconfClient, NetconfClientRestconf {
38 private EELFLogger logger = EELFManager.getInstance().getLogger(NetconfClientRestconfImpl.class);
40 private NetconfConnectionDetails connectionDetails;
43 public NetconfClientRestconfImpl(){
46 //restconf client impl
48 @SuppressWarnings("deprecation")
50 public void configure(String configuration, String deviceMountPointName, String moduleName, String nodeName) throws APPCException {
52 logger.info("Configuring device "+deviceMountPointName+" with configuration "+configuration);
54 int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),configuration,"application/json");
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);
63 public void connect(String deviceMountPointName, String payload) throws APPCException{
65 logger.info("Connecting device "+deviceMountPointName);
67 int httpCode = httpClient.postMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getConnectPath(),payload,"application/json");
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);
76 public boolean checkConnection(String deviceMountPointName) throws APPCException {
77 logger.info("Checking device "+deviceMountPointName+" connectivity");
79 String result = httpClient.getMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getCheckConnectivityPath(deviceMountPointName),"application/json");
81 return result != null;
85 public void disconnect(String deviceMountPointName) throws APPCException {
86 logger.info("Disconnecting "+deviceMountPointName);
88 int httpCode = httpClient.deleteMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getDisconnectPath(deviceMountPointName),"application/json");
90 if(httpCode != HttpStatus.SC_OK){
91 logger.error("Disconnection of device "+deviceMountPointName+" failed!");
92 throw new APPCException("Disconnection of device "+deviceMountPointName+" failed!");
97 public String getConfiguration(String deviceMountPointName, String moduleName, String nodeName) throws APPCException{
98 logger.info("Getting configuration of device "+deviceMountPointName);
100 String result = httpClient.getMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),"application/json");
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);
110 //netconf client impl
113 public void connect(NetconfConnectionDetails connectionDetails) throws APPCException {
114 if(connectionDetails == null){
115 throw new APPCException("Invalid connection details - null value");
117 this.connectionDetails = connectionDetails;
118 this.connect(connectionDetails.getHost(),getPayload());
122 public String exchangeMessage(String message) throws APPCException {
128 public void configure(String configuration) throws APPCException {
129 if(connectionDetails == null){
130 throw new APPCException("Invalid connection details - null value");
133 Properties props = connectionDetails.getAdditionalProperties();
134 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")){
135 throw new APPCException("Invalid properties!");
138 String moduleName = props.getProperty("module.name");
139 String nodeName = props.getProperty("node.name");
140 String deviceMountPointName = connectionDetails.getHost();
142 int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),configuration,"application/xml");
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);
151 public String getConfiguration() throws APPCException {
152 if(connectionDetails == null){
153 throw new APPCException("Invalid connection details - null value");
156 Properties props = connectionDetails.getAdditionalProperties();
157 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")){
158 throw new APPCException("Invalid properties!");
161 return this.getConfiguration(connectionDetails.getHost(),props.getProperty("module.name"),props.getProperty("node.name"));
165 public void disconnect() throws APPCException {
166 if(connectionDetails == null){
167 throw new APPCException("Invalid connection details - null value");
169 this.disconnect(connectionDetails.getHost());
173 private String getModuleConfigurePath(String deviceMountPointName, String moduleName, String nodeName){
176 String deviceSpecificPath = deviceMountPointName + "/yang-ext:mount/" + moduleName + ":" + nodeName;
178 return Constants.CONFIGURE_PATH + deviceSpecificPath;
181 private String getConnectPath(){
183 return Constants.CONNECT_PATH;
186 private String getCheckConnectivityPath(String deviceMountPointName) {
187 return Constants.CHECK_CONNECTION_PATH + deviceMountPointName;
190 private String getDisconnectPath(String deviceMountPointName) {
191 return Constants.DISCONNECT_PATH + deviceMountPointName;
194 private String getPayload() {
196 " \"config:module\":\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" +
207 " \"type\":\"netty:netty-event-executor\",\n" +
208 " \"name\":\"global-event-executor\"\n" +
210 " \"odl-sal-netconf-connector-cfg:binding-registry\":\n" +
212 " \"type\":\"opendaylight-md-sal-binding:binding-broker-osgi-registry\",\n" +
213 " \"name\":\"binding-osgi-broker\"\n" +
215 " \"odl-sal-netconf-connector-cfg:dom-registry\":\n" +
217 " \"type\":\"opendaylight-md-sal-dom:dom-broker-osgi-registry\",\n" +
218 " \"name\":\"dom-broker\"\n" +
220 " \"odl-sal-netconf-connector-cfg:client-dispatcher\":\n" +
222 " \"type\":\"odl-netconf-cfg:netconf-client-dispatcher\",\n" +
223 " \"name\":\"global-netconf-dispatcher\"\n" +
225 " \"odl-sal-netconf-connector-cfg:processing-executor\":\n" +
227 " \"type\":\"threadpool:threadpool\",\n" +
228 " \"name\":\"global-netconf-processing-executor\"\n" +