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