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