2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.renderer.opflex.mit;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
15 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.messages.ManagedObject;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import com.fasterxml.jackson.databind.DeserializationFeature;
22 import com.fasterxml.jackson.databind.JsonNode;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
25 import com.fasterxml.jackson.databind.node.ArrayNode;
26 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
27 import com.fasterxml.jackson.databind.node.ObjectNode;
31 protected static final Logger LOG = LoggerFactory.getLogger(MitLib.class);
32 private final ObjectMapper objectMapper;
33 private final JsonNodeFactory jnf;
36 public static class Reference {
41 public String getSubject() {
45 public void setSubject(String subject) {
46 this.subject = subject;
49 public String getReference_uri() {
53 public void setReference_uri(String reference_uri) {
54 this.reference_uri = reference_uri;
60 objectMapper = new ObjectMapper();
61 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
62 jnf = objectMapper.getNodeFactory();
65 public static final String REFERENCE_SUBJECT = "subject";
66 public static final String REFERENCE_URI = "reference_uri";
68 public BigInteger deserializeMoPropertyEnum(JsonNode node, PolicyPropertyInfo ppi) {
70 EnumInfo ei = ppi.getEnumInfo();
71 return ei.getEnumValue(node.asText());
74 public String serializeMoPropertyEnum(PolicyPropertyInfo ppi, PolicyObjectInstance poi) {
76 EnumInfo ei = ppi.getEnumInfo();
77 return ei.getEnumValue(poi.getUint64(ppi.getPropId()));
81 * Deserialize a REFERENCE property
86 public PolicyObjectInstance.PolicyReference deserializeMoPropertyRef(JsonNode node, OpflexMit mit) {
88 JsonNode sn = node.findValue(REFERENCE_SUBJECT);
91 JsonNode un = node.findValue(REFERENCE_URI);
95 PolicyClassInfo pci = mit.getClass(sn.asText());
99 return new PolicyObjectInstance.PolicyReference(pci.getClassId(), new Uri(un.asText()));
104 * Serialize a REFERENCE property
110 public ObjectNode serializeMoPropertyRef(PolicyObjectInstance.PolicyReference reference, OpflexMit mit) {
112 ObjectNode on = jnf.objectNode();
113 PolicyClassInfo pci = mit.getClass(reference.getClassId());
116 PolicyUri puri = new PolicyUri(reference.getUri().getValue());
117 // walk our way up until we find a valid class
118 String identifier = puri.pop();
119 while (mit.getClass(identifier) == null) {
120 identifier = puri.pop();
122 on.put(REFERENCE_SUBJECT, identifier);
123 on.put(REFERENCE_URI, reference.getUri().getValue());
128 * Take the {@link ManagedObject} and deserialize the properties
129 * into a concrete type to be used by the renderer. It also
130 * adds URIs for any children that are referenced in the
131 * properties to the MO's "children" array.
135 public PolicyObjectInstance deserializeMoProperties(ManagedObject mo, OpflexMit mit) {
138 * The subject gives us the class to use for the schema
140 PolicyClassInfo pci = mit.getClass(mo.getSubject());
146 PolicyObjectInstance poi = new PolicyObjectInstance(pci.getClassId());
148 if (mo.getProperties() == null)
151 for (ManagedObject.Property prop : mo.getProperties()) {
152 PolicyPropertyInfo ppi = pci.getProperty(prop.getName());
153 if ((ppi == null) || (prop.getData() == null))
156 JsonNode node = prop.getData();
158 boolean vectored = false;
159 if (ppi.getPropCardinality().equals(PolicyPropertyInfo.PropertyCardinality.VECTOR)) {
162 switch (ppi.getType()) {
168 List<String> sl = new ArrayList<String>();
169 for (int i = 0; i < node.size(); i++) {
170 JsonNode jn = node.get(i);
176 poi.setString(ppi.getPropId(), sl);
178 if (!node.isTextual())
181 poi.setString(ppi.getPropId(), node.asText());
190 List<BigInteger> bil = new ArrayList<BigInteger>();
192 for (int i = 0; i < node.size(); i++) {
193 JsonNode jn = node.get(i);
194 if (!jn.isBigInteger())
197 bil.add(new BigInteger(jn.asText()));
199 poi.setUint64(ppi.getPropId(), bil);
201 if (!node.isBigInteger())
204 poi.setUint64(ppi.getPropId(), new BigInteger(node.asText()));
213 List<Long> ll = new ArrayList<Long>();
215 for (int i = 0; i < node.size(); i++) {
216 JsonNode jn = node.get(i);
218 if (!jn.isBigInteger())
223 poi.setInt64(ppi.getPropId(), ll);
225 if (!node.isBigInteger())
228 poi.setInt64(ppi.getPropId(), node.asLong());
237 List<PolicyObjectInstance.PolicyReference> prl = new ArrayList<PolicyObjectInstance.PolicyReference>();
239 for (int i = 0; i < node.size(); i++) {
240 JsonNode jn = node.get(i);
241 PolicyObjectInstance.PolicyReference pr = deserializeMoPropertyRef(jn, mit);
247 poi.setReference(ppi.getPropId(), prl);
249 PolicyObjectInstance.PolicyReference pr = deserializeMoPropertyRef(node, mit);
253 poi.setReference(ppi.getPropId(), pr);
265 List<BigInteger> bil = new ArrayList<BigInteger>();
267 for (int i = 0; i < node.size(); i++) {
268 JsonNode jn = node.get(i);
271 BigInteger bi = deserializeMoPropertyEnum(node, ppi);
275 poi.setUint64(ppi.getPropId(), bil);
277 if (!node.isTextual())
280 BigInteger bi = deserializeMoPropertyEnum(node, ppi);
281 poi.setUint64(ppi.getPropId(), bi);
290 List<MacAddress> ml = new ArrayList<MacAddress>();
292 for (int i = 0; i < node.size(); i++) {
293 JsonNode jn = node.get(i);
298 ml.add(new MacAddress(jn.asText()));
300 poi.setMacAddress(ppi.getPropId(), ml);
302 if (!node.isTextual())
305 poi.setMacAddress(ppi.getPropId(), new MacAddress(node.asText()));
319 * Serialize the properties contained in the {@link PolicyObjectInstance} into the properties
320 * field of the {@link ManagedObject}
327 public void serializeMoProperties(PolicyClassInfo pci, PolicyObjectInstance poi, ManagedObject mo, OpflexMit mit) {
329 List<PolicyPropertyInfo> ppil = pci.getProperties();
333 List<ManagedObject.Property> pl = new ArrayList<ManagedObject.Property>();
336 * For serialization of values, we can cheat a bit,
337 * as the native "toString" method gives us exactly
338 * the formatting we need (including vectors).
340 for (PolicyPropertyInfo ppi : ppil) {
342 * Skip any properties that aren't populated for this
345 if (ppi.getType() != PolicyPropertyInfo.PropertyType.COMPOSITE
346 && !poi.isSet(ppi.getPropId(), ppi.getType(), ppi.getPropCardinality())) {
349 ManagedObject.Property p = null;
350 boolean scalar = true;
352 if (ppi.getPropCardinality() == PolicyPropertyInfo.PropertyCardinality.VECTOR) {
356 switch (ppi.getType()) {
358 p = new ManagedObject.Property();
359 p.setName(ppi.getPropName());
361 JsonNode jn = jnf.textNode(poi.getString(ppi.getPropId()));
364 int len = poi.getStringSize(ppi.getPropId());
365 ArrayNode an = jnf.arrayNode();
366 for (int i = 0; i < len; i++) {
367 an.add(poi.getString(ppi.getPropId(), i));
374 p = new ManagedObject.Property();
375 p.setName(ppi.getPropName());
377 JsonNode jn = jnf.numberNode(poi.getInt64(ppi.getPropId()));
380 int len = poi.getInt64Size(ppi.getPropId());
381 ArrayNode an = jnf.arrayNode();
382 for (int i = 0; i < len; i++) {
383 an.add(Long.valueOf(poi.getInt64(ppi.getPropId(), i)));
393 p = new ManagedObject.Property();
394 p.setName(ppi.getPropName());
396 JsonNode jn = jnf.textNode(serializeMoPropertyEnum(ppi, poi));
399 int len = poi.getUint64Size(ppi.getPropId());
400 ArrayNode an = jnf.arrayNode();
401 for (int i = 0; i < len; i++) {
402 an.add(serializeMoPropertyEnum(ppi, poi));
409 p = new ManagedObject.Property();
410 p.setName(ppi.getPropName());
412 JsonNode jn = jnf.numberNode(poi.getUint64(ppi.getPropId()));
415 int len = poi.getUint64Size(ppi.getPropId());
416 ArrayNode an = jnf.arrayNode();
417 for (int i = 0; i < len; i++) {
418 an.numberNode(poi.getUint64(ppi.getPropId()));
425 p = new ManagedObject.Property();
426 p.setName(ppi.getPropName());
428 MacAddress mac = poi.getMacAddress(ppi.getPropId());
429 JsonNode jn = jnf.textNode(mac.getValue());
432 int len = poi.getMacAddressSize(ppi.getPropId());
433 ArrayNode an = jnf.arrayNode();
434 for (int i = 0; i < len; i++) {
435 MacAddress mac = poi.getMacAddress(ppi.getPropId());
436 an.add(mac.getValue());
443 p = new ManagedObject.Property();
444 p.setName(ppi.getPropName());
446 ObjectNode on = serializeMoPropertyRef(poi.getReference(ppi.getPropId()), mit);
449 int len = poi.getReferenceSize(ppi.getPropId());
450 ArrayNode an = jnf.arrayNode();
451 for (int i = 0; i < len; i++) {
452 ObjectNode on = serializeMoPropertyRef(poi.getReference(ppi.getPropId(), i), mit);
461 * Get the URI to add to the list of children
473 mo.setProperties(pl);