[AAI-2404]add aai-schema-abstraction library
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / json / FromJsonEdgeSchema.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2019 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.schemaif.json;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.onap.aai.schemaif.SchemaProviderException;
27 import org.onap.aai.schemaif.definitions.EdgeSchema;
28 import org.onap.aai.schemaif.definitions.PropertySchema;
29 import org.onap.aai.schemaif.json.definitions.JsonEdgeSchema;
30
31 public class FromJsonEdgeSchema extends EdgeSchema {
32     public static final String WILDCARD_CHAR = "*";
33     
34     public FromJsonEdgeSchema() {
35     }
36     
37     public FromJsonEdgeSchema(EdgeSchema other) {
38         // A shallow copy should suffice, as edge definitions don't change.
39         name = other.getName();
40         source = other.getSource();
41         target = other.getTarget();
42         multiplicity = other.getMultiplicity();
43         properties = other.getPropertySchemaList();
44         annotations = other.getAnnotations();
45     }
46     
47     public void fromJson(JsonEdgeSchema jsonEdge) throws SchemaProviderException {
48
49         name = jsonEdge.getLabel();
50         source = jsonEdge.getFrom();
51         target = jsonEdge.getTo();
52         
53         // TODO:  At present, multiplicity isn't described in the JSON schema.  By default, make everything
54         // many-to-many
55         multiplicity = Multiplicity.MANY_2_MANY;
56   
57         // Populate annotation schema
58         annotations = new HashMap<String,String>();
59         if (jsonEdge.getAnnotations() != null) {
60             for (Map.Entry<String,String> entry : jsonEdge.getAnnotations().entrySet()) {
61                 annotations.put(entry.getKey().toLowerCase(), entry.getValue());
62             }
63         }
64         
65         // Currently edge properties are not supported in the json schema
66         properties = new HashMap<String,PropertySchema>();
67     }
68
69     public void replaceWildcard(String vertexName) throws SchemaProviderException {
70         if (source.equals(WILDCARD_CHAR) && target.equals(WILDCARD_CHAR)) {
71             throw new SchemaProviderException("Edge definition with wildcard source and target: " + toString());
72         }
73         
74         if (source.equals(WILDCARD_CHAR)) {
75             source = vertexName;
76         }
77         
78         if (target.equals(WILDCARD_CHAR)) {
79             target = vertexName;
80         }
81     }
82 }