Fix sonars in xacml-pdp
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / matchable / MatchablePropertyTypeMap.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2020 Nordix Foundation.
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  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.pdp.xacml.application.common.matchable;
25
26 import com.att.research.xacml.api.Identifier;
27 import com.att.research.xacml.std.IdentifierImpl;
28 import java.util.Collections;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import lombok.NonNull;
32 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaSchemaDefinition;
35 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
36 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslatorUtils;
37
38 public class MatchablePropertyTypeMap extends MatchablePropertyTypeBase<Map<String, MatchablePropertyType<?>>> {
39     MatchableProperty primitiveProperty;
40
41     /**
42      * constructor.
43      *
44      * @param toscaProperty ToscaProperty object
45      */
46     public MatchablePropertyTypeMap(@NonNull ToscaProperty toscaProperty) {
47         super(toscaProperty);
48
49         ToscaSchemaDefinition schema = toscaProperty.getEntrySchema();
50         this.primitiveProperty = MatchablePolicyType.handlePrimitive(schema.getType(), schema);
51     }
52
53     @SuppressWarnings("unchecked")
54     @Override
55     public Map<String, MatchablePropertyType<?>> validate(Object value) throws ToscaPolicyConversionException {
56         if (value instanceof Map) {
57             return (Map<String, MatchablePropertyType<?>>) value;
58         }
59         return Collections.emptyMap();
60     }
61
62     @Override
63     public Object generate(Object value, Identifier attributeId) throws ToscaPolicyConversionException {
64         var anyOf = new AnyOfType();
65         for (Entry<String, MatchablePropertyType<?>> entrySet : this.validate(value).entrySet()) {
66             final String id = entrySet.getKey();
67             final Object val = entrySet.getValue();
68             //
69             // For map we use the LHS as part of the attribute id
70             //
71             Identifier newId = new IdentifierImpl(attributeId + ":" + id);
72             //
73             // Build the AnyOfType
74             //
75             anyOf = ToscaPolicyTranslatorUtils.buildAndAppendAllof(anyOf,
76                     primitiveProperty.getType().generate(val, newId));
77         }
78         return anyOf;
79     }
80
81 }