Rework tosca converter
[clamp.git] / src / main / java / org / onap / clamp / policy / microservice / MicroServicePolicyService.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.microservice;
25
26 import java.util.List;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29 import org.onap.clamp.loop.Loop;
30 import org.onap.clamp.policy.PolicyService;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
33
34 @Service
35 public class MicroServicePolicyService implements PolicyService<MicroServicePolicy> {
36
37     private final MicroServicePolicyRepository repository;
38
39     @Autowired
40     public MicroServicePolicyService(MicroServicePolicyRepository repository) {
41         this.repository = repository;
42     }
43
44     @Override
45     public Set<MicroServicePolicy> updatePolicies(Loop loop, List<MicroServicePolicy> newMicroservicePolicies) {
46         return newMicroservicePolicies.stream().map(policy -> getAndUpdateMicroServicePolicy(loop, policy))
47                 .collect(Collectors.toSet());
48     }
49
50     @Override
51     public boolean isExisting(String policyName) {
52         return repository.existsById(policyName);
53     }
54
55     /**
56      * Get and update the MicroService policy properties.
57      *
58      * @param loop   The loop
59      * @param policy The new MicroService policy
60      * @return The updated MicroService policy
61      */
62     public MicroServicePolicy getAndUpdateMicroServicePolicy(Loop loop, MicroServicePolicy policy) {
63         return repository.save(
64                 repository.findById(policy.getName()).map(p -> updateMicroservicePolicyProperties(p, policy, loop))
65                         .orElse(new MicroServicePolicy(policy.getName(), policy.getPolicyModel(),
66                                 policy.getShared(), policy.getJsonRepresentation(), null, policy.getPdpGroup(),
67                                 policy.getPdpSubgroup())));
68     }
69
70     private MicroServicePolicy updateMicroservicePolicyProperties(MicroServicePolicy oldPolicy,
71                                                                   MicroServicePolicy newPolicy, Loop loop) {
72         oldPolicy.setConfigurationsJson(newPolicy.getConfigurationsJson());
73         if (!oldPolicy.getUsedByLoops().contains(loop)) {
74             oldPolicy.getUsedByLoops().add(loop);
75         }
76         oldPolicy.setPdpGroup(newPolicy.getPdpGroup());
77         oldPolicy.setPdpSubgroup(newPolicy.getPdpSubgroup());
78         return oldPolicy;
79     }
80
81     /**
82      * Update the MicroService policy deployment related parameters.
83      *
84      * @param microServicePolicy The micro service policy
85      * @param deploymentId       The deployment ID as returned by DCAE
86      * @param deploymentUrl      The Deployment URL as returned by DCAE
87      */
88     public void updateDcaeDeploymentFields(MicroServicePolicy microServicePolicy, String deploymentId,
89                                            String deploymentUrl) {
90         microServicePolicy.setDcaeDeploymentId(deploymentId);
91         microServicePolicy.setDcaeDeploymentStatusUrl(deploymentUrl);
92         repository.save(microServicePolicy);
93     }
94 }