aboutsummaryrefslogtreecommitdiffstats
path: root/profiles/http
diff options
context:
space:
mode:
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>2018-02-05 20:27:32 +0530
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>2018-02-05 20:27:32 +0530
commit8bf3e7795bcc1c068b800bf3a974312a47e883a2 (patch)
treeca0c8f9c262b0577dec1040dad865fa6467496bc /profiles/http
parentd701b812258d0e84a5d5cce0296b004e364a1a3b (diff)
Improve spl macro handling
Issue-ID: CLI-50 Change-Id: Ia25e6b66ee1ad8c2eee80c8759c62e0a8bd698c1 Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'profiles/http')
0 files changed, 0 insertions, 0 deletions
id='n58' href='#n58'>58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy Model
 * ================================================================================
 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019-2021 Nordix Foundation.
 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.models.tosca.simple.concepts;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.EmbeddedId;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import org.apache.commons.lang3.ObjectUtils;
import org.onap.policy.common.parameters.annotations.NotBlank;
import org.onap.policy.common.parameters.annotations.NotNull;
import org.onap.policy.common.parameters.annotations.Valid;
import org.onap.policy.common.utils.coder.YamlJsonTranslator;
import org.onap.policy.models.base.PfAuthorative;
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfKey;
import org.onap.policy.models.base.PfReferenceKey;
import org.onap.policy.models.base.PfUtils;
import org.onap.policy.models.base.validation.annotations.VerifyKey;
import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;

/**
 * Class to represent the property in TOSCA definition.
 *
 * @author Chenfei Gao (cgao@research.att.com)
 * @author Liam Fallon (liam.fallon@est.tech)
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
    private static final long serialVersionUID = 1675770231921107988L;

    @EmbeddedId
    @VerifyKey
    @NotNull
    private PfReferenceKey key;

    @Column
    @VerifyKey
    @NotNull
    private PfConceptKey type;

    @Column
    @NotBlank
    private String description;

    @Column
    private boolean required = false;

    @Column
    @NotBlank
    private String defaultValue;

    @Column
    private Status status = Status.SUPPORTED;

    @ElementCollection
    private List<@NotNull @Valid JpaToscaConstraint> constraints;

    @Column
    @Valid
    private JpaToscaSchemaDefinition entrySchema;

    @ElementCollection
    private Map<String, String> metadata;

    /**
     * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
     */
    public JpaToscaProperty() {
        this(new PfReferenceKey());
    }

    /**
     * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
     *
     * @param key the key
     */
    public JpaToscaProperty(@NonNull final PfReferenceKey key) {
        this(key, new PfConceptKey());
    }

    /**
     * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
     *
     * @param key the key
     * @param type the key of the property type
     */
    public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
        this.key = key;
        this.type = type;
    }

    /**
     * Copy constructor.
     *
     * @param copyConcept the concept to copy from
     */
    public JpaToscaProperty(final JpaToscaProperty copyConcept) {
        super(copyConcept);
        this.key = new PfReferenceKey(copyConcept.key);
        this.type = new PfConceptKey(copyConcept.type);
        this.description = copyConcept.description;
        this.required = copyConcept.required;
        this.defaultValue = copyConcept.defaultValue;
        this.status = copyConcept.status;
        // Constraints are immutable
        this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
        this.entrySchema =
                (copyConcept.entrySchema != null ? new JpaToscaSchemaDefinition(copyConcept.entrySchema) : null);
        this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null);
    }

    /**
     * Authorative constructor.
     *
     * @param authorativeConcept the authorative concept to copy from
     */
    public JpaToscaProperty(final ToscaProperty authorativeConcept) {
        this.fromAuthorative(authorativeConcept);
    }

    @Override
    public ToscaProperty toAuthorative() {
        var toscaProperty = new ToscaProperty();

        toscaProperty.setName(key.getLocalName());

        toscaProperty.setType(type.getName());
        toscaProperty.setTypeVersion(type.getVersion());

        toscaProperty.setDescription(description);
        toscaProperty.setRequired(required);
        toscaProperty.setStatus(status);

        if (defaultValue != null) {
            toscaProperty.setDefaultValue(new YamlJsonTranslator().fromYaml(defaultValue, Object.class));
        }

        toscaProperty.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));

        if (entrySchema != null) {
            toscaProperty.setEntrySchema(entrySchema.toAuthorative());
        }

        toscaProperty.setMetadata(PfUtils.mapMap(metadata, metadataItem -> metadataItem));

        return toscaProperty;
    }

    @Override
    public void fromAuthorative(ToscaProperty toscaProperty) {
        this.setKey(new PfReferenceKey());
        getKey().setLocalName(toscaProperty.getName());

        if (toscaProperty.getTypeVersion() != null) {
            type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
        } else {
            type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
        }

        description = toscaProperty.getDescription();
        required = toscaProperty.isRequired();
        status = toscaProperty.getStatus();

        if (toscaProperty.getDefaultValue() != null) {
            defaultValue = new YamlJsonTranslator().toYaml(toscaProperty.getDefaultValue()).trim();
        } else {
            defaultValue = null;
        }

        constraints = PfUtils.mapList(toscaProperty.getConstraints(), JpaToscaConstraint::newInstance);

        if (toscaProperty.getEntrySchema() != null) {
            entrySchema = new JpaToscaSchemaDefinition(toscaProperty.getEntrySchema());
        }

        metadata = PfUtils.mapMap(toscaProperty.getMetadata(), metadataItem -> metadataItem);
    }

    @Override
    public List<PfKey> getKeys() {
        final List<PfKey> keyList = getKey().getKeys();

        keyList.addAll(type.getKeys());

        if (entrySchema != null) {
            keyList.addAll(entrySchema.getKeys());
        }

        return keyList;
    }

    @Override
    public void clean() {
        key.clean();

        type.clean();

        if (description != null) {
            description = description.trim();
        }

        if (defaultValue != null) {
            defaultValue = defaultValue.trim();
        }

        if (entrySchema != null) {
            entrySchema.clean();
        }

        metadata = PfUtils.mapMap(metadata, String::trim);
    }

    @Override
    public int compareTo(final PfConcept otherConcept) {
        if (otherConcept == null) {
            return -1;
        }
        if (this == otherConcept) {
            return 0;
        }
        if (getClass() != otherConcept.getClass()) {
            return getClass().getName().compareTo(otherConcept.getClass().getName());
        }

        final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
        if (!key.equals(other.key)) {
            return key.compareTo(other.key);
        }

        return compareFields(other);
    }

    /**
     * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
     *
     * @param other the other ToscaProperty object
     */
    private int compareFields(final JpaToscaProperty other) {
        if (!type.equals(other.type)) {
            return type.compareTo(other.type);
        }

        int result = ObjectUtils.compare(description, other.description);
        if (result != 0) {
            return result;
        }

        result = ObjectUtils.compare(required, other.required);
        if (result != 0) {
            return result;
        }

        result = ObjectUtils.compare(defaultValue, other.defaultValue);
        if (result != 0) {
            return result;
        }

        result = ObjectUtils.compare(status, other.status);
        if (result != 0) {
            return result;
        }

        result = PfUtils.compareCollections(constraints, other.constraints);
        if (result != 0) {
            return result;
        }

        result = entrySchema.compareTo(other.entrySchema);
        if (result != 0) {
            return result;
        }

        return PfUtils.compareMaps(metadata, other.metadata);
    }
}