Carmelo Cascone
Committed by Gerrit Code Review

Added loading of BMv2 JSON config during device discovery in BMv2 device

provider

Change-Id: I2000ddb3eefbda0423aaa9e60dfdd1be7fd12bdc
...@@ -24,9 +24,12 @@ import org.jboss.netty.util.HashedWheelTimer; ...@@ -24,9 +24,12 @@ import org.jboss.netty.util.HashedWheelTimer;
24 import org.jboss.netty.util.Timeout; 24 import org.jboss.netty.util.Timeout;
25 import org.jboss.netty.util.TimerTask; 25 import org.jboss.netty.util.TimerTask;
26 import org.onlab.packet.ChassisId; 26 import org.onlab.packet.ChassisId;
27 +import org.onlab.util.HexString;
27 import org.onlab.util.Timer; 28 import org.onlab.util.Timer;
28 import org.onosproject.bmv2.api.runtime.Bmv2ControlPlaneServer; 29 import org.onosproject.bmv2.api.runtime.Bmv2ControlPlaneServer;
29 import org.onosproject.bmv2.api.runtime.Bmv2Device; 30 import org.onosproject.bmv2.api.runtime.Bmv2Device;
31 +import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
32 +import org.onosproject.bmv2.ctl.Bmv2ThriftClient;
30 import org.onosproject.common.net.AbstractDeviceProvider; 33 import org.onosproject.common.net.AbstractDeviceProvider;
31 import org.onosproject.core.ApplicationId; 34 import org.onosproject.core.ApplicationId;
32 import org.onosproject.core.CoreService; 35 import org.onosproject.core.CoreService;
...@@ -37,7 +40,6 @@ import org.onosproject.net.Device; ...@@ -37,7 +40,6 @@ import org.onosproject.net.Device;
37 import org.onosproject.net.DeviceId; 40 import org.onosproject.net.DeviceId;
38 import org.onosproject.net.MastershipRole; 41 import org.onosproject.net.MastershipRole;
39 import org.onosproject.net.PortNumber; 42 import org.onosproject.net.PortNumber;
40 -import org.onosproject.net.SparseAnnotations;
41 import org.onosproject.net.behaviour.PortDiscovery; 43 import org.onosproject.net.behaviour.PortDiscovery;
42 import org.onosproject.net.config.ConfigFactory; 44 import org.onosproject.net.config.ConfigFactory;
43 import org.onosproject.net.config.NetworkConfigEvent; 45 import org.onosproject.net.config.NetworkConfigEvent;
...@@ -57,13 +59,11 @@ import java.util.concurrent.Executors; ...@@ -57,13 +59,11 @@ import java.util.concurrent.Executors;
57 import java.util.concurrent.TimeUnit; 59 import java.util.concurrent.TimeUnit;
58 60
59 import static org.onlab.util.Tools.groupedThreads; 61 import static org.onlab.util.Tools.groupedThreads;
62 +import static org.onosproject.bmv2.api.runtime.Bmv2Device.*;
60 import static org.onosproject.bmv2.ctl.Bmv2ThriftClient.forceDisconnectOf; 63 import static org.onosproject.bmv2.ctl.Bmv2ThriftClient.forceDisconnectOf;
61 import static org.onosproject.bmv2.ctl.Bmv2ThriftClient.ping; 64 import static org.onosproject.bmv2.ctl.Bmv2ThriftClient.ping;
62 import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; 65 import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
63 import static org.slf4j.LoggerFactory.getLogger; 66 import static org.slf4j.LoggerFactory.getLogger;
64 -import static org.onosproject.bmv2.api.runtime.Bmv2Device.SCHEME;
65 -import static org.onosproject.bmv2.api.runtime.Bmv2Device.MANUFACTURER;
66 -import static org.onosproject.bmv2.api.runtime.Bmv2Device.HW_VERSION;
67 67
68 /** 68 /**
69 * BMv2 device provider. 69 * BMv2 device provider.
...@@ -176,27 +176,44 @@ public class Bmv2DeviceProvider extends AbstractDeviceProvider { ...@@ -176,27 +176,44 @@ public class Bmv2DeviceProvider extends AbstractDeviceProvider {
176 activeDevices.compute(did, (k, v) -> { 176 activeDevices.compute(did, (k, v) -> {
177 if (!deviceService.isAvailable(did)) { 177 if (!deviceService.isAvailable(did)) {
178 // Device not available in the core, connect it now. 178 // Device not available in the core, connect it now.
179 - SparseAnnotations annotations = DefaultAnnotations.builder() 179 + DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder()
180 - .set(AnnotationKeys.PROTOCOL, SCHEME) 180 + .set(AnnotationKeys.PROTOCOL, SCHEME);
181 - .build(); 181 + dumpJsonConfigToAnnotations(did, annotationsBuilder);
182 DeviceDescription descr = new DefaultDeviceDescription( 182 DeviceDescription descr = new DefaultDeviceDescription(
183 did.uri(), Device.Type.SWITCH, MANUFACTURER, HW_VERSION, 183 did.uri(), Device.Type.SWITCH, MANUFACTURER, HW_VERSION,
184 - UNKNOWN, UNKNOWN, new ChassisId(), annotations); 184 + UNKNOWN, UNKNOWN, new ChassisId(), annotationsBuilder.build());
185 providerService.deviceConnected(did, descr); 185 providerService.deviceConnected(did, descr);
186 } 186 }
187 - // Discover ports. 187 + updatePorts(did);
188 - Device device = deviceService.getDevice(did);
189 - if (device.is(PortDiscovery.class)) {
190 - PortDiscovery portConfig = device.as(PortDiscovery.class);
191 - List<PortDescription> portDescriptions = portConfig.getPorts();
192 - providerService.updatePorts(did, portDescriptions);
193 - } else {
194 - LOG.warn("No PortDiscovery behavior for device {}", did);
195 - }
196 return true; 188 return true;
197 }); 189 });
198 } 190 }
199 191
192 + private void dumpJsonConfigToAnnotations(DeviceId did, DefaultAnnotations.Builder builder) {
193 + // TODO: store json config string somewhere else, possibly in a Bmv2Controller (see ONOS-4419)
194 + try {
195 + String md5 = Bmv2ThriftClient.of(did).getJsonConfigMd5();
196 + // Convert to hex string for readability.
197 + md5 = HexString.toHexString(md5.getBytes());
198 + String jsonString = Bmv2ThriftClient.of(did).dumpJsonConfig();
199 + builder.set("bmv2JsonConfigMd5", md5);
200 + builder.set("bmv2JsonConfigValue", jsonString);
201 + } catch (Bmv2RuntimeException e) {
202 + LOG.warn("Unable to dump device JSON config from device {}: {}", did, e);
203 + }
204 + }
205 +
206 + private void updatePorts(DeviceId did) {
207 + Device device = deviceService.getDevice(did);
208 + if (device.is(PortDiscovery.class)) {
209 + PortDiscovery portConfig = device.as(PortDiscovery.class);
210 + List<PortDescription> portDescriptions = portConfig.getPorts();
211 + providerService.updatePorts(did, portDescriptions);
212 + } else {
213 + LOG.warn("No PortDiscovery behavior for device {}", did);
214 + }
215 + }
216 +
200 private void disconnectDevice(DeviceId did) { 217 private void disconnectDevice(DeviceId did) {
201 LOG.debug("Trying to disconnect device from core... deviceId={}", did); 218 LOG.debug("Trying to disconnect device from core... deviceId={}", did);
202 219
...@@ -204,7 +221,7 @@ public class Bmv2DeviceProvider extends AbstractDeviceProvider { ...@@ -204,7 +221,7 @@ public class Bmv2DeviceProvider extends AbstractDeviceProvider {
204 activeDevices.compute(did, (k, v) -> { 221 activeDevices.compute(did, (k, v) -> {
205 if (deviceService.isAvailable(did)) { 222 if (deviceService.isAvailable(did)) {
206 providerService.deviceDisconnected(did); 223 providerService.deviceDisconnected(did);
207 - // Make sure to close the transport session with device. 224 + // Make sure to close the transport session with device. Do we really need this?
208 forceDisconnectOf(did); 225 forceDisconnectOf(did);
209 } 226 }
210 return null; 227 return null;
......