def388accc38439d8f1d9a0a16a444ca12a3d955
[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.v2;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import io.swagger.v3.oas.annotations.Operation;
27 import io.swagger.v3.oas.annotations.Parameter;
28 import io.swagger.v3.oas.annotations.media.Content;
29 import io.swagger.v3.oas.annotations.media.Schema;
30 import io.swagger.v3.oas.annotations.responses.ApiResponse;
31 import io.swagger.v3.oas.annotations.responses.ApiResponses;
32 import io.swagger.v3.oas.annotations.tags.Tag;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException;
38 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.InvalidRequestException;
39 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
40 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
41 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.MediaType;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.bind.annotation.GetMapping;
47 import org.springframework.web.bind.annotation.RequestParam;
48 import org.springframework.web.bind.annotation.RestController;
49
50 @RestController("RicRepositoryControllerV2")
51 @Tag( //
52         name = RicRepositoryController.API_NAME, //
53         description = RicRepositoryController.API_DESCRIPTION //
54 )
55 public class RicRepositoryController {
56
57     public static final String API_NAME = "NearRT-RIC Repository";
58     public static final String API_DESCRIPTION = "";
59
60     @Autowired
61     private Rics rics;
62
63     @Autowired
64     PolicyTypes types;
65
66     private static Gson gson = new GsonBuilder() //
67             .create(); //
68
69     private static final String GET_RIC_BRIEF = "Returns info for one Near-RT RIC";
70     private static final String GET_RIC_DETAILS =
71             "Either a Near-RT RIC identity or a Mananged Element identity can be specified.<br>" //
72                     + "The intention with Mananged Element identity is the ID used in O1 for accessing the traffical element (such as the ID of CU).";
73
74     /**
75      * Example: http://localhost:8081/v2/rics/ric?managed_element_id=kista_1
76      *
77      * @throws EntityNotFoundException
78      */
79     @GetMapping(path = Consts.V2_API_ROOT + "/rics/ric", produces = MediaType.APPLICATION_JSON_VALUE)
80     @Operation(summary = GET_RIC_BRIEF, description = GET_RIC_DETAILS)
81     @ApiResponses(value = { //
82             @ApiResponse(responseCode = "200", //
83                     description = "Near-RT RIC is found", //
84                     content = @Content(schema = @Schema(implementation = RicInfo.class))), //
85             @ApiResponse(responseCode = "404", //
86                     description = "Near-RT RIC is not found", //
87                     content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
88     })
89     public ResponseEntity<Object> getRic( //
90             @Parameter(name = Consts.MANAGED_ELEMENT_ID_PARAM, required = false,
91                     description = "The identity of a Managed Element. If given, the Near-RT RIC managing the ME is returned.") //
92             @RequestParam(name = Consts.MANAGED_ELEMENT_ID_PARAM, required = false) String managedElementId,
93             @Parameter(name = Consts.RIC_ID_PARAM, required = false,
94                     description = "The identity of a Near-RT RIC to get information for.") //
95             @RequestParam(name = Consts.RIC_ID_PARAM, required = false) String ricId)
96             throws EntityNotFoundException, InvalidRequestException {
97         if (managedElementId != null && ricId != null) {
98             throw new InvalidRequestException("Give one query parameter");
99         } else if (managedElementId != null) {
100             Ric ric = this.rics.lookupRicForManagedElement(managedElementId);
101             return new ResponseEntity<>(gson.toJson(toRicInfo(ric)), HttpStatus.OK);
102         } else if (ricId != null) {
103             RicInfo info = toRicInfo(this.rics.getRic(ricId));
104             return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
105         } else {
106             throw new InvalidRequestException("Give one query parameter");
107         }
108     }
109
110     static final String QUERY_RIC_INFO_DETAILS =
111             "The call returns all Near-RT RICs that supports a given policy type identity";
112
113     /**
114      * @return a Json array of all RIC data Example: http://localhost:8081/v2/ric
115      * @throws EntityNotFoundException
116      */
117     @GetMapping(path = Consts.V2_API_ROOT + "/rics", produces = MediaType.APPLICATION_JSON_VALUE)
118     @Operation(summary = "Query Near-RT RIC information", description = QUERY_RIC_INFO_DETAILS)
119     @ApiResponses(value = { //
120             @ApiResponse(responseCode = "200", //
121                     description = "OK", //
122                     content = @Content(schema = @Schema(implementation = RicInfoList.class))), //
123             @ApiResponse(responseCode = "404", //
124                     description = "Policy type is not found", //
125                     content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
126     })
127     public ResponseEntity<Object> getRics( //
128             @Parameter(name = Consts.POLICY_TYPE_ID_PARAM, required = false,
129                     description = "The identity of a policy type. If given, all Near-RT RICs supporting the policy type are returned") //
130             @RequestParam(name = Consts.POLICY_TYPE_ID_PARAM, required = false) String supportingPolicyType)
131             throws EntityNotFoundException {
132         if ((supportingPolicyType != null) && (this.types.get(supportingPolicyType) == null)) {
133             throw new EntityNotFoundException("Policy type not found");
134         }
135
136         List<RicInfo> result = new ArrayList<>();
137         for (Ric ric : rics.getRics()) {
138             if (supportingPolicyType == null || ric.isSupportingType(supportingPolicyType)) {
139                 result.add(toRicInfo(ric));
140             }
141         }
142
143         return new ResponseEntity<>(gson.toJson(new RicInfoList(result)), HttpStatus.OK);
144     }
145
146     private RicInfo.RicState toRicState(Ric.RicState state) {
147         switch (state) {
148             case AVAILABLE:
149                 return RicInfo.RicState.AVAILABLE;
150             case CONSISTENCY_CHECK:
151                 return RicInfo.RicState.CONSISTENCY_CHECK;
152             case SYNCHRONIZING:
153                 return RicInfo.RicState.SYNCHRONIZING;
154             case UNAVAILABLE:
155                 return RicInfo.RicState.UNAVAILABLE;
156             default:
157                 return RicInfo.RicState.UNAVAILABLE;
158         }
159     }
160
161     private RicInfo toRicInfo(Ric ric) {
162         return new RicInfo(ric.id(), ric.getManagedElementIds(), ric.getSupportedPolicyTypeNames(),
163                 toRicState(ric.getState()));
164     }
165 }