Andrea Campanella
Committed by Gerrit Code Review

ONOS-3791 NETCONF session factory and exception on device connection

Change-Id: I7c6651a4f76537056a2dc8f94d54818b5b238b9a
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.netconf;
/**
* Abstract interface for the creation of a NETCONF session.
*/
public interface NetconfSessionFactory {
NetconfSession createNetconfSession(NetconfDeviceInfo netconfDeviceInfo)
throws NetconfException;
}
......@@ -46,7 +46,7 @@ public class NetconfControllerImpl implements NetconfController {
public static final Logger log = LoggerFactory
.getLogger(NetconfControllerImpl.class);
public Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>();
......@@ -130,6 +130,4 @@ public class NetconfControllerImpl implements NetconfController {
public Map<DeviceId, NetconfDevice> getDevicesMap() {
return netconfDeviceMap;
}
}
......
......@@ -20,6 +20,7 @@ import org.onosproject.netconf.NetconfDevice;
import org.onosproject.netconf.NetconfDeviceInfo;
import org.onosproject.netconf.NetconfException;
import org.onosproject.netconf.NetconfSession;
import org.onosproject.netconf.NetconfSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -35,12 +36,15 @@ public class NetconfDeviceImpl implements NetconfDevice {
private NetconfDeviceInfo netconfDeviceInfo;
private boolean deviceState = false;
protected NetconfSessionFactory sessionFactory = new SshNetconfSessionFactory();
private NetconfSession netconfSession;
public NetconfDeviceImpl(NetconfDeviceInfo deviceInfo) throws NetconfException {
public NetconfDeviceImpl(NetconfDeviceInfo deviceInfo)
throws NetconfException {
netconfDeviceInfo = deviceInfo;
try {
netconfSession = new NetconfSessionImpl(netconfDeviceInfo);
netconfSession = sessionFactory.createNetconfSession(deviceInfo);
} catch (IOException e) {
throw new NetconfException("Cannot create connection and session for device " +
deviceInfo, e);
......@@ -72,4 +76,13 @@ public class NetconfDeviceImpl implements NetconfDevice {
public NetconfDeviceInfo getDeviceInfo() {
return netconfDeviceInfo;
}
public class SshNetconfSessionFactory implements NetconfSessionFactory {
@Override
public NetconfSession createNetconfSession(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
return new NetconfSessionImpl(netconfDeviceInfo);
}
}
}
......
......@@ -401,6 +401,4 @@ public class NetconfSessionImpl implements NetconfSession {
completedReply.complete(event.getMessagePayload());
}
}
}
......
......@@ -46,6 +46,7 @@ import org.onosproject.netconf.NetconfController;
import org.onosproject.netconf.NetconfDevice;
import org.onosproject.netconf.NetconfDeviceInfo;
import org.onosproject.netconf.NetconfDeviceListener;
import org.onosproject.netconf.NetconfException;
import org.slf4j.Logger;
import java.io.IOException;
......@@ -190,10 +191,11 @@ public class NetconfDeviceProvider extends AbstractProvider
addr.ip(),
addr.port()));
} catch (IOException e) {
log.info("Can't connect to NETCONF " +
"device on {}:{}",
addr.ip(),
addr.port());
throw new RuntimeException(
new NetconfException(
"Can't connect to NETCONF " +
"device on " + addr.ip() +
":" + addr.port(), e));
}
}
);
......