e412107753089bac409d017ad86e245b61bedd40
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Bell Canada
4  *  Modifications Copyright (C) 2022-2025 Nordix Foundation
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.rest.controller;
23
24 import io.micrometer.core.annotation.Timed;
25 import jakarta.validation.Valid;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.cps.ncmp.api.inventory.NetworkCmProxyInventoryFacade;
31 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryServiceParameters;
32 import org.onap.cps.ncmp.api.inventory.models.CmHandleRegistrationResponse;
33 import org.onap.cps.ncmp.api.inventory.models.CmHandleRegistrationResponse.Status;
34 import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistrationResponse;
35 import org.onap.cps.ncmp.rest.api.NetworkCmProxyInventoryApi;
36 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
37 import org.onap.cps.ncmp.rest.model.CmHandlerRegistrationErrorResponse;
38 import org.onap.cps.ncmp.rest.model.DmiPluginRegistrationErrorResponse;
39 import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration;
40 import org.onap.cps.ncmp.rest.util.CountCmHandleSearchExecution;
41 import org.onap.cps.ncmp.rest.util.NcmpRestInputMapper;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RestController;
46
47 @RestController
48 @RequestMapping("${rest.api.ncmp-inventory-base-path}")
49 @RequiredArgsConstructor
50 public class NetworkCmProxyInventoryController implements NetworkCmProxyInventoryApi {
51
52     private final NetworkCmProxyInventoryFacade networkCmProxyInventoryFacade;
53     private final NcmpRestInputMapper ncmpRestInputMapper;
54
55     /**
56      * Get all cm handle references under a registered DMI plugin.
57      *
58      * @param cmHandleQueryParameters DMI plugin identifier
59      * @param outputAlternateId       Boolean for cm handle reference type either
60      *                                cm handle id (False) or alternate id (True)
61      * @return                        list of cm handle IDs
62      */
63     @Override
64     @CountCmHandleSearchExecution(methodName = "searchCmHandleIds", interfaceName = "CPS-NCMP-I-01")
65     public ResponseEntity<List<String>> searchCmHandleIds(final CmHandleQueryParameters cmHandleQueryParameters,
66                                                           final Boolean outputAlternateId) {
67         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = ncmpRestInputMapper
68                 .toCmHandleQueryServiceParameters(cmHandleQueryParameters);
69
70         final Collection<String> cmHandleIds = networkCmProxyInventoryFacade
71                 .executeParameterizedCmHandleIdSearch(cmHandleQueryServiceParameters, outputAlternateId);
72         return ResponseEntity.ok(List.copyOf(cmHandleIds));
73     }
74
75     /**
76      * Get all cm-handle IDs under a registered DMI plugin.
77      *
78      * @param dmiPluginIdentifier DMI plugin identifier
79      * @param outputAlternateId   Boolean for cm handle reference type either
80      *                            cm handle id (False) or alternate id (True)
81      * @return list of cm handle IDs
82      */
83     @Override
84     public ResponseEntity<List<String>> getAllCmHandleReferencesForRegisteredDmi(final String dmiPluginIdentifier,
85                                                                                  final Boolean outputAlternateId) {
86
87         final Collection<String> cmHandleIds =
88             networkCmProxyInventoryFacade.getAllCmHandleReferencesByDmiPluginIdentifier(dmiPluginIdentifier,
89                 outputAlternateId);
90         return ResponseEntity.ok(List.copyOf(cmHandleIds));
91     }
92
93     /**
94      * Update DMI Plugin Registration (used for first registration also).
95      *
96      * @param restDmiPluginRegistration the registration data
97      */
98     @Override
99     @Timed(value = "cps.ncmp.inventory.controller.update",
100         description = "Time taken to handle registration request")
101     public ResponseEntity updateDmiPluginRegistration(
102         final @Valid RestDmiPluginRegistration restDmiPluginRegistration) {
103         final DmiPluginRegistrationResponse dmiPluginRegistrationResponse =
104             networkCmProxyInventoryFacade.updateDmiRegistration(
105                 ncmpRestInputMapper.toDmiPluginRegistration(restDmiPluginRegistration));
106         final DmiPluginRegistrationErrorResponse failedRegistrationErrorResponse =
107             getFailureRegistrationResponse(dmiPluginRegistrationResponse);
108         return allRegistrationsSuccessful(failedRegistrationErrorResponse)
109             ? new ResponseEntity<>(HttpStatus.OK)
110             : new ResponseEntity<>(failedRegistrationErrorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
111     }
112
113     private boolean allRegistrationsSuccessful(
114         final DmiPluginRegistrationErrorResponse dmiPluginRegistrationErrorResponse) {
115         return dmiPluginRegistrationErrorResponse.getFailedCreatedCmHandles().isEmpty()
116                 && dmiPluginRegistrationErrorResponse.getFailedUpdatedCmHandles().isEmpty()
117                 && dmiPluginRegistrationErrorResponse.getFailedRemovedCmHandles().isEmpty()
118                 && dmiPluginRegistrationErrorResponse.getFailedUpgradeCmHandles().isEmpty();
119     }
120
121     private DmiPluginRegistrationErrorResponse getFailureRegistrationResponse(
122         final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
123         final DmiPluginRegistrationErrorResponse dmiPluginRegistrationErrorResponse =
124             new DmiPluginRegistrationErrorResponse();
125         dmiPluginRegistrationErrorResponse.setFailedCreatedCmHandles(
126             getFailedResponses(dmiPluginRegistrationResponse.getCreatedCmHandles()));
127         dmiPluginRegistrationErrorResponse.setFailedUpdatedCmHandles(
128             getFailedResponses(dmiPluginRegistrationResponse.getUpdatedCmHandles()));
129         dmiPluginRegistrationErrorResponse.setFailedRemovedCmHandles(
130             getFailedResponses(dmiPluginRegistrationResponse.getRemovedCmHandles()));
131         dmiPluginRegistrationErrorResponse.setFailedUpgradeCmHandles(
132                 getFailedResponses(dmiPluginRegistrationResponse.getUpgradedCmHandles()));
133         return dmiPluginRegistrationErrorResponse;
134     }
135
136     private List<CmHandlerRegistrationErrorResponse> getFailedResponses(
137             final List<CmHandleRegistrationResponse> cmHandleRegistrationResponseList) {
138         return cmHandleRegistrationResponseList.stream()
139                 .filter(cmHandleRegistrationResponse -> cmHandleRegistrationResponse.getStatus() == Status.FAILURE)
140                 .map(this::toCmHandleRegistrationErrorResponse).collect(Collectors.toList());
141     }
142
143     private CmHandlerRegistrationErrorResponse toCmHandleRegistrationErrorResponse(
144         final CmHandleRegistrationResponse registrationResponse) {
145         return new CmHandlerRegistrationErrorResponse()
146             .cmHandle(registrationResponse.getCmHandle())
147             .errorCode(registrationResponse.getNcmpResponseStatus().getCode())
148             .errorText(registrationResponse.getErrorText());
149     }
150
151 }