SDN Controller Programming Guide

154
for (String field : fields) {
if (node.path(field).isMissingNode()) {
throw new IllegalArgumentException("JSON node '" + node
+ "' is missing field '" + field + "'");
}
}
}
}
}
There are some dependencies to declare in order to implement the codecs. Open the hm-
rs/pom.xml file and add the XML extract from the JSON Module Dependencies listing to the
<dependencies> node; after updating the POM file update the Eclipse project dependencies (see
Updating Project Dependencies on page 115).
JSON Module Dependencies:
<dependency>
<groupId>com.hp.util</groupId>
<artifactId>hp-util-codec</artifactId>
<version>${hp-util.version}</version>
</dependency>
<dependency>
<groupId>com.hp.sdn</groupId>
<artifactId>sdn-adm-api</artifactId>
<version>${sdn.version}</version>
</dependency>
The HP VAN SDN Controller SDK uses Jackson API [39] as the underlying API to handle JSON
conversion.
In order to make the SwitchJsonCodec, and any other codec in the application, available so it can
be reused, create a JSON factory that is registered to the central JSON repository which is
exposed as a regular service called JsonService. The following listing shows the implementation of
this JSON factory:
HmJsonFactory.java:
package com.hp.hm.rs.json;
import com.hp.util.json.AbstractJsonFactory;
import com.hp.util.json.JsonFactory;
...
@Component
@Service
@Property(name = "app", value = "flare")
public class HmJsonFactory extends AbstractJsonFactory {
public HmJsonFactory() {
// Register all application’s JSON codecs
addCodecs(Switch.class, new SwitchJsonCodec());
}