2 * ============LICENSE_START==========================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 package org.onap.aai.champcore.schema;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Optional;
29 import java.util.concurrent.atomic.AtomicInteger;
31 import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
32 import org.onap.aai.champcore.model.ChampConnectionConstraint;
33 import org.onap.aai.champcore.model.ChampField;
34 import org.onap.aai.champcore.model.ChampObject;
35 import org.onap.aai.champcore.model.ChampObjectConstraint;
36 import org.onap.aai.champcore.model.ChampPartition;
37 import org.onap.aai.champcore.model.ChampPropertyConstraint;
38 import org.onap.aai.champcore.model.ChampRelationship;
39 import org.onap.aai.champcore.model.ChampRelationshipConstraint;
40 import org.onap.aai.champcore.model.ChampSchema;
42 public final class DefaultChampSchemaEnforcer implements ChampSchemaEnforcer {
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());
50 if (pc.isRequired() && !property.isPresent()) {
51 throw new ChampSchemaViolationException("Required property " + pc.getField().getName() + " is not present");
54 if (property.isPresent()) {
55 switch (pc.getCardinality()) {
57 if (!pc.getField().getJavaType().isInstance(property.get())) {
58 throw new ChampSchemaViolationException("Expected type " + pc.getField().getType() + " for type " + pc.getField().getName());
62 if (!(property.get() instanceof List)) throw new ChampSchemaViolationException("Expected List type for ChampCardinality." + pc.getCardinality());
65 if (!(property.get() instanceof Set)) throw new ChampSchemaViolationException("Expected Set type for ChampCardinality." + pc.getCardinality());
68 throw new RuntimeException("Unknown property constraint cardinality " + pc.getCardinality());
75 public void validate(ChampRelationship champRelationship,
76 ChampRelationshipConstraint champRelationshipConstraint) throws ChampSchemaViolationException {
78 for (ChampPropertyConstraint pc : champRelationshipConstraint.getPropertyConstraints()) {
79 final ChampField field = pc.getField();
80 final Optional<Object> property = champRelationship.getProperty(field.getName());
82 if (pc.isRequired() && !property.isPresent()) {
83 throw new ChampSchemaViolationException("Required property " + pc.getField().getName() + " is not present");
86 if (property.isPresent() && !pc.getField().getJavaType().isInstance(property.get())) {
87 throw new ChampSchemaViolationException("Expected type " + pc.getField().getType() + " for type " + pc.getField().getName());
93 public void validate(ChampPartition champPartition, ChampSchema schema) throws ChampSchemaViolationException {
95 for (ChampObject object : champPartition.getChampObjects()) {
96 final Optional<ChampObjectConstraint> objConstraint = schema.getObjectConstraint(object.getType());
98 if (!objConstraint.isPresent()) continue;
100 validate(object, objConstraint.get());
102 final Map<String, Set<ChampRelationship>> incidentRelationshipsByType = champPartition.getIncidentRelationshipsByType(object);
104 for (Map.Entry<String, Set<ChampRelationship>> incidentRelationshipsOfType : incidentRelationshipsByType.entrySet()) {
105 final Optional<ChampRelationshipConstraint> relConstraint = schema.getRelationshipConstraint(incidentRelationshipsOfType.getKey());
107 if (relConstraint.isPresent()) {
108 final ChampRelationshipConstraint relationshipConstraint = relConstraint.get();
109 final Map<ChampConnectionConstraint, AtomicInteger> connectionCounts = new HashMap<ChampConnectionConstraint, AtomicInteger> ();
111 for (ChampRelationship incidentRelationship : incidentRelationshipsOfType.getValue()) {
112 final Optional<ChampConnectionConstraint> connectionConstraint = relationshipConstraint.getConnectionConstraint(incidentRelationship);
114 validate(incidentRelationship, relationshipConstraint);
116 if (connectionConstraint.isPresent()) {
118 if (!connectionCounts.containsKey(connectionConstraint.get())) {
119 connectionCounts.put(connectionConstraint.get(), new AtomicInteger(0));
122 final int connectionCount = connectionCounts.get(connectionConstraint.get()).incrementAndGet();
124 switch (connectionConstraint.get().getMultiplicity()) {
129 if (connectionCount > 0) throw new ChampSchemaViolationException("Violated connection constraint " + connectionConstraint.get());
132 if (connectionCount > 1) throw new ChampSchemaViolationException("Violated connection constraint " + connectionConstraint.get());