Continue work with PolicyControl
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / controllers / RicRepositoryController.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 package org.oransc.policyagent.controllers;
21
22 import com.google.gson.Gson;
23 import com.google.gson.GsonBuilder;
24
25 import io.swagger.annotations.ApiOperation;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28
29 import java.util.Optional;
30 import java.util.Vector;
31
32 import org.oransc.policyagent.configuration.ApplicationConfig;
33 import org.oransc.policyagent.configuration.RicConfig;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.GetMapping;
38 import org.springframework.web.bind.annotation.RequestParam;
39 import org.springframework.web.bind.annotation.RestController;
40
41 @RestController
42 public class RicRepositoryController {
43
44     private final ApplicationConfig appConfig;
45     private static Gson gson = new GsonBuilder() //
46         .serializeNulls() //
47         .create(); //
48
49     @Autowired
50     RicRepositoryController(ApplicationConfig config) {
51         this.appConfig = config;
52     }
53
54     /**
55      * Example: http://localhost:8080/rics?managedElementId=kista_1
56      */
57     @GetMapping("/ric")
58     @ApiOperation(value = "Returns the name of a RIC managing one Mananged Element")
59     @ApiResponses(
60         value = { //
61             @ApiResponse(code = 200, message = "RIC is fond"), //
62             @ApiResponse(code = 404, message = "RIC is not fond") //
63         })
64     public ResponseEntity<String> getRic(
65         @RequestParam(name = "managedElementId", required = false, defaultValue = "") String managedElementId) {
66
67         Optional<RicConfig> config = appConfig.lookupRicConfigForManagedElement(managedElementId);
68
69         if (config.isPresent()) {
70             return new ResponseEntity<>(config.get().name(), HttpStatus.OK);
71         } else {
72             return new ResponseEntity<>("", HttpStatus.NOT_FOUND);
73         }
74     }
75
76     /**
77      * @return a Json array of all RIC data
78      *         Example: http://localhost:8080/ric
79      */
80     @GetMapping("/rics")
81     @ApiOperation(value = "Returns defined NearRT RIC:s as Json")
82     @ApiResponses(
83         value = { //
84             @ApiResponse(code = 200, message = "OK") //
85         })
86     public ResponseEntity<String> getRics() {
87         Vector<RicInfo> result = new Vector<RicInfo>();
88         for (RicConfig ricConfig : this.appConfig.getRicConfigs()) {
89             RicInfo ric = ImmutableRicInfo.builder() //
90                 .managedElementIds(ricConfig.managedElementIds()) //
91                 .name(ricConfig.name()) //
92                 .build();
93             result.add(ric);
94         }
95         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
96     }
97
98 }