Andrea Campanella
Committed by Gerrit Code Review

[ONOS-4045]Adding mastership service to NetconfProvider

Change-Id: Id39cbef54a079ab6e080a9d3f60770c4bea90b3f
...@@ -18,6 +18,7 @@ package org.onosproject.drivers.netconf; ...@@ -18,6 +18,7 @@ package org.onosproject.drivers.netconf;
18 18
19 import com.google.common.base.Preconditions; 19 import com.google.common.base.Preconditions;
20 import org.onosproject.drivers.utilities.XmlConfigParser; 20 import org.onosproject.drivers.utilities.XmlConfigParser;
21 +import org.onosproject.mastership.MastershipService;
21 import org.onosproject.net.DeviceId; 22 import org.onosproject.net.DeviceId;
22 import org.onosproject.net.behaviour.ControllerConfig; 23 import org.onosproject.net.behaviour.ControllerConfig;
23 import org.onosproject.net.behaviour.ControllerInfo; 24 import org.onosproject.net.behaviour.ControllerInfo;
...@@ -44,21 +45,29 @@ public class NetconfControllerConfig extends AbstractHandlerBehaviour ...@@ -44,21 +45,29 @@ public class NetconfControllerConfig extends AbstractHandlerBehaviour
44 45
45 private final Logger log = getLogger(NetconfControllerConfig.class); 46 private final Logger log = getLogger(NetconfControllerConfig.class);
46 47
48 +
47 @Override 49 @Override
48 public List<ControllerInfo> getControllers() { 50 public List<ControllerInfo> getControllers() {
49 DriverHandler handler = handler(); 51 DriverHandler handler = handler();
50 NetconfController controller = handler.get(NetconfController.class); 52 NetconfController controller = handler.get(NetconfController.class);
51 - DeviceId ofDeviceId = handler.data().deviceId(); 53 + MastershipService mastershipService = handler.get(MastershipService.class);
54 + DeviceId deviceId = handler.data().deviceId();
52 Preconditions.checkNotNull(controller, "Netconf controller is null"); 55 Preconditions.checkNotNull(controller, "Netconf controller is null");
53 List<ControllerInfo> controllers = new ArrayList<>(); 56 List<ControllerInfo> controllers = new ArrayList<>();
54 - try { 57 + if (mastershipService.isLocalMaster(deviceId)) {
55 - String reply = controller.getDevicesMap().get(ofDeviceId).getSession(). 58 + try {
56 - getConfig("running"); 59 + String reply = controller.getNetconfDevice(deviceId).getSession().
57 - log.debug("Reply XML {}", reply); 60 + getConfig("running");
58 - controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser. 61 + log.debug("Reply XML {}", reply);
59 - loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))))); 62 + controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser.
60 - } catch (IOException e) { 63 + loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
61 - log.error("Cannot communicate with device {} ", ofDeviceId); 64 + } catch (IOException e) {
65 + log.error("Cannot communicate with device {} ", deviceId, e);
66 + }
67 + } else {
68 + log.warn("I'm not master for {} please use master, {} to execute command",
69 + deviceId,
70 + mastershipService.getMasterFor(deviceId));
62 } 71 }
63 return controllers; 72 return controllers;
64 } 73 }
...@@ -69,30 +78,36 @@ public class NetconfControllerConfig extends AbstractHandlerBehaviour ...@@ -69,30 +78,36 @@ public class NetconfControllerConfig extends AbstractHandlerBehaviour
69 NetconfController controller = handler.get(NetconfController.class); 78 NetconfController controller = handler.get(NetconfController.class);
70 DeviceId deviceId = handler.data().deviceId(); 79 DeviceId deviceId = handler.data().deviceId();
71 Preconditions.checkNotNull(controller, "Netconf controller is null"); 80 Preconditions.checkNotNull(controller, "Netconf controller is null");
72 - try { 81 + MastershipService mastershipService = handler.get(MastershipService.class);
73 - NetconfDevice device = controller.getNetconfDevice(deviceId); 82 + if (mastershipService.isLocalMaster(deviceId)) {
74 - String config = null;
75 -
76 try { 83 try {
77 - String reply = device.getSession().getConfig("running"); 84 + NetconfDevice device = controller.getNetconfDevice(deviceId);
78 - log.info("reply XML {}", reply); 85 + String config = null;
79 - config = XmlConfigParser.createControllersConfig( 86 +
80 - XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")), 87 + try {
81 - XmlConfigParser.loadXml( 88 + String reply = device.getSession().getConfig("running");
82 - new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))), 89 + log.info("reply XML {}", reply);
83 - "running", "merge", "create", controllers 90 + config = XmlConfigParser.createControllersConfig(
84 - ); 91 + XmlConfigParser.loadXml(getClass().getResourceAsStream("controllers.xml")),
92 + XmlConfigParser.loadXml(
93 + new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))),
94 + "running", "merge", "create", controllers
95 + );
96 + } catch (IOException e) {
97 + log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
98 + }
99 + device.getSession().editConfig(config.substring(config.indexOf("-->") + 3));
100 + } catch (NullPointerException e) {
101 + log.warn("No NETCONF device with requested parameters " + e);
102 + throw new NullPointerException("No NETCONF device with requested parameters " + e);
85 } catch (IOException e) { 103 } catch (IOException e) {
86 log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage()); 104 log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
87 } 105 }
88 - device.getSession().editConfig(config.substring(config.indexOf("-->") + 3)); 106 + } else {
89 - } catch (NullPointerException e) { 107 + log.warn("I'm not master for {} please use master, {} to execute command",
90 - log.warn("No NETCONF device with requested parameters " + e); 108 + deviceId,
91 - throw new NullPointerException("No NETCONF device with requested parameters " + e); 109 + mastershipService.getMasterFor(deviceId));
92 - } catch (IOException e) {
93 - log.error("Cannot comunicate to device {} , exception {}", deviceId, e.getMessage());
94 } 110 }
95 -
96 } 111 }
97 112
98 //TODO maybe put method getNetconfClientService like in ovsdb if we need it 113 //TODO maybe put method getNetconfClientService like in ovsdb if we need it
......
...@@ -46,31 +46,31 @@ public interface NetconfController { ...@@ -46,31 +46,31 @@ public interface NetconfController {
46 * Tries to connect to a specific NETCONF device, if the connection is succesful 46 * Tries to connect to a specific NETCONF device, if the connection is succesful
47 * it creates and adds the device to the ONOS core as a NetconfDevice. 47 * it creates and adds the device to the ONOS core as a NetconfDevice.
48 * 48 *
49 - * @param deviceInfo info about the device to add 49 + * @param deviceId deviceId of the device to connect
50 * @return NetconfDevice Netconf device 50 * @return NetconfDevice Netconf device
51 * @throws NetconfException when device is not available 51 * @throws NetconfException when device is not available
52 */ 52 */
53 - NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws NetconfException; 53 + NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException;
54 54
55 /** 55 /**
56 * Disconnects a Netconf device and removes it from the core. 56 * Disconnects a Netconf device and removes it from the core.
57 * 57 *
58 - * @param deviceInfo info about the device to remove 58 + * @param deviceId id of the device to remove
59 + * @param remove true if device is to be removed from core
59 */ 60 */
60 - void disconnectDevice(NetconfDeviceInfo deviceInfo); 61 + void disconnectDevice(DeviceId deviceId, boolean remove);
61 62
62 /** 63 /**
63 * Removes a Netconf device from the core. 64 * Removes a Netconf device from the core.
64 * 65 *
65 - * @param deviceInfo info about the device to remove 66 + * @param deviceId id of the device to remove
66 */ 67 */
67 - void removeDevice(NetconfDeviceInfo deviceInfo); 68 + void removeDevice(DeviceId deviceId);
68 69
69 /** 70 /**
70 * Gets all the nodes information. 71 * Gets all the nodes information.
71 * 72 *
72 * @return map of devices 73 * @return map of devices
73 - *
74 */ 74 */
75 Map<DeviceId, NetconfDevice> getDevicesMap(); 75 Map<DeviceId, NetconfDevice> getDevicesMap();
76 76
...@@ -97,5 +97,4 @@ public interface NetconfController { ...@@ -97,5 +97,4 @@ public interface NetconfController {
97 * @return NetconfDevice Netconf device 97 * @return NetconfDevice Netconf device
98 */ 98 */
99 NetconfDevice getNetconfDevice(IpAddress ip, int port); 99 NetconfDevice getNetconfDevice(IpAddress ip, int port);
100 -
101 } 100 }
......
...@@ -16,22 +16,25 @@ ...@@ -16,22 +16,25 @@
16 16
17 package org.onosproject.netconf; 17 package org.onosproject.netconf;
18 18
19 +import org.onosproject.net.DeviceId;
20 +
19 /** 21 /**
20 * Allows for providers interested in node events to be notified. 22 * Allows for providers interested in node events to be notified.
21 */ 23 */
22 public interface NetconfDeviceListener { 24 public interface NetconfDeviceListener {
23 25
24 /** 26 /**
25 - * Notifies that the node was added. 27 + * Notifies that the device was added.
26 * 28 *
27 - * @param nodeId the node where the event occurred 29 + * @param deviceId the device that was added
28 */ 30 */
29 - void deviceAdded(NetconfDeviceInfo nodeId); 31 + void deviceAdded(DeviceId deviceId);
30 32
31 /** 33 /**
32 - * Notifies that the node was removed. 34 + * Notifies that the device was removed.
33 * 35 *
34 - * @param nodeId the node where the event occurred 36 + * @param deviceId the device that was removed
35 */ 37 */
36 - void deviceRemoved(NetconfDeviceInfo nodeId); 38 +
39 + void deviceRemoved(DeviceId deviceId);
37 } 40 }
......
...@@ -35,7 +35,7 @@ public class DefaultNetconfDevice implements NetconfDevice { ...@@ -35,7 +35,7 @@ public class DefaultNetconfDevice implements NetconfDevice {
35 .getLogger(DefaultNetconfDevice.class); 35 .getLogger(DefaultNetconfDevice.class);
36 36
37 private NetconfDeviceInfo netconfDeviceInfo; 37 private NetconfDeviceInfo netconfDeviceInfo;
38 - private boolean deviceState = false; 38 + private boolean deviceState = true;
39 protected NetconfSessionFactory sessionFactory = new SshNetconfSessionFactory(); 39 protected NetconfSessionFactory sessionFactory = new SshNetconfSessionFactory();
40 private NetconfSession netconfSession; 40 private NetconfSession netconfSession;
41 41
...@@ -53,10 +53,10 @@ public class DefaultNetconfDevice implements NetconfDevice { ...@@ -53,10 +53,10 @@ public class DefaultNetconfDevice implements NetconfDevice {
53 try { 53 try {
54 netconfSession = sessionFactory.createNetconfSession(deviceInfo); 54 netconfSession = sessionFactory.createNetconfSession(deviceInfo);
55 } catch (IOException e) { 55 } catch (IOException e) {
56 + deviceState = false;
56 throw new NetconfException("Cannot create connection and session for device " + 57 throw new NetconfException("Cannot create connection and session for device " +
57 deviceInfo, e); 58 deviceInfo, e);
58 } 59 }
59 - deviceState = true;
60 } 60 }
61 61
62 @Override 62 @Override
......
...@@ -19,9 +19,16 @@ package org.onosproject.netconf.ctl; ...@@ -19,9 +19,16 @@ package org.onosproject.netconf.ctl;
19 import org.apache.felix.scr.annotations.Activate; 19 import org.apache.felix.scr.annotations.Activate;
20 import org.apache.felix.scr.annotations.Component; 20 import org.apache.felix.scr.annotations.Component;
21 import org.apache.felix.scr.annotations.Deactivate; 21 import org.apache.felix.scr.annotations.Deactivate;
22 +import org.apache.felix.scr.annotations.Reference;
23 +import org.apache.felix.scr.annotations.ReferenceCardinality;
22 import org.apache.felix.scr.annotations.Service; 24 import org.apache.felix.scr.annotations.Service;
23 import org.onlab.packet.IpAddress; 25 import org.onlab.packet.IpAddress;
26 +import org.onosproject.net.Device;
24 import org.onosproject.net.DeviceId; 27 import org.onosproject.net.DeviceId;
28 +import org.onosproject.net.device.DeviceService;
29 +import org.onosproject.net.key.DeviceKeyId;
30 +import org.onosproject.net.key.DeviceKeyService;
31 +import org.onosproject.net.key.UsernamePassword;
25 import org.onosproject.netconf.NetconfController; 32 import org.onosproject.netconf.NetconfController;
26 import org.onosproject.netconf.NetconfDevice; 33 import org.onosproject.netconf.NetconfDevice;
27 import org.onosproject.netconf.NetconfDeviceFactory; 34 import org.onosproject.netconf.NetconfDeviceFactory;
...@@ -34,6 +41,7 @@ import org.osgi.service.component.ComponentContext; ...@@ -34,6 +41,7 @@ import org.osgi.service.component.ComponentContext;
34 import org.slf4j.Logger; 41 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory; 42 import org.slf4j.LoggerFactory;
36 43
44 +import java.util.Arrays;
37 import java.util.Map; 45 import java.util.Map;
38 import java.util.Set; 46 import java.util.Set;
39 import java.util.concurrent.ConcurrentHashMap; 47 import java.util.concurrent.ConcurrentHashMap;
...@@ -45,6 +53,11 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -45,6 +53,11 @@ import java.util.concurrent.CopyOnWriteArraySet;
45 @Component(immediate = true) 53 @Component(immediate = true)
46 @Service 54 @Service
47 public class NetconfControllerImpl implements NetconfController { 55 public class NetconfControllerImpl implements NetconfController {
56 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 + protected DeviceService deviceService;
58 +
59 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 + protected DeviceKeyService deviceKeyService;
48 61
49 public static final Logger log = LoggerFactory 62 public static final Logger log = LoggerFactory
50 .getLogger(NetconfControllerImpl.class); 63 .getLogger(NetconfControllerImpl.class);
...@@ -95,55 +108,93 @@ public class NetconfControllerImpl implements NetconfController { ...@@ -95,55 +108,93 @@ public class NetconfControllerImpl implements NetconfController {
95 } 108 }
96 109
97 @Override 110 @Override
98 - public NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { 111 + public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
99 - if (netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) { 112 + if (netconfDeviceMap.containsKey(deviceId)) {
100 - log.info("Device {} is already present", deviceInfo); 113 + log.debug("Device {} is already present", deviceId);
101 - return netconfDeviceMap.get(deviceInfo.getDeviceId()); 114 + return netconfDeviceMap.get(deviceId);
102 } else { 115 } else {
103 - log.info("Creating NETCONF device {}", deviceInfo); 116 + log.debug("Creating NETCONF device {}", deviceId);
104 - NetconfDevice device = createDevice(deviceInfo); 117 + Device device = deviceService.getDevice(deviceId);
105 - device.getSession().addDeviceOutputListener(downListener); 118 + String ip;
106 - return device; 119 + int port;
120 + if (device != null) {
121 + ip = device.annotations().value("ipaddress");
122 + port = Integer.parseInt(device.annotations().value("port"));
123 + } else {
124 + String[] info = deviceId.toString().split(":");
125 + if (info.length == 3) {
126 + ip = info[1];
127 + port = Integer.parseInt(info[2]);
128 + } else {
129 + ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
130 + && !el.equals(info[info.length - 1]))
131 + .reduce((t, u) -> t + ":" + u)
132 + .get();
133 + log.debug("ip v6 {}", ip);
134 + port = Integer.parseInt(info[info.length - 1]);
135 + }
136 + }
137 + try {
138 + UsernamePassword deviceKey = deviceKeyService.getDeviceKey(
139 + DeviceKeyId.deviceKeyId(deviceId.toString())).asUsernamePassword();
140 +
141 + NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(deviceKey.username(),
142 + deviceKey.password(),
143 + IpAddress.valueOf(ip),
144 + port);
145 + NetconfDevice netconfDevicedevice = createDevice(deviceInfo);
146 +
147 + netconfDevicedevice.getSession().addDeviceOutputListener(downListener);
148 + return netconfDevicedevice;
149 + } catch (NullPointerException e) {
150 + throw new NetconfException("No Device Key for device " + deviceId, e);
151 + }
107 } 152 }
108 } 153 }
109 154
110 @Override 155 @Override
111 - public void disconnectDevice(NetconfDeviceInfo deviceInfo) { 156 + public void disconnectDevice(DeviceId deviceId, boolean remove) {
112 - if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) { 157 + if (!netconfDeviceMap.containsKey(deviceId)) {
113 - log.warn("Device {} is not present", deviceInfo); 158 + log.warn("Device {} is not present", deviceId);
114 } else { 159 } else {
115 - stopDevice(deviceInfo); 160 + stopDevice(deviceId, remove);
161 + }
162 + }
163 +
164 + private void stopDevice(DeviceId deviceId, boolean remove) {
165 + netconfDeviceMap.get(deviceId).disconnect();
166 + netconfDeviceMap.remove(deviceId);
167 + if (remove) {
168 + for (NetconfDeviceListener l : netconfDeviceListeners) {
169 + l.deviceRemoved(deviceId);
170 + }
116 } 171 }
117 } 172 }
118 173
119 @Override 174 @Override
120 - public void removeDevice(NetconfDeviceInfo deviceInfo) { 175 + public void removeDevice(DeviceId deviceId) {
121 - if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) { 176 + if (!netconfDeviceMap.containsKey(deviceId)) {
122 - log.warn("Device {} is not present", deviceInfo); 177 + log.warn("Device {} is not present", deviceId);
178 + for (NetconfDeviceListener l : netconfDeviceListeners) {
179 + l.deviceRemoved(deviceId);
180 + }
123 } else { 181 } else {
124 - netconfDeviceMap.remove(deviceInfo.getDeviceId()); 182 + netconfDeviceMap.remove(deviceId);
125 for (NetconfDeviceListener l : netconfDeviceListeners) { 183 for (NetconfDeviceListener l : netconfDeviceListeners) {
126 - l.deviceRemoved(deviceInfo); 184 + l.deviceRemoved(deviceId);
127 } 185 }
128 } 186 }
129 } 187 }
130 188
131 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { 189 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
132 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo); 190 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
191 + netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
133 for (NetconfDeviceListener l : netconfDeviceListeners) { 192 for (NetconfDeviceListener l : netconfDeviceListeners) {
134 - l.deviceAdded(deviceInfo); 193 + l.deviceAdded(deviceInfo.getDeviceId());
135 } 194 }
136 - netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
137 return netconfDevice; 195 return netconfDevice;
138 } 196 }
139 197
140 - private void stopDevice(NetconfDeviceInfo deviceInfo) {
141 - netconfDeviceMap.get(deviceInfo.getDeviceId()).disconnect();
142 - netconfDeviceMap.remove(deviceInfo.getDeviceId());
143 - for (NetconfDeviceListener l : netconfDeviceListeners) {
144 - l.deviceRemoved(deviceInfo);
145 - }
146 - }
147 198
148 @Override 199 @Override
149 public Map<DeviceId, NetconfDevice> getDevicesMap() { 200 public Map<DeviceId, NetconfDevice> getDevicesMap() {
...@@ -155,6 +206,8 @@ public class NetconfControllerImpl implements NetconfController { ...@@ -155,6 +206,8 @@ public class NetconfControllerImpl implements NetconfController {
155 return netconfDeviceMap.keySet(); 206 return netconfDeviceMap.keySet();
156 } 207 }
157 208
209 +
210 +
158 //Device factory for the specific NetconfDeviceImpl 211 //Device factory for the specific NetconfDeviceImpl
159 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory { 212 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory {
160 213
...@@ -171,7 +224,7 @@ public class NetconfControllerImpl implements NetconfController { ...@@ -171,7 +224,7 @@ public class NetconfControllerImpl implements NetconfController {
171 @Override 224 @Override
172 public void event(NetconfDeviceOutputEvent event) { 225 public void event(NetconfDeviceOutputEvent event) {
173 if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) { 226 if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) {
174 - removeDevice(event.getDeviceInfo()); 227 + removeDevice(event.getDeviceInfo().getDeviceId());
175 } 228 }
176 } 229 }
177 230
......
...@@ -375,13 +375,12 @@ public class NetconfSessionImpl implements NetconfSession { ...@@ -375,13 +375,12 @@ public class NetconfSessionImpl implements NetconfSession {
375 375
376 private boolean close(boolean force) throws NetconfException { 376 private boolean close(boolean force) throws NetconfException {
377 StringBuilder rpc = new StringBuilder(); 377 StringBuilder rpc = new StringBuilder();
378 - rpc.append("<rpc>"); 378 + rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">");
379 if (force) { 379 if (force) {
380 - rpc.append("<kill-configuration/>"); 380 + rpc.append("<kill-session/>");
381 } else { 381 } else {
382 - rpc.append("<close-configuration/>"); 382 + rpc.append("<close-session/>");
383 } 383 }
384 - rpc.append("<close-configuration/>");
385 rpc.append("</rpc>"); 384 rpc.append("</rpc>");
386 rpc.append(ENDPATTERN); 385 rpc.append(ENDPATTERN);
387 return checkReply(sendRequest(rpc.toString())) || close(true); 386 return checkReply(sendRequest(rpc.toString())) || close(true);
......
...@@ -22,6 +22,8 @@ import org.junit.Before; ...@@ -22,6 +22,8 @@ import org.junit.Before;
22 import org.junit.Test; 22 import org.junit.Test;
23 import org.onlab.packet.IpAddress; 23 import org.onlab.packet.IpAddress;
24 import org.onosproject.net.DeviceId; 24 import org.onosproject.net.DeviceId;
25 +import org.onosproject.net.device.DeviceService;
26 +import org.onosproject.net.key.DeviceKeyService;
25 import org.onosproject.netconf.NetconfDevice; 27 import org.onosproject.netconf.NetconfDevice;
26 import org.onosproject.netconf.NetconfDeviceFactory; 28 import org.onosproject.netconf.NetconfDeviceFactory;
27 import org.onosproject.netconf.NetconfDeviceInfo; 29 import org.onosproject.netconf.NetconfDeviceInfo;
...@@ -44,12 +46,14 @@ import static org.junit.Assert.*; ...@@ -44,12 +46,14 @@ import static org.junit.Assert.*;
44 * Unit tests for the Netconf controller implementation test. 46 * Unit tests for the Netconf controller implementation test.
45 */ 47 */
46 public class NetconfControllerImplTest { 48 public class NetconfControllerImplTest {
49 +
47 NetconfControllerImpl ctrl; 50 NetconfControllerImpl ctrl;
48 51
49 //DeviceInfo 52 //DeviceInfo
50 NetconfDeviceInfo deviceInfo1; 53 NetconfDeviceInfo deviceInfo1;
51 NetconfDeviceInfo deviceInfo2; 54 NetconfDeviceInfo deviceInfo2;
52 NetconfDeviceInfo badDeviceInfo3; 55 NetconfDeviceInfo badDeviceInfo3;
56 + NetconfDeviceInfo deviceInfoIpV6;
53 57
54 //Devices & DeviceId 58 //Devices & DeviceId
55 NetconfDevice device1; 59 NetconfDevice device1;
...@@ -68,21 +72,29 @@ public class NetconfControllerImplTest { ...@@ -68,21 +72,29 @@ public class NetconfControllerImplTest {
68 private static final String DEVICE_1_IP = "10.10.10.11"; 72 private static final String DEVICE_1_IP = "10.10.10.11";
69 private static final String DEVICE_2_IP = "10.10.10.12"; 73 private static final String DEVICE_2_IP = "10.10.10.12";
70 private static final String BAD_DEVICE_IP = "10.10.10.13"; 74 private static final String BAD_DEVICE_IP = "10.10.10.13";
75 + private static final String DEVICE_IPV6 = "2001:db8::1";
71 76
72 private static final int DEVICE_1_PORT = 11; 77 private static final int DEVICE_1_PORT = 11;
73 private static final int DEVICE_2_PORT = 12; 78 private static final int DEVICE_2_PORT = 12;
74 private static final int BAD_DEVICE_PORT = 13; 79 private static final int BAD_DEVICE_PORT = 13;
80 + private static final int IPV6_DEVICE_PORT = 14;
81 +
82 + private static DeviceService deviceService = new NetconfDeviceServiceMock();
83 + private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock();
75 84
76 85
77 @Before 86 @Before
78 public void setUp() throws Exception { 87 public void setUp() throws Exception {
79 ctrl = new NetconfControllerImpl(); 88 ctrl = new NetconfControllerImpl();
80 ctrl.deviceFactory = new TestNetconfDeviceFactory(); 89 ctrl.deviceFactory = new TestNetconfDeviceFactory();
90 + ctrl.deviceService = deviceService;
91 + ctrl.deviceKeyService = deviceKeyService;
81 92
82 //Creating mock devices 93 //Creating mock devices
83 deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT); 94 deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
84 deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT); 95 deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
85 badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT); 96 badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
97 + deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
86 98
87 device1 = new TestNetconfDevice(deviceInfo1); 99 device1 = new TestNetconfDevice(deviceInfo1);
88 deviceId1 = deviceInfo1.getDeviceId(); 100 deviceId1 = deviceInfo1.getDeviceId();
...@@ -102,9 +114,9 @@ public class NetconfControllerImplTest { ...@@ -102,9 +114,9 @@ public class NetconfControllerImplTest {
102 reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl); 114 reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
103 115
104 eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null, 116 eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null,
105 - null, Optional.of(1), deviceInfo1); 117 + null, Optional.of(1), deviceInfo1);
106 eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null, 118 eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
107 - null, Optional.of(2), deviceInfo2); 119 + null, Optional.of(2), deviceInfo2);
108 } 120 }
109 121
110 @After 122 @After
...@@ -128,7 +140,7 @@ public class NetconfControllerImplTest { ...@@ -128,7 +140,7 @@ public class NetconfControllerImplTest {
128 ctrl.addDeviceListener(deviceListener3); 140 ctrl.addDeviceListener(deviceListener3);
129 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3)); 141 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3));
130 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1, 142 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1,
131 - deviceListener2, deviceListener3)); 143 + deviceListener2, deviceListener3));
132 144
133 ctrl.removeDeviceListener(deviceListener1); 145 ctrl.removeDeviceListener(deviceListener1);
134 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2)); 146 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2));
...@@ -168,7 +180,7 @@ public class NetconfControllerImplTest { ...@@ -168,7 +180,7 @@ public class NetconfControllerImplTest {
168 public void testConnectBadDevice() throws Exception { 180 public void testConnectBadDevice() throws Exception {
169 reflectedDeviceMap.clear(); 181 reflectedDeviceMap.clear();
170 try { 182 try {
171 - ctrl.connectDevice(badDeviceInfo3); 183 + ctrl.connectDevice(badDeviceInfo3.getDeviceId());
172 } finally { 184 } finally {
173 assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size()); 185 assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
174 } 186 }
...@@ -180,25 +192,37 @@ public class NetconfControllerImplTest { ...@@ -180,25 +192,37 @@ public class NetconfControllerImplTest {
180 @Test 192 @Test
181 public void testConnectCorrectDevice() throws Exception { 193 public void testConnectCorrectDevice() throws Exception {
182 reflectedDeviceMap.clear(); 194 reflectedDeviceMap.clear();
183 - ctrl.connectDevice(deviceInfo1); 195 + ctrl.connectDevice(deviceInfo1.getDeviceId());
184 - ctrl.connectDevice(deviceInfo2); 196 + ctrl.connectDevice(deviceInfo2.getDeviceId());
185 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId1)); 197 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId1));
186 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2)); 198 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
187 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size()); 199 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
188 } 200 }
189 201
202 + /**
203 + * Check for correct ipv6 device connection. In this case the device map get modified.
204 + */
205 + @Test
206 + public void testConnectCorrectIpv6Device() throws Exception {
207 + reflectedDeviceMap.clear();
208 + ctrl.connectDevice(deviceInfoIpV6.getDeviceId());
209 + assertTrue("Incorrect device connection", ctrl.getDevicesMap()
210 + .containsKey(deviceInfoIpV6.getDeviceId()));
211 + assertEquals("Incorrect device connection", 1, ctrl.getDevicesMap().size());
212 + }
213 +
190 214
191 /** 215 /**
192 * Check for connect devices already added to the map. 216 * Check for connect devices already added to the map.
193 */ 217 */
194 @Test 218 @Test
195 public void testConnectAlreadyExistingDevice() throws Exception { 219 public void testConnectAlreadyExistingDevice() throws Exception {
196 - NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1); 220 + NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
197 - NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2); 221 + NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
198 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(), 222 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
199 - deviceInfo1.getDeviceId()); 223 + deviceInfo1.getDeviceId());
200 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(), 224 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
201 - deviceInfo2.getDeviceId()); 225 + deviceInfo2.getDeviceId());
202 } 226 }
203 227
204 /** 228 /**
...@@ -206,7 +230,7 @@ public class NetconfControllerImplTest { ...@@ -206,7 +230,7 @@ public class NetconfControllerImplTest {
206 */ 230 */
207 @Test 231 @Test
208 public void testDisconnectDevice() throws Exception { 232 public void testDisconnectDevice() throws Exception {
209 - ctrl.disconnectDevice(deviceInfo1); 233 + ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true);
210 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1)); 234 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
211 } 235 }
212 236
...@@ -215,7 +239,7 @@ public class NetconfControllerImplTest { ...@@ -215,7 +239,7 @@ public class NetconfControllerImplTest {
215 */ 239 */
216 @Test 240 @Test
217 public void testRemoveDevice() throws Exception { 241 public void testRemoveDevice() throws Exception {
218 - ctrl.removeDevice(deviceInfo1); 242 + ctrl.removeDevice(deviceInfo1.getDeviceId());
219 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1)); 243 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
220 } 244 }
221 245
...@@ -234,13 +258,13 @@ public class NetconfControllerImplTest { ...@@ -234,13 +258,13 @@ public class NetconfControllerImplTest {
234 @Test 258 @Test
235 public void testDeviceDownEventListener() throws Exception { 259 public void testDeviceDownEventListener() throws Exception {
236 reflectedDeviceMap.clear(); 260 reflectedDeviceMap.clear();
237 - ctrl.connectDevice(deviceInfo1); 261 + ctrl.connectDevice(deviceInfo1.getDeviceId());
238 boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2); 262 boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
239 assertFalse("Irrelevant Device Event", result1); 263 assertFalse("Irrelevant Device Event", result1);
240 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size()); 264 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
241 reflectedDownListener.event(eventForDeviceInfo1); 265 reflectedDownListener.event(eventForDeviceInfo1);
242 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size()); 266 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
243 - ctrl.connectDevice(deviceInfo2); 267 + ctrl.connectDevice(deviceInfo2.getDeviceId());
244 boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2); 268 boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
245 assertTrue("Irrelevant Device Event", result2); 269 assertTrue("Irrelevant Device Event", result2);
246 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size()); 270 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
...@@ -269,7 +293,7 @@ public class NetconfControllerImplTest { ...@@ -269,7 +293,7 @@ public class NetconfControllerImplTest {
269 293
270 public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { 294 public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
271 netconfDeviceInfo = deviceInfo; 295 netconfDeviceInfo = deviceInfo;
272 - if (netconfDeviceInfo.ip() != badDeviceInfo3.ip()) { 296 + if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
273 netconfSession = EasyMock.createMock(NetconfSession.class); 297 netconfSession = EasyMock.createMock(NetconfSession.class);
274 deviceState = true; 298 deviceState = true;
275 } else { 299 } else {
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.netconf.ctl;
18 +
19 +import org.onlab.packet.IpAddress;
20 +import org.onosproject.net.key.DeviceKey;
21 +import org.onosproject.net.key.DeviceKeyId;
22 +import org.onosproject.net.key.DeviceKeyListener;
23 +import org.onosproject.net.key.DeviceKeyService;
24 +import org.onosproject.netconf.NetconfDeviceInfo;
25 +
26 +import java.util.Collection;
27 +
28 +/**
29 + * Mock DeviceKey service to return device keys.
30 + */
31 +class NetconfDeviceKeyServiceMock implements DeviceKeyService {
32 +
33 + private static final String DEVICE_1_IP = "10.10.10.11";
34 + private static final String DEVICE_2_IP = "10.10.10.12";
35 + private static final String BAD_DEVICE_IP = "10.10.10.13";
36 + private static final String DEVICE_IPV6 = "2001:db8::1";
37 +
38 + private static final int DEVICE_1_PORT = 11;
39 + private static final int DEVICE_2_PORT = 12;
40 + private static final int BAD_DEVICE_PORT = 13;
41 + private static final int IPV6_DEVICE_PORT = 14;
42 +
43 + //DeviceInfo
44 + private NetconfDeviceInfo deviceInfo1 =
45 + new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP),
46 + DEVICE_1_PORT);
47 + private NetconfDeviceInfo deviceInfo2 =
48 + new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP),
49 + DEVICE_2_PORT);
50 + private NetconfDeviceInfo badDeviceInfo3 =
51 + new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP),
52 + BAD_DEVICE_PORT);
53 + private NetconfDeviceInfo deviceInfoIpV6 =
54 + new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
55 +
56 + @Override
57 + public Collection<DeviceKey> getDeviceKeys() {
58 + return null;
59 + }
60 +
61 + @Override
62 + public DeviceKey getDeviceKey(DeviceKeyId deviceKeyId) {
63 + if (deviceKeyId.toString().equals(deviceInfo1.getDeviceId().toString())) {
64 + return DeviceKey.createDeviceKeyUsingUsernamePassword(
65 + DeviceKeyId.deviceKeyId(deviceInfo1.getDeviceId().toString()),
66 + null, deviceInfo1.name(), deviceInfo1.password());
67 + } else if (deviceKeyId.toString().equals(deviceInfo2.getDeviceId().toString())) {
68 + return DeviceKey.createDeviceKeyUsingUsernamePassword(
69 + DeviceKeyId.deviceKeyId(deviceInfo2.getDeviceId().toString()),
70 + null, deviceInfo2.name(), deviceInfo2.password());
71 + } else if (deviceKeyId.toString().equals(badDeviceInfo3.getDeviceId().toString())) {
72 + return DeviceKey.createDeviceKeyUsingUsernamePassword(
73 + DeviceKeyId.deviceKeyId(badDeviceInfo3.getDeviceId().toString()),
74 + null, badDeviceInfo3.name(), badDeviceInfo3.password());
75 + } else if (deviceKeyId.toString().equals(deviceInfoIpV6.getDeviceId().toString())) {
76 + return DeviceKey.createDeviceKeyUsingUsernamePassword(
77 + DeviceKeyId.deviceKeyId(deviceInfoIpV6.getDeviceId().toString()),
78 + null, deviceInfoIpV6.name(), deviceInfoIpV6.password());
79 + }
80 + return null;
81 + }
82 +
83 + @Override
84 + public void addListener(DeviceKeyListener listener) {
85 +
86 + }
87 +
88 + @Override
89 + public void removeListener(DeviceKeyListener listener) {
90 +
91 + }
92 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.netconf.ctl;
18 +
19 +import org.onosproject.net.Device;
20 +import org.onosproject.net.DeviceId;
21 +import org.onosproject.net.MastershipRole;
22 +import org.onosproject.net.Port;
23 +import org.onosproject.net.PortNumber;
24 +import org.onosproject.net.device.DeviceListener;
25 +import org.onosproject.net.device.DeviceService;
26 +import org.onosproject.net.device.PortStatistics;
27 +
28 +import java.util.List;
29 +
30 +/**
31 + * Mock device Service.
32 + */
33 +class NetconfDeviceServiceMock implements DeviceService {
34 +
35 + @Override
36 + public int getDeviceCount() {
37 + return 0;
38 + }
39 +
40 + @Override
41 + public Iterable<Device> getDevices() {
42 + return null;
43 + }
44 +
45 + @Override
46 + public Iterable<Device> getDevices(Device.Type type) {
47 + return null;
48 + }
49 +
50 + @Override
51 + public Iterable<Device> getAvailableDevices() {
52 + return null;
53 + }
54 +
55 + @Override
56 + public Iterable<Device> getAvailableDevices(Device.Type type) {
57 + return null;
58 + }
59 +
60 + @Override
61 + public Device getDevice(DeviceId deviceId) {
62 + return null;
63 + }
64 +
65 + @Override
66 + public MastershipRole getRole(DeviceId deviceId) {
67 + return null;
68 + }
69 +
70 + @Override
71 + public List<Port> getPorts(DeviceId deviceId) {
72 + return null;
73 + }
74 +
75 + @Override
76 + public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
77 + return null;
78 + }
79 +
80 + @Override
81 + public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
82 + return null;
83 + }
84 +
85 + @Override
86 + public Port getPort(DeviceId deviceId, PortNumber portNumber) {
87 + return null;
88 + }
89 +
90 + @Override
91 + public boolean isAvailable(DeviceId deviceId) {
92 + return false;
93 + }
94 +
95 + @Override
96 + public void addListener(DeviceListener listener) {
97 +
98 + }
99 +
100 + @Override
101 + public void removeListener(DeviceListener listener) {
102 +
103 + }
104 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -8,4 +8,4 @@ export OCN="10.128.12.4" ...@@ -8,4 +8,4 @@ export OCN="10.128.12.4"
8 8
9 export OCT=$OC1 9 export OCT=$OC1
10 export ONOS_USE_SSH=true 10 export ONOS_USE_SSH=true
11 -export ONOS_APPS=drivers,openflow,proxyarp,ovsdb,pathpainter 11 +export ONOS_APPS=drivers,openflow,proxyarp,ovsdb,pathpainter
...\ No newline at end of file ...\ No newline at end of file
......