ec8538d9eb580207fb0d804b801a467c432cfa70
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aaiclient.client.aai;
22
23 import java.io.Serializable;
24 import java.net.URL;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.regex.Pattern;
29 import org.onap.aai.annotations.Metadata;
30 import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName;
31 import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectType;
32 import org.onap.aaiclient.client.aai.entities.uri.AAIFluentTypeReverseLookup;
33 import org.reflections.Reflections;
34 import org.reflections.scanners.SubTypesScanner;
35 import org.reflections.util.ClasspathHelper;
36 import org.reflections.util.ConfigurationBuilder;
37 import com.google.common.base.CaseFormat;
38
39 /**
40  * Types are accessed through AAIFluentTypeBuilder and should no longer be added as static members
41  *
42  */
43 public class AAIObjectType implements AAIObjectBase, AAIObjectName, GraphInventoryObjectType, Serializable {
44
45     private static final long serialVersionUID = -2877184776691514600L;
46     private static Map<String, AAIObjectType> map = new HashMap<>();
47
48     public static final AAIObjectType GENERIC_QUERY = new AAIObjectType("/search", "/generic-query", "generic-query");
49     public static final AAIObjectType BULK_PROCESS = new AAIObjectType("/bulkprocess", "", "bulkprocess");
50     public static final AAIObjectType SINGLE_TRANSACTION =
51             new AAIObjectType("/bulk/single-transaction", "", "single-transaction");
52     public static final AAIObjectType NODES_QUERY = new AAIObjectType("/search", "/nodes-query", "nodes-query");
53     public static final AAIObjectType CUSTOM_QUERY = new AAIObjectType("/query", "", "query");
54     public static final AAIObjectType UNKNOWN = new AAIObjectType("", "", "unknown") {
55
56         private static final long serialVersionUID = 9208984071038447607L;
57
58         @Override
59         public boolean passThrough() {
60             return true;
61         }
62     };
63     public static final AAIObjectType DSL = new AAIObjectType("/dsl", "", "dsl");
64     public static final AAIObjectType SUB_L_INTERFACE = new AAIObjectType(
65             "/cloud-infrastructure/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}/l-interfaces/l-interface/{interface-name}",
66             "/l-interfaces/l-interface/{sub-interface-name}", "sub-l-interface");
67
68     private final String uriTemplate;
69     private final String parentUri;
70     private final String partialUri;
71     private final Class<?> aaiObjectClass;
72     private final String name;
73
74     static {
75         /* Locate any AAIObjectTypes on the classpath and add them to our map */
76         java.util.Collection<URL> packages = ClasspathHelper.forPackage("");
77         Reflections r =
78                 new Reflections(new ConfigurationBuilder().setUrls(packages).setScanners(new SubTypesScanner()));
79
80         Set<Class<? extends AAIObjectType>> resources = r.getSubTypesOf(AAIObjectType.class);
81
82         for (Class<? extends AAIObjectType> customTypeClass : resources) {
83             AAIObjectType customType;
84             try {
85                 customType = customTypeClass.newInstance();
86             } catch (InstantiationException | IllegalAccessException e) {
87             }
88         }
89     }
90
91     protected AAIObjectType() {
92         this.parentUri = null;
93         this.partialUri = null;
94         this.uriTemplate = null;
95         this.aaiObjectClass = null;
96         this.name = null;
97     }
98
99     protected AAIObjectType(String parentUri, String partialUri, String name) {
100         this(parentUri, partialUri, name, true);
101     }
102
103     public AAIObjectType(String parentUri, String partialUri, String name, boolean register) {
104         this.parentUri = parentUri;
105         this.partialUri = partialUri;
106         this.uriTemplate = parentUri + partialUri;
107         this.aaiObjectClass = null;
108         this.name = name;
109         if (register && !AAIObjectType.map.containsKey(name)) {
110             AAIObjectType.map.put(name, this);
111         }
112     }
113
114     protected AAIObjectType(String parentUri, Class<?> aaiObjectClass) {
115         this.parentUri = parentUri;
116         this.partialUri = removeParentUri(aaiObjectClass, parentUri);
117         this.uriTemplate = parentUri + partialUri;
118         this.aaiObjectClass = aaiObjectClass;
119         this.name = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, aaiObjectClass.getSimpleName());
120         if (!AAIObjectType.map.containsKey(name)) {
121             AAIObjectType.map.put(name, this);
122         }
123     }
124
125     @Override
126     public String toString() {
127         return this.uriTemplate();
128     }
129
130     public static AAIObjectType fromTypeName(String name, String uri) {
131
132         return new AAIFluentTypeReverseLookup().fromName(name, uri);
133     }
134
135     public static AAIObjectType fromTypeName(String name) {
136         if (map.containsKey(name)) {
137             return map.get(name);
138         } else {
139             return AAIObjectType.UNKNOWN;
140         }
141     }
142
143     @Override
144     public String typeName() {
145         return this.typeName(CaseFormat.LOWER_HYPHEN);
146     }
147
148     @Override
149     public String typeName(CaseFormat format) {
150         return CaseFormat.LOWER_HYPHEN.to(format, this.name.replace("default-", ""));
151     }
152
153     @Override
154     public String uriTemplate() {
155         return this.uriTemplate;
156     }
157
158     @Override
159     public String partialUri() {
160         return this.partialUri;
161     }
162
163     @Override
164     public int hashCode() {
165         return this.typeName().hashCode();
166     }
167
168     @Override
169     public boolean equals(Object o) {
170
171         if (o instanceof GraphInventoryObjectName) {
172             return this.typeName().equals(((GraphInventoryObjectName) o).typeName());
173         }
174
175         return false;
176     }
177
178     protected String removeParentUri(Class<?> aaiObjectClass, String parentUri) {
179         return aaiObjectClass.getAnnotation(Metadata.class).uriTemplate().replaceFirst(Pattern.quote(parentUri), "");
180     }
181 }