224935e3b5c459e07640bbafc335afcdc06321ff
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
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.onap.ccsdk.oran.a1policymanagementservice.controllers.v1;
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.ApiParam;
29 import io.swagger.annotations.ApiResponse;
30 import io.swagger.annotations.ApiResponses;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException;
36 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
37 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
38 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.RequestParam;
44 import org.springframework.web.bind.annotation.RestController;
45
46 @RestController
47 @Api(tags = Consts.V1_API_NAME)
48 public class RicRepositoryController {
49
50     @Autowired
51     private Rics rics;
52
53     @Autowired
54     PolicyTypes types;
55
56     private static Gson gson = new GsonBuilder() //
57             .create(); //
58
59     /**
60      * Example: http://localhost:8081/rics?managedElementId=kista_1
61      * 
62      * @throws EntityNotFoundException
63      */
64     @GetMapping("/ric")
65     @ApiOperation(value = "Returns the name of a RIC managing one Mananged Element")
66     @ApiResponses(value = { //
67             @ApiResponse(code = 200, message = "Near-RT RIC is found", response = String.class), //
68             @ApiResponse(code = 404, message = "Near-RT RIC is not found", response = String.class) //
69     })
70     public ResponseEntity<String> getRic( //
71             @ApiParam(name = "managedElementId", required = true, value = "The identity of the Managed Element") //
72             @RequestParam(name = "managedElementId", required = true) String managedElementId)
73             throws EntityNotFoundException {
74         Ric ric = this.rics.lookupRicForManagedElement(managedElementId);
75         return new ResponseEntity<>(ric.id(), HttpStatus.OK);
76     }
77
78     /**
79      * @return a Json array of all RIC data Example: http://localhost:8081/ric
80      */
81     @GetMapping("/rics")
82     @ApiOperation(value = "Query Near-RT RIC information")
83     @ApiResponses(value = { //
84             @ApiResponse(code = 200, message = "OK", response = RicInfo.class, responseContainer = "List"), //
85             @ApiResponse(code = 404, message = "Policy type is not found", response = String.class)})
86     public ResponseEntity<String> getRics( //
87             @ApiParam(name = "policyType", required = false, value = "The name of the policy type") //
88             @RequestParam(name = "policyType", required = false) String supportingPolicyType) {
89         if ((supportingPolicyType != null) && (this.types.get(supportingPolicyType) == null)) {
90             return new ResponseEntity<>("Policy type not found", HttpStatus.NOT_FOUND);
91         }
92
93         List<RicInfo> result = new ArrayList<>();
94         for (Ric ric : rics.getRics()) {
95             if (supportingPolicyType == null || ric.isSupportingType(supportingPolicyType)) {
96                 result.add(new RicInfo(ric.id(), ric.getManagedElementIds(), ric.getSupportedPolicyTypeNames(),
97                         ric.getState().toString()));
98             }
99         }
100
101         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
102     }
103
104 }