2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.policy.microservice;
26 import java.util.List;
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;
35 public class MicroServicePolicyService implements PolicyService<MicroServicePolicy> {
37 private final MicroServicePolicyRepository repository;
40 public MicroServicePolicyService(MicroServicePolicyRepository repository) {
41 this.repository = repository;
45 public Set<MicroServicePolicy> updatePolicies(Loop loop, List<MicroServicePolicy> newMicroservicePolicies) {
46 return newMicroservicePolicies.stream().map(policy -> getAndUpdateMicroServicePolicy(loop, policy))
47 .collect(Collectors.toSet());
51 public boolean isExisting(String policyName) {
52 return repository.existsById(policyName);
56 * Get and update the MicroService policy properties.
58 * @param loop The loop
59 * @param policy The new MicroService policy
60 * @return The updated MicroService policy
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())));
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);
76 oldPolicy.setPdpGroup(newPolicy.getPdpGroup());
77 oldPolicy.setPdpSubgroup(newPolicy.getPdpSubgroup());
82 * Update the MicroService policy deployment related parameters.
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
88 public void updateDcaeDeploymentFields(MicroServicePolicy microServicePolicy, String deploymentId,
89 String deploymentUrl) {
90 microServicePolicy.setDcaeDeploymentId(deploymentId);
91 microServicePolicy.setDcaeDeploymentStatusUrl(deploymentUrl);
92 repository.save(microServicePolicy);