2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.adapter.netconf.odlconnector;
27 import org.apache.http.HttpStatus;
28 import org.openecomp.appc.adapter.netconf.NetconfClient;
29 import org.openecomp.appc.adapter.netconf.NetconfClientRestconf;
30 import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
31 import org.openecomp.appc.adapter.netconf.util.Constants;
32 import org.openecomp.appc.exceptions.APPCException;
33 import org.openecomp.appc.util.httpClient;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
37 import java.util.Properties;
39 public class NetconfClientRestconfImpl implements NetconfClient, NetconfClientRestconf {
41 private EELFLogger logger = EELFManager.getInstance().getLogger(NetconfClientRestconfImpl.class);
43 private NetconfConnectionDetails connectionDetails;
46 public NetconfClientRestconfImpl(){
49 //restconf client impl
51 @SuppressWarnings("deprecation")
53 public void configure(String configuration, String deviceMountPointName, String moduleName, String nodeName) throws APPCException {
55 logger.info("Configuring device "+deviceMountPointName+" with configuration "+configuration);
57 int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),configuration,"application/json");
59 if (httpCode != HttpStatus.SC_OK) {
60 logger.error("Configuration request failed. throwing Exception !");
61 throw new APPCException("Error configuring node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
66 public void connect(String deviceMountPointName, String payload) throws APPCException{
68 logger.info("Connecting device "+deviceMountPointName);
70 int httpCode = httpClient.postMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getConnectPath(),payload,"application/json");
72 if(httpCode != HttpStatus.SC_NO_CONTENT){
73 logger.error("Connect request failed with code "+httpCode+". throwing Exception !");
74 throw new APPCException("Error connecting device :" + deviceMountPointName);
79 public boolean checkConnection(String deviceMountPointName) throws APPCException {
80 logger.info("Checking device "+deviceMountPointName+" connectivity");
82 String result = httpClient.getMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getCheckConnectivityPath(deviceMountPointName),"application/json");
84 return result != null;
88 public void disconnect(String deviceMountPointName) throws APPCException {
89 logger.info("Disconnecting "+deviceMountPointName);
91 int httpCode = httpClient.deleteMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getDisconnectPath(deviceMountPointName),"application/json");
93 if(httpCode != HttpStatus.SC_OK){
94 logger.error("Disconnection of device "+deviceMountPointName+" failed!");
95 throw new APPCException("Disconnection of device "+deviceMountPointName+" failed!");
100 public String getConfiguration(String deviceMountPointName, String moduleName, String nodeName) throws APPCException{
101 logger.info("Getting configuration of device "+deviceMountPointName);
103 String result = httpClient.getMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),"application/json");
105 if (result == null) {
106 logger.error("Configuration request failed. throwing Exception !");
107 throw new APPCException("Error getting configuration of node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
113 //netconf client impl
116 public void connect(NetconfConnectionDetails connectionDetails) throws APPCException {
117 if(connectionDetails == null){
118 throw new APPCException("Invalid connection details - null value");
120 this.connectionDetails = connectionDetails;
121 this.connect(connectionDetails.getHost(),getPayload());
125 public String exchangeMessage(String message) throws APPCException {
131 public void configure(String configuration) throws APPCException {
132 if(connectionDetails == null){
133 throw new APPCException("Invalid connection details - null value");
136 Properties props = connectionDetails.getAdditionalProperties();
137 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")){
138 throw new APPCException("Invalid properties!");
141 String moduleName = props.getProperty("module.name");
142 String nodeName = props.getProperty("node.name");
143 String deviceMountPointName = connectionDetails.getHost();
145 int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,getModuleConfigurePath(deviceMountPointName, moduleName, nodeName),configuration,"application/xml");
147 if (httpCode != HttpStatus.SC_OK) {
148 logger.error("Configuration request failed. throwing Exception !");
149 throw new APPCException("Error configuring node :"+nodeName + ", of Module :" + moduleName + ", in device :" + deviceMountPointName);
154 public String getConfiguration() throws APPCException {
155 if(connectionDetails == null){
156 throw new APPCException("Invalid connection details - null value");
159 Properties props = connectionDetails.getAdditionalProperties();
160 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")){
161 throw new APPCException("Invalid properties!");
164 return this.getConfiguration(connectionDetails.getHost(),props.getProperty("module.name"),props.getProperty("node.name"));
168 public void disconnect() throws APPCException {
169 if(connectionDetails == null){
170 throw new APPCException("Invalid connection details - null value");
172 this.disconnect(connectionDetails.getHost());
176 private String getModuleConfigurePath(String deviceMountPointName, String moduleName, String nodeName){
179 String deviceSpecificPath = deviceMountPointName + "/yang-ext:mount/" + moduleName + ":" + nodeName;
181 return Constants.CONFIGURE_PATH + deviceSpecificPath;
184 private String getConnectPath(){
186 return Constants.CONNECT_PATH;
189 private String getCheckConnectivityPath(String deviceMountPointName) {
190 return Constants.CHECK_CONNECTION_PATH + deviceMountPointName;
193 private String getDisconnectPath(String deviceMountPointName) {
194 return Constants.DISCONNECT_PATH + deviceMountPointName;
197 private String getPayload() {
199 " \"config:module\":\n" +
201 " \"type\":\"odl-sal-netconf-connector-cfg:sal-netconf-connector\",\n" +
202 " \"netconf-northbound-ssh\\odl-sal-netconf-connector-cfg:name\":"+connectionDetails.getHost()+",\n" +
203 " \"odl-sal-netconf-connector-cfg:address\":"+connectionDetails.getHost()+",\n" +
204 " \"odl-sal-netconf-connector-cfg:port\":"+connectionDetails.getPort()+",\n" +
205 " \"odl-sal-netconf-connector-cfg:username\":"+connectionDetails.getUsername()+",\n" +
206 " \"odl-sal-netconf-connector-cfg:password\":"+connectionDetails.getPassword()+",\n" +
207 " \"tcp-only\":\"false\",\n" +
208 " \"odl-sal-netconf-connector-cfg:event-executor\":\n" +
210 " \"type\":\"netty:netty-event-executor\",\n" +
211 " \"name\":\"global-event-executor\"\n" +
213 " \"odl-sal-netconf-connector-cfg:binding-registry\":\n" +
215 " \"type\":\"opendaylight-md-sal-binding:binding-broker-osgi-registry\",\n" +
216 " \"name\":\"binding-osgi-broker\"\n" +
218 " \"odl-sal-netconf-connector-cfg:dom-registry\":\n" +
220 " \"type\":\"opendaylight-md-sal-dom:dom-broker-osgi-registry\",\n" +
221 " \"name\":\"dom-broker\"\n" +
223 " \"odl-sal-netconf-connector-cfg:client-dispatcher\":\n" +
225 " \"type\":\"odl-netconf-cfg:netconf-client-dispatcher\",\n" +
226 " \"name\":\"global-netconf-dispatcher\"\n" +
228 " \"odl-sal-netconf-connector-cfg:processing-executor\":\n" +
230 " \"type\":\"threadpool:threadpool\",\n" +
231 " \"name\":\"global-netconf-processing-executor\"\n" +