a0987ada4ecc93ceb0f43da13b93e5fb4e44867a
[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.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 import java.util.Optional;
35
36 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
37 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
38 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
39 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.MediaType;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.RequestParam;
46 import org.springframework.web.bind.annotation.RestController;
47
48 @RestController("RicRepositoryControllerV2")
49 @Api(tags = Consts.V2_API_NAME)
50 public class RicRepositoryController {
51
52     @Autowired
53     private Rics rics;
54
55     @Autowired
56     PolicyTypes types;
57
58     private static Gson gson = new GsonBuilder() //
59         .serializeNulls() //
60         .create(); //
61
62     /**
63      * Example: http://localhost:8081/v2/ric?managed_element_id=kista_1
64      */
65     @GetMapping(path = Consts.V2_API_ROOT + "/ric", produces = MediaType.APPLICATION_JSON_VALUE)
66     @ApiOperation(value = "Returns info for the NearRT-RIC with the given identity or managing one Mananged Element")
67     @ApiResponses(
68         value = { //
69             @ApiResponse(code = 200, message = "NearRT-RIC is found", response = RicInfo.class), //
70             @ApiResponse(code = 404, message = "NearRT-RIC is not found", response = ErrorResponse.ErrorInfo.class) //
71         })
72     public ResponseEntity<Object> getRic( //
73         @ApiParam(
74             name = Consts.MANAGED_ELEMENT_ID_PARAM,
75             required = false,
76             value = "The identity of a Managed Element. If given, the NearRT-RIC managing the ME is returned.") //
77         @RequestParam(name = Consts.MANAGED_ELEMENT_ID_PARAM, required = false) String managedElementId,
78         @ApiParam(
79             name = Consts.RIC_ID_PARAM,
80             required = false,
81             value = "The identity of a NearRT-RIC to get information for.") //
82         @RequestParam(name = Consts.RIC_ID_PARAM, required = false) String ricId) {
83         try {
84             if (managedElementId != null && ricId != null) {
85                 return ErrorResponse.create("Give one query parameter", HttpStatus.BAD_REQUEST);
86             } else if (managedElementId != null) {
87                 Optional<Ric> ric = this.rics.lookupRicForManagedElement(managedElementId);
88                 if (ric.isPresent()) {
89                     return new ResponseEntity<>(gson.toJson(toRicInfo(ric.get())), HttpStatus.OK);
90                 } else {
91                     return ErrorResponse.create("No NearRT-RIC managing the ME is found", HttpStatus.NOT_FOUND);
92                 }
93             } else if (ricId != null) {
94                 RicInfo info = toRicInfo(this.rics.getRic(ricId));
95                 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
96             } else {
97                 return ErrorResponse.create("Give one query parameter", HttpStatus.BAD_REQUEST);
98             }
99         } catch (ServiceException e) {
100             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
101         }
102     }
103
104     /**
105      * @return a Json array of all RIC data Example: http://localhost:8081/v2/ric
106      */
107     @GetMapping(path = Consts.V2_API_ROOT + "/rics", produces = MediaType.APPLICATION_JSON_VALUE)
108     @ApiOperation(value = "Query NearRT-RIC information")
109     @ApiResponses(
110         value = { //
111             @ApiResponse(code = 200, message = "OK", response = RicInfoList.class), //
112             @ApiResponse(code = 404, message = "Policy type is not found", response = ErrorResponse.ErrorInfo.class)})
113     public ResponseEntity<Object> getRics( //
114         @ApiParam(
115             name = Consts.POLICY_TYPE_ID_PARAM,
116             required = false,
117             value = "The identity of a policy type. If given, all NearRT-RICs supporteing the policy type are returned") //
118         @RequestParam(name = Consts.POLICY_TYPE_ID_PARAM, required = false) String supportingPolicyType) {
119         if ((supportingPolicyType != null) && (this.types.get(supportingPolicyType) == null)) {
120             return ErrorResponse.create("Policy type not found", HttpStatus.NOT_FOUND);
121         }
122
123         List<RicInfo> result = new ArrayList<>();
124         for (Ric ric : rics.getRics()) {
125             if (supportingPolicyType == null || ric.isSupportingType(supportingPolicyType)) {
126                 result.add(toRicInfo(ric));
127             }
128         }
129
130         return new ResponseEntity<>(gson.toJson(new RicInfoList(result)), HttpStatus.OK);
131     }
132
133     private RicInfo.RicState toRicState(Ric.RicState state) {
134         if (state == Ric.RicState.AVAILABLE) {
135             return RicInfo.RicState.AVAILABLE;
136         } else if (state == Ric.RicState.CONSISTENCY_CHECK) {
137             return RicInfo.RicState.CONSISTENCY_CHECK;
138         } else if (state == Ric.RicState.SYNCHRONIZING) {
139             return RicInfo.RicState.SYNCHRONIZING;
140         } else if (state == Ric.RicState.UNAVAILABLE) {
141             return RicInfo.RicState.UNAVAILABLE;
142         }
143         return RicInfo.RicState.UNAVAILABLE;
144     }
145
146     private RicInfo toRicInfo(Ric ric) {
147         return new RicInfo(ric.id(), ric.getManagedElementIds(), ric.getSupportedPolicyTypeNames(),
148             toRicState(ric.getState()));
149     }
150 }