2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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============================================
21 package org.onap.aai.champcore.schema;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Optional;
28 import java.util.concurrent.atomic.AtomicInteger;
30 import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
31 import org.onap.aai.champcore.model.ChampConnectionConstraint;
32 import org.onap.aai.champcore.model.ChampField;
33 import org.onap.aai.champcore.model.ChampObject;
34 import org.onap.aai.champcore.model.ChampObjectConstraint;
35 import org.onap.aai.champcore.model.ChampPartition;
36 import org.onap.aai.champcore.model.ChampPropertyConstraint;
37 import org.onap.aai.champcore.model.ChampRelationship;
38 import org.onap.aai.champcore.model.ChampRelationshipConstraint;
39 import org.onap.aai.champcore.model.ChampSchema;
41 public final class DefaultChampSchemaEnforcer implements ChampSchemaEnforcer {
44 public void validate(ChampObject champObject, ChampObjectConstraint champObjectConstraint) throws ChampSchemaViolationException {
45 for (ChampPropertyConstraint pc : champObjectConstraint.getPropertyConstraints()) {
46 final ChampField field = pc.getField();
47 final Optional<Object> property = champObject.getProperty(field.getName());
49 if (pc.isRequired() && !property.isPresent()) {
50 throw new ChampSchemaViolationException("Required property " + pc.getField().getName() + " is not present");
53 if (property.isPresent()) {
54 switch (pc.getCardinality()) {
56 if (!pc.getField().getJavaType().isInstance(property.get())) {
57 throw new ChampSchemaViolationException("Expected type " + pc.getField().getType() + " for type " + pc.getField().getName());
61 if (!(property.get() instanceof List)) throw new ChampSchemaViolationException("Expected List type for ChampCardinality." + pc.getCardinality());
64 if (!(property.get() instanceof Set)) throw new ChampSchemaViolationException("Expected Set type for ChampCardinality." + pc.getCardinality());
67 throw new RuntimeException("Unknown property constraint cardinality " + pc.getCardinality());
74 public void validate(ChampRelationship champRelationship,
75 ChampRelationshipConstraint champRelationshipConstraint) throws ChampSchemaViolationException {
77 for (ChampPropertyConstraint pc : champRelationshipConstraint.getPropertyConstraints()) {
78 final ChampField field = pc.getField();
79 final Optional<Object> property = champRelationship.getProperty(field.getName());
81 if (pc.isRequired() && !property.isPresent()) {
82 throw new ChampSchemaViolationException("Required property " + pc.getField().getName() + " is not present");
85 if (property.isPresent() && !pc.getField().getJavaType().isInstance(property.get())) {
86 throw new ChampSchemaViolationException("Expected type " + pc.getField().getType() + " for type " + pc.getField().getName());
92 public void validate(ChampPartition champPartition, ChampSchema schema) throws ChampSchemaViolationException {
94 for (ChampObject object : champPartition.getChampObjects()) {
95 final Optional<ChampObjectConstraint> objConstraint = schema.getObjectConstraint(object.getType());
97 if (!objConstraint.isPresent()) continue;
99 validate(object, objConstraint.get());
101 final Map<String, Set<ChampRelationship>> incidentRelationshipsByType = champPartition.getIncidentRelationshipsByType(object);
103 for (Map.Entry<String, Set<ChampRelationship>> incidentRelationshipsOfType : incidentRelationshipsByType.entrySet()) {
104 final Optional<ChampRelationshipConstraint> relConstraint = schema.getRelationshipConstraint(incidentRelationshipsOfType.getKey());
106 if (relConstraint.isPresent()) {
107 final ChampRelationshipConstraint relationshipConstraint = relConstraint.get();
108 final Map<ChampConnectionConstraint, AtomicInteger> connectionCounts = new HashMap<ChampConnectionConstraint, AtomicInteger> ();
110 for (ChampRelationship incidentRelationship : incidentRelationshipsOfType.getValue()) {
111 final Optional<ChampConnectionConstraint> connectionConstraint = relationshipConstraint.getConnectionConstraint(incidentRelationship);
113 validate(incidentRelationship, relationshipConstraint);
115 if (connectionConstraint.isPresent()) {
117 if (!connectionCounts.containsKey(connectionConstraint.get())) {
118 connectionCounts.put(connectionConstraint.get(), new AtomicInteger(0));
121 final int connectionCount = connectionCounts.get(connectionConstraint.get()).incrementAndGet();
123 switch (connectionConstraint.get().getMultiplicity()) {
128 if (connectionCount > 0) throw new ChampSchemaViolationException("Violated connection constraint " + connectionConstraint.get());
131 if (connectionCount > 1) throw new ChampSchemaViolationException("Violated connection constraint " + connectionConstraint.get());