SDN Controller Programming Guide

177
NOTE
Synchronization on the in-memory data structure and the fact that
Switch
is a mutable class have been
intentionally ignored. Even though it is important to consider the multi-thread environment nature of
RESTful web services and data protection (since the same references from the in-memory data store are
returned by the business logic) they are irrelevant for the purpose of the illustration: Consuming services
published by the controller. A more serious implementation would make use of the synchronization
tools offered by Java and make copies of the objects before they are returned (Adding a copy
constructor in
Switch
for example) or even better use a database. Complicated code is avoided for
illustration purposes.
SwitchManager.java In-Memory Data Storage:
package com.hp.hm.impl;
...
public class SwitchManager implements SwitchService {
@SuppressWarnings("unused")
private final SystemInformationService systemInformationService;
@SuppressWarnings("unused")
private AlertService alertService;
private Map<Id<Switch, Long>, Switch> devices;
private AtomicLong idCount;
public SwitchManager(SystemInformationService systemInformationService) {
if (systemInformationService == null) {
throw new NullPointerException(...);
}
this.systemInformationService = systemInformationService;
devices = new HashMap<Id<Switch, Long>, Switch>();
idCount = new AtomicLong(1);
}
public void setAlertService(AlertService alertService) {
this.alertService = alertService;
}
@Override
public Switch add(Switch device) {
if (device == null) {
throw new NullPointerException("device cannot be null");
}
Switch deviceToAdd = device;