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 ObjectMapper objectMapper;
33 private JsonNodeFactory jnf;
36 public static class Reference {
39 public String getSubject() {
42 public void setSubject(String subject) {
43 this.subject = subject;
45 public String getReference_uri() {
48 public void setReference_uri(String reference_uri) {
49 this.reference_uri = reference_uri;
55 objectMapper = new ObjectMapper();
56 objectMapper.configure(
57 DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
58 jnf = objectMapper.getNodeFactory();
61 public static final String REFERENCE_SUBJECT = "subject";
62 public static final String REFERENCE_URI = "reference_uri";
64 public BigInteger deserializeMoPropertyEnum(JsonNode node, PolicyPropertyInfo ppi) {
66 EnumInfo ei = ppi.getEnumInfo();
67 return ei.getEnumValue(node.asText());
70 public String serializeMoPropertyEnum(PolicyPropertyInfo ppi, PolicyObjectInstance poi) {
72 EnumInfo ei = ppi.getEnumInfo();
73 return ei.getEnumValue(poi.getUint64(ppi.getPropId()));
77 * Deserialize a REFERENCE property
82 public PolicyObjectInstance.PolicyReference deserializeMoPropertyRef(JsonNode node, OpflexMit mit) {
84 JsonNode sn = node.findValue(REFERENCE_SUBJECT);
85 if (sn == null) return null;
86 JsonNode un = node.findValue(REFERENCE_URI);
87 if (un == null) return null;
89 PolicyClassInfo pci = mit.getClass(sn.asText());
90 if (pci == null) return null;
92 return new PolicyObjectInstance.PolicyReference(pci.getClassId(), new Uri(un.asText()));
97 * Serialize a REFERENCE property
103 public ObjectNode serializeMoPropertyRef(PolicyObjectInstance.PolicyReference reference, OpflexMit mit) {
105 ObjectNode on = jnf.objectNode();
106 PolicyClassInfo pci = mit.getClass(reference.getClassId());
107 if (pci == null) return null;
108 PolicyUri puri = new PolicyUri(reference.getUri().getValue());
109 // walk our way up until we find a valid class
110 String identifier = puri.pop();
111 while (mit.getClass(identifier) == null) {
112 identifier = puri.pop();
114 on.put(REFERENCE_SUBJECT, identifier);
115 on.put(REFERENCE_URI, reference.getUri().getValue());
120 * Take the {@link ManagedObject} and deserialize the properties
121 * into a concrete type to be used by the renderer. It also
122 * adds URIs for any children that are referenced in the
123 * properties to the MO's "children" array.
127 public PolicyObjectInstance deserializeMoProperties(ManagedObject mo, OpflexMit mit) {
130 * The subject gives us the class to use for the schema
132 PolicyClassInfo pci = mit.getClass(mo.getSubject());
135 if (pci == null) return null;
137 PolicyObjectInstance poi = new PolicyObjectInstance(pci.getClassId());
139 if (mo.getProperties() == null) return poi;
141 for (ManagedObject.Property prop : mo.getProperties()) {
142 PolicyPropertyInfo ppi = pci.getProperty(prop.getName());
143 if ((ppi == null) || (prop.getData() == null)) continue;
145 JsonNode node = prop.getData();
147 boolean vectored = false;
148 if (ppi.getPropCardinality().equals(PolicyPropertyInfo.PropertyCardinality.VECTOR)) {
151 switch(ppi.getType()) {
153 if (vectored == true) {
154 if (!node.isArray()) continue;
156 List<String> sl = new ArrayList<String>();
157 for (int i=0; i <node.size(); i++ ) {
158 JsonNode jn = node.get(i);
159 if (!jn.isTextual()) continue;
163 poi.setString(ppi.getPropId(), sl);
166 if (!node.isTextual()) continue;
168 poi.setString(ppi.getPropId(), node.asText());
173 if (vectored == true) {
174 if (!node.isArray()) continue;
176 List<BigInteger> bil = new ArrayList<BigInteger>();
178 for (int i=0; i <node.size(); i++ ) {
179 JsonNode jn = node.get(i);
180 if (!jn.isBigInteger()) continue;
182 bil.add(new BigInteger(jn.asText()));
184 poi.setUint64(ppi.getPropId(), bil);
187 if (!node.isBigInteger()) continue;
189 poi.setUint64(ppi.getPropId(), new BigInteger(node.asText()));
194 if (vectored == true) {
195 if (!node.isArray()) continue;
197 List<Long> ll = new ArrayList<Long>();
199 for (int i=0; i <node.size(); i++ ) {
200 JsonNode jn = node.get(i);
202 if (!jn.isBigInteger()) continue;
206 poi.setInt64(ppi.getPropId(), ll);
209 if (!node.isBigInteger()) continue;
211 poi.setInt64(ppi.getPropId(), node.asLong());
216 if (vectored == true) {
217 if (!node.isArray()) continue;
219 List<PolicyObjectInstance.PolicyReference> prl =
220 new ArrayList<PolicyObjectInstance.PolicyReference>();
222 for (int i=0; i <node.size(); i++ ) {
223 JsonNode jn = node.get(i);
224 PolicyObjectInstance.PolicyReference pr = deserializeMoPropertyRef(jn, mit);
225 if (pr == null) continue;
229 poi.setReference(ppi.getPropId(), prl);
232 PolicyObjectInstance.PolicyReference pr = deserializeMoPropertyRef(node, mit);
233 if (pr == null) continue;
235 poi.setReference(ppi.getPropId(), pr);
243 if (vectored == true) {
244 if (!node.isArray()) continue;
246 List<BigInteger> bil = new ArrayList<BigInteger>();
248 for (int i=0; i <node.size(); i++ ) {
249 JsonNode jn = node.get(i);
250 if (!jn.isTextual()) continue;
251 BigInteger bi = deserializeMoPropertyEnum(node,ppi);
255 poi.setUint64(ppi.getPropId(), bil);
258 if (!node.isTextual()) continue;
260 BigInteger bi = deserializeMoPropertyEnum(node,ppi);
261 poi.setUint64(ppi.getPropId(), bi);
266 if (vectored == true) {
267 if (!node.isArray()) continue;
269 List<MacAddress> ml = new ArrayList<MacAddress>();
271 for (int i=0; i <node.size(); i++ ) {
272 JsonNode jn = node.get(i);
274 if (!jn.isTextual()) continue;
276 ml.add(new MacAddress(jn.asText()));
278 poi.setMacAddress(ppi.getPropId(), ml);
281 if (!node.isTextual()) continue;
283 poi.setMacAddress(ppi.getPropId(), new MacAddress(node.asText()));
297 * Serialize the properties contained in the {@link PolicyObjectInstance}
298 * into the properties field of the {@link ManagedObject}
305 public void serializeMoProperties(PolicyClassInfo pci, PolicyObjectInstance poi, ManagedObject mo, OpflexMit mit) {
307 List<PolicyPropertyInfo> ppil = pci.getProperties();
308 if (ppil == null) return;
310 List<ManagedObject.Property> pl = new ArrayList<ManagedObject.Property>();
313 * For serialization of values, we can cheat a bit,
314 * as the native "toString" method gives us exactly
315 * the formatting we need (including vectors).
317 for (PolicyPropertyInfo ppi: ppil) {
319 * Skip any properties that aren't populated for this
322 if (ppi.getType() != PolicyPropertyInfo.PropertyType.COMPOSITE &&
323 !poi.isSet(ppi.getPropId(), ppi.getType(), ppi.getPropCardinality())) {
326 ManagedObject.Property p = null;
327 boolean scalar = true;
329 if (ppi.getPropCardinality() == PolicyPropertyInfo.PropertyCardinality.VECTOR) {
333 switch (ppi.getType()) {
335 p = new ManagedObject.Property();
336 p.setName(ppi.getPropName());
337 if (scalar == true) {
338 JsonNode jn = jnf.textNode(poi.getString(ppi.getPropId()));
342 int len = poi.getStringSize(ppi.getPropId());
343 ArrayNode an = jnf.arrayNode();
344 for (int i = 0; i < len; i++ ) {
345 an.add(poi.getString(ppi.getPropId(), i));
352 p = new ManagedObject.Property();
353 p.setName(ppi.getPropName());
354 if (scalar == true) {
355 JsonNode jn = jnf.numberNode(poi.getInt64(ppi.getPropId()));
359 int len = poi.getInt64Size(ppi.getPropId());
360 ArrayNode an = jnf.arrayNode();
361 for (int i = 0; i < len; i++ ) {
362 an.add(Long.valueOf(poi.getInt64(ppi.getPropId(), i)));
372 p = new ManagedObject.Property();
373 p.setName(ppi.getPropName());
374 if (scalar == true) {
375 JsonNode jn = jnf.textNode(serializeMoPropertyEnum(ppi, poi));
379 int len = poi.getUint64Size(ppi.getPropId());
380 ArrayNode an = jnf.arrayNode();
381 for (int i = 0; i < len; i++ ) {
382 an.add(serializeMoPropertyEnum(ppi, poi));
389 p = new ManagedObject.Property();
390 p.setName(ppi.getPropName());
391 if (scalar == true) {
392 JsonNode jn = jnf.numberNode(poi.getUint64(ppi.getPropId()));
396 int len = poi.getUint64Size(ppi.getPropId());
397 ArrayNode an = jnf.arrayNode();
398 for (int i = 0; i < len; i++ ) {
399 an.numberNode(poi.getUint64(ppi.getPropId()));
406 p = new ManagedObject.Property();
407 p.setName(ppi.getPropName());
408 if (scalar == true) {
409 MacAddress mac = poi.getMacAddress(ppi.getPropId());
410 JsonNode jn = jnf.textNode(mac.getValue().toString());
414 int len = poi.getMacAddressSize(ppi.getPropId());
415 ArrayNode an = jnf.arrayNode();
416 for (int i = 0; i < len; i++ ) {
417 MacAddress mac = poi.getMacAddress(ppi.getPropId());
418 an.add(mac.getValue().toString());
425 p = new ManagedObject.Property();
426 p.setName(ppi.getPropName());
427 if (scalar == true) {
428 ObjectNode on = serializeMoPropertyRef(poi.getReference(ppi.getPropId()), mit);
432 int len = poi.getReferenceSize(ppi.getPropId());
433 ArrayNode an = jnf.arrayNode();
434 for (int i = 0; i < len; i++ ) {
435 ObjectNode on = serializeMoPropertyRef(poi.getReference(ppi.getPropId(), i), mit);
444 * Get the URI to add to the list of children
456 mo.setProperties(pl);