Merge "Added @Override annotation above signature"
[aai/champ.git] / src / main / java / org / openecomp / aai / champ / schema / DefaultChampSchemaEnforcer.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.openecomp.aai.champ.schema;
23
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.Set;
29 import java.util.concurrent.atomic.AtomicInteger;
30
31 import org.openecomp.aai.champ.exceptions.ChampSchemaViolationException;
32 import org.openecomp.aai.champ.model.ChampConnectionConstraint;
33 import org.openecomp.aai.champ.model.ChampField;
34 import org.openecomp.aai.champ.model.ChampObject;
35 import org.openecomp.aai.champ.model.ChampObjectConstraint;
36 import org.openecomp.aai.champ.model.ChampPartition;
37 import org.openecomp.aai.champ.model.ChampPropertyConstraint;
38 import org.openecomp.aai.champ.model.ChampRelationship;
39 import org.openecomp.aai.champ.model.ChampRelationshipConstraint;
40 import org.openecomp.aai.champ.model.ChampSchema;
41
42 public final class DefaultChampSchemaEnforcer implements ChampSchemaEnforcer {
43
44         @Override
45         public void validate(ChampObject champObject, ChampObjectConstraint champObjectConstraint) throws ChampSchemaViolationException {
46                 for (ChampPropertyConstraint pc : champObjectConstraint.getPropertyConstraints()) {
47                         final ChampField field = pc.getField();
48                         final Optional<Object> property = champObject.getProperty(field.getName());
49                         
50                         if (pc.isRequired() && !property.isPresent()) {
51                                 throw new ChampSchemaViolationException("Required property " + pc.getField().getName() + " is not present");
52                         }
53
54                         if (property.isPresent()) {
55                                 switch (pc.getCardinality()) {
56                                 case SINGLE:
57                                         if (!pc.getField().getJavaType().isInstance(property.get())) {
58                                                 throw new ChampSchemaViolationException("Expected type " + pc.getField().getType() + " for type " + pc.getField().getName());
59                                         }
60                                         break;
61                                 case LIST:
62                                         if (!(property.get() instanceof List)) throw new ChampSchemaViolationException("Expected List type for ChampCardinality." + pc.getCardinality());
63                                         break;
64                                 case SET:
65                                         if (!(property.get() instanceof Set)) throw new ChampSchemaViolationException("Expected Set type for ChampCardinality." + pc.getCardinality());
66                                         break;
67                                 default:
68                                         throw new RuntimeException("Unknown property constraint cardinality " + pc.getCardinality());
69                                 }
70                         }
71                 }
72         }
73
74         @Override
75         public void validate(ChampRelationship champRelationship,
76                         ChampRelationshipConstraint champRelationshipConstraint) throws ChampSchemaViolationException {
77
78                 for (ChampPropertyConstraint pc : champRelationshipConstraint.getPropertyConstraints()) {
79                         final ChampField field = pc.getField();
80                         final Optional<Object> property = champRelationship.getProperty(field.getName());
81                         
82                         if (pc.isRequired() && !property.isPresent()) {
83                                 throw new ChampSchemaViolationException("Required property " + pc.getField().getName() + " is not present");
84                         }
85
86                         if (property.isPresent() && !pc.getField().getJavaType().isInstance(property.get())) {
87                                 throw new ChampSchemaViolationException("Expected type " + pc.getField().getType() + " for type " + pc.getField().getName());
88                         }
89                 }
90         }
91
92         @Override
93         public void validate(ChampPartition champPartition, ChampSchema schema) throws ChampSchemaViolationException {
94         
95                 for (ChampObject object : champPartition.getChampObjects()) {
96                         final Optional<ChampObjectConstraint> objConstraint = schema.getObjectConstraint(object.getType());
97
98                         if (!objConstraint.isPresent()) continue;
99                         
100                         validate(object, objConstraint.get());
101
102                         final Map<String, Set<ChampRelationship>> incidentRelationshipsByType = champPartition.getIncidentRelationshipsByType(object);
103
104                         for (Map.Entry<String, Set<ChampRelationship>> incidentRelationshipsOfType : incidentRelationshipsByType.entrySet()) {
105                                 final Optional<ChampRelationshipConstraint> relConstraint = schema.getRelationshipConstraint(incidentRelationshipsOfType.getKey());
106
107                                 if (relConstraint.isPresent()) {
108                                         final ChampRelationshipConstraint relationshipConstraint = relConstraint.get();
109                                         final Map<ChampConnectionConstraint, AtomicInteger> connectionCounts = new HashMap<ChampConnectionConstraint, AtomicInteger> ();
110
111                                         for (ChampRelationship incidentRelationship : incidentRelationshipsOfType.getValue()) {
112                                                 final Optional<ChampConnectionConstraint> connectionConstraint = relationshipConstraint.getConnectionConstraint(incidentRelationship);
113
114                                                 validate(incidentRelationship, relationshipConstraint);
115
116                                                 if (connectionConstraint.isPresent()) {
117
118                                                         if (!connectionCounts.containsKey(connectionConstraint.get())) {
119                                                                 connectionCounts.put(connectionConstraint.get(), new AtomicInteger(0));
120                                                         }
121
122                                                         final int connectionCount = connectionCounts.get(connectionConstraint.get()).incrementAndGet();
123
124                                                         switch (connectionConstraint.get().getMultiplicity()) {
125                                                         case MANY:
126                                                                 //Always valid
127                                                         break;
128                                                         case NONE:
129                                                                 if (connectionCount > 0) throw new ChampSchemaViolationException("Violated connection constraint " + connectionConstraint.get());
130                                                         break;
131                                                         case ONE:
132                                                                 if (connectionCount > 1) throw new ChampSchemaViolationException("Violated connection constraint " + connectionConstraint.get());
133                                                         break;
134                                                         default:
135                                                         break;
136                                                         }
137                                                 }
138                                         }
139                                 }
140                                 
141                         }
142                 }
143         }
144 }