Migrate TyperUtils.getTableSchema() users
[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     static final UuidBaseType SINGLETON = new UuidBaseType(null, null);
19     static final BaseTypeFactory<UuidBaseType> FACTORY = new BaseTypeFactory<>() {
20         @Override
21         UuidBaseType create(final JsonNode typeDefinition) {
22             final JsonNode refTableNode = typeDefinition.get("refTable");
23             final String refTable = refTableNode != null ? refTableNode.asText() : null;
24
25             final JsonNode refTypeJson = typeDefinition.get("refType");
26             final RefType refType = refTypeJson != null ? RefType.valueOf(refTypeJson.asText()) : RefType.strong;
27
28             // FIXME: this is weird from refTable/refType perspective -- if there is no table, we should not default
29             //        to strong reference and squash to singleton
30             return new UuidBaseType(refTable, refType);
31         }
32     };
33
34     private final String refTable;
35     private final RefType refType;
36
37     UuidBaseType(final String refTable, final RefType refType) {
38         this.refTable = refTable;
39         this.refType = refType;
40     }
41
42     @Override
43     public Object toValue(final JsonNode value) {
44         if (value.isArray()) {
45             if (value.size() == 2 && value.get(0).isTextual() && "uuid".equals(value.get(0).asText())) {
46                 return new UUID(value.get(1).asText());
47             }
48         } else {
49             /*
50              * UUIDBaseType used by RefTable from SouthBound will always be an Array of ["uuid", <uuid>].
51              * But there are some cases from northbound where the RefTable type can be expanded to a Row
52              * with contents. In those scenarios, just retain the content and return a ReferencedRow for
53              * the upper layer functions to process it.
54              */
55             return new ReferencedRow(refTable, value);
56         }
57         return null;
58     }
59
60     @Override
61     public void validate(final Object value) {
62
63     }
64
65     public String getRefTable() {
66         return refTable;
67     }
68
69     public UuidBaseType.RefType getRefType() {
70         return refType;
71     }
72
73     @Override
74     public String toString() {
75         return "UuidBaseType";
76     }
77
78     @Override
79     public int hashCode() {
80         final int prime = 31;
81         int result = 1;
82         result = prime * result
83                 + (refTable == null ? 0 : refTable.hashCode());
84         result = prime * result
85                 + (refType == null ? 0 : refType.hashCode());
86         return result;
87     }
88
89     @Override
90     public boolean equals(final Object obj) {
91         if (this == obj) {
92             return true;
93         }
94         if (obj == null) {
95             return false;
96         }
97         if (getClass() != obj.getClass()) {
98             return false;
99         }
100         UuidBaseType other = (UuidBaseType) obj;
101         if (refTable == null) {
102             if (other.refTable != null) {
103                 return false;
104             }
105         } else if (!refTable.equals(other.refTable)) {
106             return false;
107         }
108         if (refType != other.refType) {
109             return false;
110         }
111         return true;
112     }
113 }