Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / parsers / uri / URIToDBKey.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.parsers.uri;
21
22 import com.google.common.base.Joiner;
23 import org.onap.aai.exceptions.AAIException;
24 import org.onap.aai.introspection.Introspector;
25 import org.onap.aai.introspection.Loader;
26 import org.onap.aai.serialization.db.EdgeType;
27
28 import javax.ws.rs.core.MultivaluedMap;
29 import java.io.UnsupportedEncodingException;
30 import java.net.URI;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /**
35  * Creates a Unique database key from a URI
36  * 
37  * The key is of the form node-type/key(s).
38  */
39 public class URIToDBKey implements Parsable {
40
41         
42         private List<String> dbKeys = new ArrayList<>();
43
44         /**
45          * Instantiates a new URI to DB key.
46          *
47          * @param loader the loader
48          * @param uri the uri
49          * @throws IllegalArgumentException the illegal argument exception
50          * @throws AAIException the AAI exception
51          * @throws UnsupportedEncodingException the unsupported encoding exception
52          */
53         public URIToDBKey(Loader loader, URI uri) throws IllegalArgumentException, AAIException, UnsupportedEncodingException {
54                 
55                 URIParser parser = new URIParser(loader, uri);
56                 parser.parse(this);
57         }
58         /*
59         public URIToDBKey(Version version, String uri) throws IllegalArgumentException {
60                 
61                 super(version, uri);
62                 try {
63                         context = ModelInjestor.getInstance().getContextForVersion(version);
64                         if (context == null) {
65                                 throw new IllegalArgumentException("could not find a context for version: " + version);
66                         }
67                         this.parse();
68                 } catch (Exception e) {
69                         throw new IllegalArgumentException("uri not valid against our model: " + uri);
70                 }
71         }*/
72         
73         /**
74          * @{inheritDoc}
75          */
76         @Override
77         public void processNamespace(Introspector obj) {
78         
79         }
80         
81         /**
82          * @{inheritDoc}
83          */
84         @Override
85         public String getCloudRegionTransform() {
86                 return "add";
87         }
88         
89         /**
90          * Gets the result.
91          *
92          * @return the result
93          */
94         public Object getResult() {
95                 return Joiner.on("/").join(this.dbKeys);
96         }
97
98         /**
99          * @{inheritDoc}
100          */
101         @Override
102         public boolean useOriginalLoader() {
103                 return false;
104         }
105
106         @Override
107         public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
108                         throws AAIException {
109
110                 dbKeys.add(obj.getDbName());
111
112                 for (String key : uriKeys.keySet()) {
113                         dbKeys.add(uriKeys.getFirst(key).toString());
114                 }               
115         }
116
117         @Override
118         public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
119                         boolean isFinalContainer) throws AAIException {
120         }
121 }