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