Merge "Running Dmaap consumer in a seprate thread"
[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.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30
31 import java.util.Optional;
32 import java.util.Vector;
33
34 import org.oransc.policyagent.configuration.ApplicationConfig;
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 @Api(value = "RIC Management API")
46 public class RicRepositoryController {
47
48     @Autowired
49     private Rics rics;
50
51     private static Gson gson = new GsonBuilder() //
52         .serializeNulls() //
53         .create(); //
54
55     @Autowired
56     RicRepositoryController(ApplicationConfig appConfig) {
57     }
58
59     /**
60      * Example: http://localhost:8080/rics?managedElementId=kista_1
61      */
62     @GetMapping("/ric")
63     @ApiOperation(value = "Returns the name of a RIC managing one Mananged Element")
64     @ApiResponses(
65         value = { //
66             @ApiResponse(code = 200, message = "RIC is fond", response = String.class), //
67             @ApiResponse(code = 404, message = "RIC is not fond", response = String.class) //
68         })
69     public ResponseEntity<String> getRic(
70         @RequestParam(name = "managedElementId", required = false, defaultValue = "") String managedElementId) {
71
72         Optional<Ric> ric = this.rics.lookupRicForManagedElement(managedElementId);
73
74         if (ric.isPresent()) {
75             return new ResponseEntity<>(ric.get().name(), HttpStatus.OK);
76         } else {
77             return new ResponseEntity<>("", HttpStatus.NOT_FOUND);
78         }
79     }
80
81     /**
82      * @return a Json array of all RIC data
83      *         Example: http://localhost:8080/ric
84      */
85     @GetMapping("/rics")
86     @ApiOperation(value = "Query NearRT RIC information")
87     @ApiResponses(
88         value = { //
89             @ApiResponse(code = 200, message = "OK", response = RicInfo.class, responseContainer = "List") //
90         })
91     public ResponseEntity<String> getRics(
92         @RequestParam(name = "policyType", required = false) String supportingPolicyType) {
93
94         Vector<RicInfo> result = new Vector<>();
95         synchronized (rics) {
96             for (Ric ric : rics.getRics()) {
97                 if (supportingPolicyType == null || ric.isSupportingType(supportingPolicyType)) {
98                     result.add(new RicInfo(ric.name(), ric.getManagedElementIds(), ric.getSupportedPolicyTypeNames()));
99                 }
100             }
101         }
102
103         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
104     }
105
106 }