Upgrade jackson library to version 2.3.0
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / implementation / src / main / java / org / opendaylight / controller / sal / connector / remoterpc / dto / RouteIdentifierImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.connector.remoterpc.dto;
9
10 import java.io.Serializable;
11 import java.net.URI;
12
13 import com.fasterxml.jackson.databind.JsonNode;
14 import com.fasterxml.jackson.databind.ObjectMapper;
15 import org.opendaylight.controller.sal.connector.api.RpcRouter;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
18
19 public class RouteIdentifierImpl implements RpcRouter.RouteIdentifier<QName, QName, InstanceIdentifier>,Serializable {
20
21   transient ObjectMapper mapper = new ObjectMapper();
22
23   private QName context;
24   private QName type;
25   private InstanceIdentifier route;
26
27   @Override
28   public QName getContext() {
29     return this.context;
30   }
31
32   @Override
33   public QName getType() {
34     return this.type;
35   }
36
37   @Override
38   public InstanceIdentifier getRoute() {
39     return this.route;
40   }
41
42   public void setContext(QName context) {
43     this.context = context;
44   }
45
46   public void setType(QName type) {
47     this.type = type;
48   }
49
50   public void setRoute(InstanceIdentifier route) {
51     this.route = route;
52   }
53
54   @Override
55   public String toString() {
56     try {
57       return mapper.writeValueAsString(this);
58     } catch (Throwable e) {
59       //do nothing
60     }
61
62     return super.toString();
63   }
64
65   public RpcRouter.RouteIdentifier fromString(String input)
66       throws Exception {
67
68     JsonNode root = mapper.readTree(input);
69     this.context  = parseQName(root.get("context"));
70     this.type     = parseQName(root.get("type"));
71
72     return this;
73   }
74
75   private QName parseQName(JsonNode node){
76     if (node == null) return null;
77
78     String namespace = (node.get("namespace") != null) ?
79                        node.get("namespace").asText()  : "";
80
81     String localName = (node.get("localName") != null) ?
82                        node.get("localName").asText() : "";
83
84     URI uri = URI.create(namespace);
85     return new QName(uri, localName);
86   }
87 }