Merge "Minor pom updates"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / controllers / PolicyController.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.Vector;
30
31 import org.oransc.policyagent.configuration.ApplicationConfig;
32 import org.oransc.policyagent.configuration.RicConfig;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.RequestParam;
38 import org.springframework.web.bind.annotation.RestController;
39 import reactor.core.publisher.Mono;
40
41 @RestController
42 public class PolicyController {
43
44     private final ApplicationConfig appConfig;
45     private static Gson gson = new GsonBuilder() //
46         .serializeNulls() //
47         .create(); //
48
49     @Autowired
50     PolicyController(ApplicationConfig config) {
51         this.appConfig = config;
52     }
53
54     // http://localhost:8080/policy?type=type3&instance=xxx
55     @GetMapping("/policy")
56     public String getPolicy(@RequestParam(name = "type", required = false, defaultValue = "type1") String typeName,
57         @RequestParam(name = "instance", required = false, defaultValue = "new") String instanceId) {
58         System.out.println("**** getPolicy " + typeName);
59
60         return "policy" + typeName + instanceId;
61     }
62
63     public String getHello() {
64         return "Howdy";
65     }
66
67     @GetMapping("/status")
68     @ApiOperation(value = "Returns status and statistics of the service")
69     @ApiResponses(
70         value = { //
71             @ApiResponse(code = 200, message = "DATAFILE service is living"),
72             @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
73             @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
74             @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") //
75         })
76     public Mono<ResponseEntity<String>> getStatus() {
77         Mono<ResponseEntity<String>> response = Mono.just(new ResponseEntity<>("hunky dory", HttpStatus.OK));
78         return response;
79     }
80
81     // http://localhost:8080/rics?managedElementId=kista_1
82     @GetMapping("/rics")
83     @ApiOperation(value = "Returns defined NearRT RIC:s")
84     public ResponseEntity<String> getRics(
85         @RequestParam(name = "managedElementId", required = false, defaultValue = "") String managedElementId) {
86         Vector<RicInfo> result = new Vector<RicInfo>();
87         Vector<RicConfig> config = getRicConfigs(managedElementId);
88
89         for (RicConfig ricConfig : config) {
90             RicInfo ric = ImmutableRicInfo.builder() //
91                 .managedElementIds(ricConfig.managedElementIds()) //
92                 .name(ricConfig.name()) //
93                 .build();
94             result.add(ric);
95         }
96
97         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
98     }
99
100     private Vector<RicConfig> getRicConfigs(String managedElementId) {
101         if (managedElementId.equals("")) {
102             return this.appConfig.getRicConfigs();
103         }
104
105         Vector<RicConfig> result = new Vector<RicConfig>(1);
106         appConfig.getRicConfig(managedElementId).ifPresent((config) -> {
107             result.add(config);
108         });
109         return result;
110     }
111
112 }