Northbound/RFC7285 CostMap


Y. Richard Yang
 

Dear Kai, team,

I am reading up the parser of RFC7285 (alto/alto-commons/src/main/java/org/opendaylight/alto/commons/types/rfc7285). Let me start by saying that the current design is very clean. Great work!

Here is some more thinking:
- Jackson has three methods to process JSON (data binding, tree, and streaming: http://wiki.fasterxml.com/JacksonInFiveMinutes). The cleanest is definitely data binding, which is the current design. 

- An unknown type in RFC7285 is cost map. As we discussed in previous email, it can be  
Map<String, Map<String, Integer>> 
or 
Map<String, Map<String, Double>> 
depending on the cost-mode. Later, it may become an array...

The current design is the following (alto/alto-commons/src/main/java/org/opendaylight/alto/commons/types/rfc7285/CostMap.java):
    @JsonProperty("meta")
    public Meta meta;

    @JsonProperty("cost-map")
    public Map<String, Map<String, Object>> map
                        = new LinkedHashMap<String, Map<String, Object>>();

This is a clever design using Object. 

One question: I have not tested the code. Given that the northbound writing is public String asJSON(Object obj) throws Exception {
        return mapper.writeValueAsString(obj);
    }

Is it RFC7285 compliant (i.e., output int or double for cost map)?

Suppose we do want the more specific type (e.g., some cost being int and some being double is not allowed), so that Object is changed to a specific ValueType, then generic binding may not work, and we will need to use the streaming parser:

  JsonFactory f = new JsonFactory();
  JsonParser jp = f.createJsonParser(json);
  // skip to first sub object -- **need to assume that the first is meta though**
  Meta meta = mapper.readValue(jp, Meta.class);
  // look into the cost mode in meta and decide the ValueType;
  // map is a pointer to the specific types
  if ("numerical".equals(meta.costType.mode))
    // one branch
  else if 
    // another branch
  ...

I am not proposing the preceding change yet, but it may help to give comments on the design choices--for example, the choice of map of map to Object instead of specific int or double.

Make sense?

Richard

Join z.archive.alto-dev@lists.opendaylight.org to automatically receive all group messages.