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