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