Merge "Removed the DMAAP Accepted/Rejected Call"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / controllers / ServiceController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oransc.policyagent.controllers;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
29
30 import java.time.Duration;
31 import java.util.Collection;
32 import java.util.Vector;
33
34 import org.oransc.policyagent.exceptions.ServiceException;
35 import org.oransc.policyagent.repository.Policies;
36 import org.oransc.policyagent.repository.Policy;
37 import org.oransc.policyagent.repository.Service;
38 import org.oransc.policyagent.repository.Services;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.DeleteMapping;
43 import org.springframework.web.bind.annotation.GetMapping;
44 import org.springframework.web.bind.annotation.PutMapping;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestParam;
47 import org.springframework.web.bind.annotation.RestController;
48
49 @RestController
50 public class ServiceController {
51
52     private final Services services;
53     private final Policies policies;
54
55     private static Gson gson = new GsonBuilder() //
56         .serializeNulls() //
57         .create(); //
58
59     @Autowired
60     ServiceController(Services services, Policies policies) {
61         this.services = services;
62         this.policies = policies;
63     }
64
65     @GetMapping("/services")
66     @ApiOperation(value = "Returns service information", response = ServiceStatus.class)
67     @ApiResponses(value = {@ApiResponse(code = 200, message = "OK")})
68     public ResponseEntity<String> getServices( //
69         @RequestParam(name = "name", required = false) String name) {
70
71         Collection<ServiceStatus> servicesStatus = new Vector<>();
72         synchronized (this.services) {
73             for (Service s : this.services.getAll()) {
74                 if (name == null || name.equals(s.getName())) {
75                     servicesStatus.add(toServiceStatus(s));
76                 }
77             }
78         }
79
80         String res = gson.toJson(servicesStatus);
81         return new ResponseEntity<String>(res, HttpStatus.OK);
82     }
83
84     private ServiceStatus toServiceStatus(Service s) {
85         return ImmutableServiceStatus.builder() //
86             .name(s.getName()) //
87             .keepAliveInterval(s.getKeepAliveInterval().toSeconds()) //
88             .timeSincePing(s.timeSinceLastPing().toSeconds()) //
89             .build();
90     }
91
92     @PutMapping("/service")
93     public ResponseEntity<String> putService( //
94         @RequestBody String jsonBody) {
95         try {
96             ServiceRegistrationInfo s = gson.fromJson(jsonBody, ImmutableServiceRegistrationInfo.class);
97             this.services.put(toService(s));
98             return new ResponseEntity<String>("OK", HttpStatus.OK);
99         } catch (Exception e) {
100             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
101         }
102     }
103
104     @DeleteMapping("/services")
105     public ResponseEntity<String> deleteService( //
106         @RequestParam(name = "name", required = true) String name) {
107         try {
108             Service service = removeService(name);
109             // Remove the policies from the repo and let the consistency monitoring
110             // do the rest.
111             removePolicies(service);
112             return new ResponseEntity<String>("OK", HttpStatus.NO_CONTENT);
113         } catch (Exception e) {
114             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
115         }
116     }
117
118     private Service removeService(String name) throws ServiceException {
119         synchronized (this.services) {
120             Service service = this.services.getService(name);
121             this.services.remove(service.getName());
122             return service;
123         }
124     }
125
126     private void removePolicies(Service service) {
127         synchronized (this.policies) {
128             Vector<Policy> policyList = new Vector<>(this.policies.getForService(service.getName()));
129             for (Policy policy : policyList) {
130                 this.policies.remove(policy);
131             }
132         }
133     }
134
135     private Service toService(ServiceRegistrationInfo s) {
136         return new Service(s.name(), Duration.ofSeconds(s.keepAliveInterval()), s.callbackUrl());
137     }
138
139 }