7a50b76b440c4dbd766c209c2393821a7d2bed19
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / UuidBaseType.java
1 /*
2  * Copyright © 2014, 2017 EBay Software Foundation 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.ovsdb.lib.schema;
9
10 import com.fasterxml.jackson.databind.JsonNode;
11 import org.opendaylight.ovsdb.lib.notation.ReferencedRow;
12 import org.opendaylight.ovsdb.lib.notation.UUID;
13
14 final class UuidBaseType extends BaseType<UuidBaseType> {
15     // These enum types correspond to JSON values and need to be in lower-case currently
16     public enum RefType { strong, weak }
17
18     private String refTable;
19     private UuidBaseType.RefType refType;
20
21     @Override
22     void fillConstraints(final JsonNode node) {
23         JsonNode refTableNode = node.get("refTable");
24         setRefTable(refTableNode != null ? refTableNode.asText() : null);
25
26         JsonNode refTypeJson = node.get("refType");
27         setRefType(refTypeJson != null ? RefType.valueOf(refTypeJson.asText()) : RefType.strong);
28     }
29
30     @Override
31     public Object toValue(final JsonNode value) {
32         if (value.isArray()) {
33             if (value.size() == 2 && value.get(0).isTextual() && "uuid".equals(value.get(0).asText())) {
34                 return new UUID(value.get(1).asText());
35             }
36         } else {
37             /*
38              * UUIDBaseType used by RefTable from SouthBound will always be an Array of ["uuid", <uuid>].
39              * But there are some cases from northbound where the RefTable type can be expanded to a Row
40              * with contents. In those scenarios, just retain the content and return a ReferencedRow for
41              * the upper layer functions to process it.
42              */
43             return new ReferencedRow(refTable, value);
44         }
45         return null;
46     }
47
48     @Override
49     public void validate(final Object value) {
50
51     }
52
53     public String getRefTable() {
54         return refTable;
55     }
56
57     public void setRefTable(final String refTable) {
58         this.refTable = refTable;
59     }
60
61     public UuidBaseType.RefType getRefType() {
62         return refType;
63     }
64
65     public void setRefType(final UuidBaseType.RefType refType) {
66         this.refType = refType;
67     }
68
69     @Override
70     public String toString() {
71         return "UuidBaseType";
72     }
73
74     @Override
75     public int hashCode() {
76         final int prime = 31;
77         int result = 1;
78         result = prime * result
79                 + (refTable == null ? 0 : refTable.hashCode());
80         result = prime * result
81                 + (refType == null ? 0 : refType.hashCode());
82         return result;
83     }
84
85     @Override
86     public boolean equals(final Object obj) {
87         if (this == obj) {
88             return true;
89         }
90         if (obj == null) {
91             return false;
92         }
93         if (getClass() != obj.getClass()) {
94             return false;
95         }
96         UuidBaseType other = (UuidBaseType) obj;
97         if (refTable == null) {
98             if (other.refTable != null) {
99                 return false;
100             }
101         } else if (!refTable.equals(other.refTable)) {
102             return false;
103         }
104         if (refType != other.refType) {
105             return false;
106         }
107         return true;
108     }
109 }