Merge "Add Error Handling in A1 Client"
[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
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.util.Optional;
31 import java.util.Vector;
32
33 import org.oransc.policyagent.configuration.ApplicationConfig;
34 import org.oransc.policyagent.configuration.RicConfig;
35 import org.oransc.policyagent.repository.Ric;
36 import org.oransc.policyagent.repository.Rics;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.RequestParam;
42 import org.springframework.web.bind.annotation.RestController;
43
44 @RestController
45 public class RicRepositoryController {
46
47     private final ApplicationConfig appConfig;
48
49     @Autowired
50     private Rics rics;
51
52     private static Gson gson = new GsonBuilder() //
53         .serializeNulls() //
54         .create(); //
55
56     @Autowired
57     RicRepositoryController(ApplicationConfig appConfig) {
58         this.appConfig = appConfig;
59     }
60
61     /**
62      * Example: http://localhost:8080/rics?managedElementId=kista_1
63      */
64     @GetMapping("/ric")
65     @ApiOperation(value = "Returns the name of a RIC managing one Mananged Element")
66     @ApiResponses(
67         value = { //
68             @ApiResponse(code = 200, message = "RIC is fond"), //
69             @ApiResponse(code = 404, message = "RIC is not fond") //
70         })
71     public ResponseEntity<String> getRic(
72         @RequestParam(name = "managedElementId", required = false, defaultValue = "") String managedElementId) {
73
74         Optional<RicConfig> config = appConfig.lookupRicConfigForManagedElement(managedElementId);
75
76         if (config.isPresent()) {
77             return new ResponseEntity<>(config.get().name(), HttpStatus.OK);
78         } else {
79             return new ResponseEntity<>("", HttpStatus.NOT_FOUND);
80         }
81     }
82
83     /**
84      * @return a Json array of all RIC data
85      *         Example: http://localhost:8080/ric
86      */
87     @GetMapping("/rics")
88     @ApiOperation(value = "Returns defined NearRT RIC:s as Json")
89     @ApiResponses(
90         value = { //
91             @ApiResponse(code = 200, message = "OK") //
92         })
93     public ResponseEntity<String> getRics() {
94         Vector<RicInfo> result = new Vector<>();
95         for (Ric ric : rics.getRics()) {
96             result.add(ImmutableRicInfo.builder() //
97                 .name(ric.name()) //
98                 .nodeNames(ric.getManagedNodes()) //
99                 .build());
100         }
101
102         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
103     }
104
105 }