a061ad6f5e554723a95bd0d5f4eea9f79a1a687b
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / tools / CreateUUID.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.introspection.tools;
23
24 import org.onap.aai.introspection.Introspector;
25 import org.onap.aai.schema.enums.PropertyMetadata;
26
27 import java.util.Map;
28 import java.util.UUID;
29
30 /**
31  * <b>CreateUUID</b> is an issue resolver that is responsible
32  * for looking to check if the property that is missing has
33  * the metadata autoGenerateUuid associated to it in the oxm
34  * As if that is found, then it will automatically resolve the
35  * issue by generating a uuid and setting that value to that property
36  *
37  * If this is needed for a specific property that you need
38  * then you need to add the following xml code in the oxm
39  *
40  * <pre>
41  *     {@code
42  *      <xml-element java-attribute="myElementProp" name="my-element-prop" type="java.lang.String">
43  *      <xml-properties>
44  *          <xml-property name="autoGenerateUuid" value="true" />
45  *      </xml-properties>
46  *     }
47  * </pre>
48  */
49 public class CreateUUID implements IssueResolver {
50
51         /**
52          * Resolves the issue by checking if the issue type is missing key prop
53          * and if it is it will retrieve the introspector associated with the issue
54          * then gets the metadata associated to that specific property
55          * and if it contains the auto generate meta property and if it does
56          * then it will fix it by setting that property value to generated uuid
57          *
58          * @param issue the issue with the details associated to the problem
59          * @return true if the issue has been successfully resolved
60          *          false otherwise
61          */
62         @Override
63         public boolean resolveIssue(Issue issue) {
64
65                 Introspector obj = issue.getIntrospector();
66                 if (issue.getType().equals(IssueType.MISSING_KEY_PROP)) {
67                         Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(issue.getPropName());
68                         if (metadata.containsKey(PropertyMetadata.AUTO_GENERATE_UUID) && metadata.get(PropertyMetadata.AUTO_GENERATE_UUID).equals("true")) {
69                                 obj.setValue(issue.getPropName(), UUID.randomUUID().toString());
70                                 return true;
71                         }
72                 }
73                 
74                 return false;
75         }
76
77 }