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