Rework the policy refresh
[clamp.git] / src / main / java / org / onap / clamp / policy / operational / OperationalPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights
6  *                             reserved.
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  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.policy.operational;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.util.List;
29 import java.util.Set;
30 import java.util.stream.Collectors;
31 import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
32 import org.onap.clamp.loop.Loop;
33 import org.onap.clamp.loop.template.PolicyModelsRepository;
34 import org.onap.clamp.policy.PolicyService;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37
38 @Service
39 public class OperationalPolicyService implements PolicyService<OperationalPolicy> {
40
41     private final OperationalPolicyRepository operationalPolicyRepository;
42
43     private final PolicyModelsRepository policyModelsRepository;
44
45     private static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicyService.class);
46
47     @Autowired
48     public OperationalPolicyService(OperationalPolicyRepository repository,
49                                     PolicyModelsRepository policyModelsRepository) {
50         this.operationalPolicyRepository = repository;
51         this.policyModelsRepository = policyModelsRepository;
52     }
53
54     @Override
55     public Set<OperationalPolicy> updatePolicies(Loop loop, List<OperationalPolicy> operationalPolicies) {
56         return operationalPolicies
57                 .parallelStream()
58                 .map(policy ->
59                         operationalPolicyRepository
60                                 .findById(policy.getName())
61                                 .map(p -> setConfiguration(p, policy))
62                                 .orElse(initializeMissingFields(loop, policy)))
63                 .collect(Collectors.toSet());
64     }
65
66     @Override
67     public boolean isExisting(String policyName) {
68         return operationalPolicyRepository.existsById(policyName);
69     }
70
71     private OperationalPolicy initializeMissingFields(Loop loop, OperationalPolicy policy) {
72         policy.setLoop(loop);
73         return policy;
74     }
75
76     private OperationalPolicy setConfiguration(OperationalPolicy policy, OperationalPolicy newPolicy) {
77         policy.setConfigurationsJson(newPolicy.getConfigurationsJson());
78         policy.setPdpGroup(newPolicy.getPdpGroup());
79         policy.setPdpSubgroup(newPolicy.getPdpSubgroup());
80         return policy;
81     }
82
83     /**
84      * Api to refresh the Operational Policy UI window.
85      *
86      * @param operationalPolicy The operational policy object
87      * @param toscaConverter    the tosca converter required to convert the tosca model to json schema
88      */
89     public void refreshOperationalPolicyJsonRepresentation(OperationalPolicy operationalPolicy,
90                                                            ToscaConverterWithDictionarySupport toscaConverter) {
91         operationalPolicy.updateJsonRepresentation(toscaConverter);
92         this.operationalPolicyRepository.saveAndFlush(operationalPolicy);
93     }
94 }