2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2019 Nordix Foundation.
 
   4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 
   5  * ================================================================================
 
   6  * Licensed under the Apache License, Version 2.0 (the "License");
 
   7  * you may not use this file except in compliance with the License.
 
   8  * You may obtain a copy of the License at
 
  10  *      http://www.apache.org/licenses/LICENSE-2.0
 
  12  * Unless required by applicable law or agreed to in writing, software
 
  13  * distributed under the License is distributed on an "AS IS" BASIS,
 
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  * See the License for the specific language governing permissions and
 
  16  * limitations under the License.
 
  18  * SPDX-License-Identifier: Apache-2.0
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.policy.models.tosca.authorative.provider;
 
  24 import java.util.ArrayList;
 
  25 import java.util.LinkedHashMap;
 
  26 import java.util.List;
 
  28 import lombok.NonNull;
 
  29 import org.onap.policy.models.base.PfConceptKey;
 
  30 import org.onap.policy.models.base.PfModelException;
 
  31 import org.onap.policy.models.dao.PfDao;
 
  32 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 
  33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 
  34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
 
  35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
 
  36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
 
  37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
 
  39 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
 
  40 import org.slf4j.Logger;
 
  41 import org.slf4j.LoggerFactory;
 
  44  * This class provides the provision of information on TOSCA concepts in the database to callers.
 
  46  * @author Liam Fallon (liam.fallon@est.tech)
 
  48 public class AuthorativeToscaProvider {
 
  49     private static final Logger LOGGER = LoggerFactory.getLogger(AuthorativeToscaProvider.class);
 
  54      * @param dao the DAO to use to access the database
 
  55      * @param name the name of the policy type to get.
 
  56      * @param version the version of the policy type to get.
 
  57      * @return the policy types found
 
  58      * @throws PfModelException on errors getting policy types
 
  60     public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
 
  61             throws PfModelException {
 
  63         LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version);
 
  65         ToscaServiceTemplate serviceTemplate =
 
  66                 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative();
 
  68         LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate);
 
  69         return serviceTemplate;
 
  75      * @param dao the DAO to use to access the database
 
  76      * @param name the name of the policy type to get, set to null to get all policy types
 
  77      * @param version the version of the policy type to get, set to null to get all versions
 
  78      * @return the policy types found
 
  79      * @throws PfModelException on errors getting policy types
 
  81     public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version)
 
  82             throws PfModelException {
 
  84         LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version);
 
  86         List<ToscaPolicyType> policyTypeList = asConceptList(
 
  87                 new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes());
 
  89         LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList);
 
  90         return policyTypeList;
 
  94      * Get filtered policy types.
 
  96      * @param dao the DAO to use to access the database
 
  97      * @param filter the filter for the policy types to get
 
  98      * @return the policy types found
 
  99      * @throws PfModelException on errors getting policy types
 
 101     public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao,
 
 102             @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
 
 104         LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter);
 
 106         ToscaServiceTemplate serviceTemplate =
 
 107                 new SimpleToscaProvider().getPolicyTypes(dao, null, null).toAuthorative();
 
 109         List<ToscaPolicyType> filteredPolicyTypes = asConceptList(serviceTemplate.getPolicyTypes());
 
 110         filteredPolicyTypes = filter.filter(filteredPolicyTypes);
 
 112         serviceTemplate.setPolicyTypes(asConceptMap(filteredPolicyTypes));
 
 114         LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, serviceTemplate);
 
 115         return serviceTemplate;
 
 119      * Get filtered policy types.
 
 121      * @param dao the DAO to use to access the database
 
 122      * @param filter the filter for the policy types to get
 
 123      * @return the policy types found
 
 124      * @throws PfModelException on errors getting policy types
 
 126     public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao,
 
 127             @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException {
 
 129         LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter);
 
 131         List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null));
 
 133         LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter,
 
 134                 filteredPolicyTypeList);
 
 135         return filteredPolicyTypeList;
 
 139      * Create policy types.
 
 141      * @param dao the DAO to use to access the database
 
 142      * @param serviceTemplate the service template containing the definition of the policy types to be created
 
 143      * @return the TOSCA service template containing the created policy types
 
 144      * @throws PfModelException on errors creating policy types
 
 146     public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
 
 147             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
 
 149         LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate);
 
 151         ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
 
 152                 .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
 
 154         LOGGER.debug("<-createPolicyTypes: createdServiceTempalate={}", createdServiceTempalate);
 
 155         return createdServiceTempalate;
 
 159      * Update policy types.
 
 161      * @param dao the DAO to use to access the database
 
 162      * @param serviceTemplate the service template containing the definition of the policy types to be modified
 
 163      * @return the TOSCA service template containing the modified policy types
 
 164      * @throws PfModelException on errors updating policy types
 
 166     public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
 
 167             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
 
 169         LOGGER.debug("->updatePolicyTypes: serviceTempalate={}", serviceTemplate);
 
 171         ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
 
 172                 .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
 
 174         LOGGER.debug("<-updatePolicyTypes: updatedServiceTempalate={}", updatedServiceTempalate);
 
 175         return updatedServiceTempalate;
 
 179      * Delete policy type.
 
 181      * @param dao the DAO to use to access the database
 
 182      * @param name the name of the policy type to delete.
 
 183      * @param version the version of the policy type to delete.
 
 184      * @return the TOSCA service template containing the policy type that was deleted
 
 185      * @throws PfModelException on errors deleting policy types
 
 187     public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name,
 
 188             @NonNull final String version) throws PfModelException {
 
 190         LOGGER.debug("->deletePolicyType: name={}, version={}", name, version);
 
 192         ToscaServiceTemplate deletedServiceTempalate =
 
 193                 new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative();
 
 195         LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTempalate={}", name, version,
 
 196                 deletedServiceTempalate);
 
 197         return deletedServiceTempalate;
 
 203      * @param dao the DAO to use to access the database
 
 204      * @param name the name of the policy to get.
 
 205      * @param version the version of the policy to get.
 
 206      * @return the policies found
 
 207      * @throws PfModelException on errors getting policies
 
 209     public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
 
 210             throws PfModelException {
 
 211         LOGGER.debug("->getPolicies: name={}, version={}", name, version);
 
 213         ToscaServiceTemplate gotServiceTempalate =
 
 214                 new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative();
 
 216         LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version, gotServiceTempalate);
 
 217         return gotServiceTempalate;
 
 223      * @param dao the DAO to use to access the database
 
 224      * @param name the name of the policy to get, null to get all policies
 
 225      * @param version the version of the policy to get, null to get all versions of a policy
 
 226      * @return the policies found
 
 227      * @throws PfModelException on errors getting policies
 
 229     public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version)
 
 230             throws PfModelException {
 
 231         LOGGER.debug("->getPolicyList: name={}, version={}", name, version);
 
 233         List<ToscaPolicy> policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version)
 
 234                 .toAuthorative().getToscaTopologyTemplate().getPolicies());
 
 236         LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList);
 
 241      * Get filtered policies.
 
 243      * @param dao the DAO to use to access the database
 
 244      * @param filter the filter for the policies to get
 
 245      * @return the policies found
 
 246      * @throws PfModelException on errors getting policies
 
 248     public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
 
 249             throws PfModelException {
 
 251         LOGGER.debug("->getFilteredPolicies: filter={}", filter);
 
 252         String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
 
 254         ToscaServiceTemplate serviceTemplate =
 
 255                 new SimpleToscaProvider().getPolicies(dao, filter.getName(), version).toAuthorative();
 
 257         List<ToscaPolicy> filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies());
 
 258         filteredPolicies = filter.filter(filteredPolicies);
 
 260         serviceTemplate.getToscaTopologyTemplate().setPolicies(asConceptMap(filteredPolicies));
 
 262         LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, serviceTemplate);
 
 263         return serviceTemplate;
 
 267      * Get filtered policies.
 
 269      * @param dao the DAO to use to access the database
 
 270      * @param filter the filter for the policies to get
 
 271      * @return the policies found
 
 272      * @throws PfModelException on errors getting policies
 
 274     public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter)
 
 275             throws PfModelException {
 
 277         LOGGER.debug("->getFilteredPolicyList: filter={}", filter);
 
 278         String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion();
 
 280         List<ToscaPolicy> policyList = filter.filter(getPolicyList(dao, filter.getName(), version));
 
 282         LOGGER.debug("<-getFilteredPolicyList: filter={}, policyList={}", filter, policyList);
 
 289      * @param dao the DAO to use to access the database
 
 290      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
 
 291      * @return the TOSCA service template containing the policy types that were created
 
 292      * @throws PfModelException on errors creating policies
 
 294     public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
 
 295             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
 
 297         LOGGER.debug("->createPolicies: serviceTempalate={}", serviceTemplate);
 
 299         ToscaServiceTemplate createdServiceTempalate = new SimpleToscaProvider()
 
 300                 .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
 
 302         LOGGER.debug("<-createPolicies: createdServiceTempalate={}", createdServiceTempalate);
 
 303         return createdServiceTempalate;
 
 309      * @param dao the DAO to use to access the database
 
 310      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
 
 311      * @return the TOSCA service template containing the policies that were updated
 
 312      * @throws PfModelException on errors updating policies
 
 314     public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
 
 315             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
 
 317         LOGGER.debug("->updatePolicies: serviceTempalate={}", serviceTemplate);
 
 319         ToscaServiceTemplate updatedServiceTempalate = new SimpleToscaProvider()
 
 320                 .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
 
 322         LOGGER.debug("<-updatePolicies: updatedServiceTempalate={}", updatedServiceTempalate);
 
 323         return updatedServiceTempalate;
 
 329      * @param dao the DAO to use to access the database
 
 330      * @param name the name of the policy to delete.
 
 331      * @param version the version of the policy to delete.
 
 332      * @return the TOSCA service template containing the policy that weas deleted
 
 333      * @throws PfModelException on errors deleting policies
 
 335     public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name,
 
 336             @NonNull final String version) throws PfModelException {
 
 338         LOGGER.debug("->deletePolicy: name={}, version={}", name, version);
 
 340         ToscaServiceTemplate deletedServiceTempalate =
 
 341                 new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative();
 
 343         LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTempalate={}", name, version,
 
 344                 deletedServiceTempalate);
 
 345         return deletedServiceTempalate;
 
 349      * Return the contents of a list of maps as a plain list.
 
 351      * @param listOfMaps the list of maps
 
 352      * @return the plain list
 
 354     private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) {
 
 355         List<T> returnList = new ArrayList<>();
 
 356         for (Map<String, T> conceptMap : listOfMaps) {
 
 357             for (T concept : conceptMap.values()) {
 
 358                 returnList.add(concept);
 
 366      * Return the contents of a list of concepts as a list of maps of concepts.
 
 368      * @param conceptList the concept list
 
 369      * @return the list of concept map
 
 371     private <T extends ToscaEntity> List<Map<String, T>> asConceptMap(List<T> conceptList) {
 
 372         List<Map<String, T>> toscaEntityMapList = new ArrayList<>();
 
 373         for (T concept : conceptList) {
 
 374             Map<String, T> conceptMap = new LinkedHashMap<>();
 
 375             conceptMap.put(concept.getName(), concept);
 
 376             toscaEntityMapList.add(conceptMap);
 
 379         return toscaEntityMapList;