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.ccsdk.sli.adaptors.netconf.odlconnector;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import java.util.Properties;
31 import org.apache.http.HttpStatus;
32 import org.onap.ccsdk.sli.adaptors.netconf.HttpClient;
33 import org.onap.ccsdk.sli.adaptors.netconf.NetconfAdaptorConstants;
34 import org.onap.ccsdk.sli.adaptors.netconf.NetconfClient;
35 import org.onap.ccsdk.sli.adaptors.netconf.NetconfClientRestconf;
36 import org.onap.ccsdk.sli.adaptors.netconf.NetconfConnectionDetails;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
39 public class NetconfClientRestconfImpl implements NetconfClient, NetconfClientRestconf {
41 private final EELFLogger logger = EELFManager.getInstance().getLogger(NetconfClientRestconfImpl.class);
43 private NetconfConnectionDetails connectionDetails;
44 private final String appFormat = "application/json";
46 public NetconfClientRestconfImpl(){
50 //restconf client impl
52 @SuppressWarnings("deprecation")
54 public void configure(String configuration, String deviceMountPointName, String moduleName, String nodeName) throws SvcLogicException {
56 logger.info("Configuring device " + deviceMountPointName + " with configuration " + configuration);
58 int httpCode = HttpClient.putMethod(NetconfAdaptorConstants.PROTOCOL, NetconfAdaptorConstants.CONTROLLER_IP, NetconfAdaptorConstants.CONTROLLER_PORT,
59 getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), configuration, appFormat);
61 if (httpCode != HttpStatus.SC_OK) {
62 logger.error("Configuration request failed. throwing Exception !");
63 throw new SvcLogicException("Error configuring node :" + nodeName + ", of Module :" + moduleName +
64 ", in device :" + deviceMountPointName);
69 public void connect(String deviceMountPointName, String payload) throws SvcLogicException{
71 logger.info("Connecting device " + deviceMountPointName);
73 int httpCode = HttpClient.postMethod(NetconfAdaptorConstants.PROTOCOL, NetconfAdaptorConstants.CONTROLLER_IP, NetconfAdaptorConstants.CONTROLLER_PORT,
74 getConnectPath(), payload, appFormat);
76 if(httpCode != HttpStatus.SC_NO_CONTENT){
77 logger.error("Connect request failed with code " + httpCode + ". throwing Exception !");
78 throw new SvcLogicException("Error connecting device :" + deviceMountPointName);
83 public boolean checkConnection(String deviceMountPointName) throws SvcLogicException {
84 logger.info("Checking device " + deviceMountPointName + " connectivity");
86 String result = HttpClient.getMethod(NetconfAdaptorConstants.PROTOCOL, NetconfAdaptorConstants.CONTROLLER_IP,
87 NetconfAdaptorConstants.CONTROLLER_PORT, getCheckConnectivityPath(deviceMountPointName), appFormat);
89 return result != null;
93 public void disconnect(String deviceMountPointName) throws SvcLogicException {
94 logger.info("Disconnecting " + deviceMountPointName);
96 int httpCode = HttpClient.deleteMethod(NetconfAdaptorConstants.PROTOCOL, NetconfAdaptorConstants.CONTROLLER_IP, NetconfAdaptorConstants.CONTROLLER_PORT,
97 getDisconnectPath(deviceMountPointName), appFormat);
99 if(httpCode != HttpStatus.SC_OK){
100 logger.error("Disconnection of device " + deviceMountPointName + " failed!");
101 throw new SvcLogicException("Disconnection of device " + deviceMountPointName + " failed!");
106 public String getConfiguration(String deviceMountPointName, String moduleName, String nodeName) throws SvcLogicException{
107 logger.info("Getting configuration of device " + deviceMountPointName);
109 String result = HttpClient.getMethod(NetconfAdaptorConstants.PROTOCOL, NetconfAdaptorConstants.CONTROLLER_IP, NetconfAdaptorConstants.CONTROLLER_PORT,
110 getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), appFormat);
112 if (result == null) {
113 logger.error("Configuration request failed. throwing Exception !");
114 throw new SvcLogicException("Error getting configuration of node :" + nodeName + ", of Module :" + moduleName +
115 ", in device :" + deviceMountPointName);
121 //netconf client impl
124 public void connect(NetconfConnectionDetails connectionDetails) throws SvcLogicException {
125 if(connectionDetails == null){
126 throw new SvcLogicException("Invalid connection details - null value");
128 this.connectionDetails = connectionDetails;
129 this.connect(connectionDetails.getHost(), getPayload());
133 public String exchangeMessage(String message) throws SvcLogicException {
139 public void configure(String configuration) throws SvcLogicException {
140 if(connectionDetails == null){
141 throw new SvcLogicException("Invalid connection details - null value");
144 Properties props = connectionDetails.getAdditionalProperties();
145 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")) {
146 throw new SvcLogicException("Invalid properties!");
149 String moduleName = props.getProperty("module.name");
150 String nodeName = props.getProperty("node.name");
151 String deviceMountPointName = connectionDetails.getHost();
153 int httpCode = HttpClient.putMethod(NetconfAdaptorConstants.PROTOCOL, NetconfAdaptorConstants.CONTROLLER_IP, NetconfAdaptorConstants.CONTROLLER_PORT,
154 getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), configuration, "application/xml");
156 if (httpCode != HttpStatus.SC_OK) {
157 logger.error("Configuration request failed. throwing Exception !");
158 throw new SvcLogicException("Error configuring node :" + nodeName + ", of Module :" + moduleName +
159 ", in device :" + deviceMountPointName);
164 public String getConfiguration() throws SvcLogicException {
165 if(connectionDetails == null){
166 throw new SvcLogicException("Invalid connection details - null value");
169 Properties props = connectionDetails.getAdditionalProperties();
170 if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")) {
171 throw new SvcLogicException("Invalid properties!");
174 return this.getConfiguration(connectionDetails.getHost(), props.getProperty("module.name"),
175 props.getProperty("node.name"));
179 public void disconnect() throws SvcLogicException {
180 if(connectionDetails == null){
181 throw new SvcLogicException("Invalid connection details - null value");
183 this.disconnect(connectionDetails.getHost());
187 private String getModuleConfigurePath(String deviceMountPointName, String moduleName, String nodeName){
189 String deviceSpecificPath = deviceMountPointName + "/yang-ext:mount/" + moduleName + ":" + nodeName;
191 return NetconfAdaptorConstants.CONFIGURE_PATH + deviceSpecificPath;
194 private String getConnectPath(){
196 return NetconfAdaptorConstants.CONNECT_PATH;
199 private String getCheckConnectivityPath(String deviceMountPointName) {
200 return NetconfAdaptorConstants.CHECK_CONNECTION_PATH + deviceMountPointName;
203 private String getDisconnectPath(String deviceMountPointName) {
204 return NetconfAdaptorConstants.DISCONNECT_PATH + deviceMountPointName;
207 private String getPayload() {
209 " \"config:module\":\n" +
211 " \"type\":\"odl-sal-netconf-connector-cfg:sal-netconf-connector\",\n" +
212 " \"netconf-northbound-ssh\\odl-sal-netconf-connector-cfg:name\":"+connectionDetails.getHost()+",\n" +
213 " \"odl-sal-netconf-connector-cfg:address\":"+connectionDetails.getHost()+",\n" +
214 " \"odl-sal-netconf-connector-cfg:port\":"+connectionDetails.getPort()+",\n" +
215 " \"odl-sal-netconf-connector-cfg:username\":"+connectionDetails.getUsername()+",\n" +
216 " \"odl-sal-netconf-connector-cfg:password\":"+connectionDetails.getPassword()+",\n" +
217 " \"tcp-only\":\"false\",\n" +
218 " \"odl-sal-netconf-connector-cfg:event-executor\":\n" +
220 " \"type\":\"netty:netty-event-executor\",\n" +
221 " \"name\":\"global-event-executor\"\n" +
223 " \"odl-sal-netconf-connector-cfg:binding-registry\":\n" +
225 " \"type\":\"opendaylight-md-sal-binding:binding-broker-osgi-registry\",\n" +
226 " \"name\":\"binding-osgi-broker\"\n" +
228 " \"odl-sal-netconf-connector-cfg:dom-registry\":\n" +
230 " \"type\":\"opendaylight-md-sal-dom:dom-broker-osgi-registry\",\n" +
231 " \"name\":\"dom-broker\"\n" +
233 " \"odl-sal-netconf-connector-cfg:client-dispatcher\":\n" +
235 " \"type\":\"odl-netconf-cfg:netconf-client-dispatcher\",\n" +
236 " \"name\":\"global-netconf-dispatcher\"\n" +
238 " \"odl-sal-netconf-connector-cfg:processing-executor\":\n" +
240 " \"type\":\"threadpool:threadpool\",\n" +
241 " \"name\":\"global-netconf-processing-executor\"\n" +