LoopLog repository
[clamp.git] / src / main / java / org / onap / clamp / loop / LoopService.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.loop;
25
26 import com.google.gson.JsonObject;
27
28 import java.util.List;
29 import java.util.Set;
30
31 import javax.persistence.EntityNotFoundException;
32
33 import org.onap.clamp.policy.microservice.MicroServicePolicy;
34 import org.onap.clamp.policy.microservice.MicroservicePolicyService;
35 import org.onap.clamp.policy.operational.OperationalPolicy;
36 import org.onap.clamp.policy.operational.OperationalPolicyService;
37 import org.springframework.stereotype.Component;
38 import org.springframework.stereotype.Service;
39
40 @Service
41 @Component
42 public class LoopService {
43
44     private final LoopsRepository loopsRepository;
45     private final MicroservicePolicyService microservicePolicyService;
46     private final OperationalPolicyService operationalPolicyService;
47
48     /**
49      * Constructor.
50      */
51     public LoopService(LoopsRepository loopsRepository, MicroservicePolicyService microservicePolicyService,
52         OperationalPolicyService operationalPolicyService) {
53         this.loopsRepository = loopsRepository;
54         this.microservicePolicyService = microservicePolicyService;
55         this.operationalPolicyService = operationalPolicyService;
56     }
57
58     Loop saveOrUpdateLoop(Loop loop) {
59         return loopsRepository.save(loop);
60     }
61
62     List<String> getClosedLoopNames() {
63         return loopsRepository.getAllLoopNames();
64     }
65
66     public Loop getLoop(String loopName) {
67         return loopsRepository.findById(loopName).orElse(null);
68     }
69
70     Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) {
71         Loop loop = findClosedLoopByName(loopName);
72         Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies);
73         loop.setOperationalPolicies(newPolicies);
74         return loopsRepository.save(loop);
75     }
76
77     Loop updateAndSaveMicroservicePolicies(String loopName, List<MicroServicePolicy> newMicroservicePolicies) {
78         Loop loop = findClosedLoopByName(loopName);
79         Set<MicroServicePolicy> newPolicies = microservicePolicyService.updatePolicies(loop, newMicroservicePolicies);
80         loop.setMicroServicePolicies(newPolicies);
81         return loopsRepository.save(loop);
82     }
83
84     Loop updateAndSaveGlobalPropertiesJson(String loopName, JsonObject newGlobalPropertiesJson) {
85         Loop loop = findClosedLoopByName(loopName);
86         loop.setGlobalPropertiesJson(newGlobalPropertiesJson);
87         return loopsRepository.save(loop);
88     }
89
90     MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) {
91         Loop loop = findClosedLoopByName(loopName);
92         MicroServicePolicy newPolicies = microservicePolicyService.getAndUpdateMicroServicePolicy(loop,
93             newMicroservicePolicy);
94         return newPolicies;
95     }
96
97     private Loop findClosedLoopByName(String loopName) {
98         return loopsRepository.findById(loopName)
99             .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName));
100     }
101 }