5de8c12044e1c5b14b6e04bcffb612f4fa1f6ac4
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Bell Canada
4  *  Modifications Copyright (C) 2022-2025 OpenInfra Foundation Europe. All rights reserved.
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         description = "Search for cm handle ids within CPS-NCMP-I-01 interface")
66     public ResponseEntity<List<String>> searchCmHandleIds(final CmHandleQueryParameters cmHandleQueryParameters,
67                                                           final Boolean outputAlternateId) {
68         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = ncmpRestInputMapper
69                 .toCmHandleQueryServiceParameters(cmHandleQueryParameters);
70
71         final Collection<String> cmHandleIds = networkCmProxyInventoryFacade
72                 .executeParameterizedCmHandleIdSearch(cmHandleQueryServiceParameters, outputAlternateId);
73         return ResponseEntity.ok(List.copyOf(cmHandleIds));
74     }
75
76     /**
77      * Get all cm-handle IDs under a registered DMI plugin.
78      *
79      * @param dmiPluginIdentifier DMI plugin identifier
80      * @param outputAlternateId   Boolean for cm handle reference type either
81      *                            cm handle id (False) or alternate id (True)
82      * @return list of cm handle IDs
83      */
84     @Override
85     public ResponseEntity<List<String>> getAllCmHandleReferencesForRegisteredDmi(final String dmiPluginIdentifier,
86                                                                                  final Boolean outputAlternateId) {
87
88         final Collection<String> cmHandleIds =
89             networkCmProxyInventoryFacade.getAllCmHandleReferencesByDmiPluginIdentifier(dmiPluginIdentifier,
90                 outputAlternateId);
91         return ResponseEntity.ok(List.copyOf(cmHandleIds));
92     }
93
94     /**
95      * Update DMI Plugin Registration (used for first registration also).
96      *
97      * @param restDmiPluginRegistration the registration data
98      */
99     @Override
100     @Timed(value = "cps.ncmp.inventory.controller.update",
101         description = "Time taken to handle registration request")
102     public ResponseEntity updateDmiPluginRegistration(
103         final @Valid RestDmiPluginRegistration restDmiPluginRegistration) {
104         final DmiPluginRegistrationResponse dmiPluginRegistrationResponse =
105             networkCmProxyInventoryFacade.updateDmiRegistration(
106                 ncmpRestInputMapper.toDmiPluginRegistration(restDmiPluginRegistration));
107         final DmiPluginRegistrationErrorResponse failedRegistrationErrorResponse =
108             getFailureRegistrationResponse(dmiPluginRegistrationResponse);
109         return allRegistrationsSuccessful(failedRegistrationErrorResponse)
110             ? new ResponseEntity<>(HttpStatus.OK)
111             : new ResponseEntity<>(failedRegistrationErrorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
112     }
113
114     private boolean allRegistrationsSuccessful(
115         final DmiPluginRegistrationErrorResponse dmiPluginRegistrationErrorResponse) {
116         return dmiPluginRegistrationErrorResponse.getFailedCreatedCmHandles().isEmpty()
117                 && dmiPluginRegistrationErrorResponse.getFailedUpdatedCmHandles().isEmpty()
118                 && dmiPluginRegistrationErrorResponse.getFailedRemovedCmHandles().isEmpty()
119                 && dmiPluginRegistrationErrorResponse.getFailedUpgradeCmHandles().isEmpty();
120     }
121
122     private DmiPluginRegistrationErrorResponse getFailureRegistrationResponse(
123         final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
124         final DmiPluginRegistrationErrorResponse dmiPluginRegistrationErrorResponse =
125             new DmiPluginRegistrationErrorResponse();
126         dmiPluginRegistrationErrorResponse.setFailedCreatedCmHandles(
127             getFailedResponses(dmiPluginRegistrationResponse.getCreatedCmHandles()));
128         dmiPluginRegistrationErrorResponse.setFailedUpdatedCmHandles(
129             getFailedResponses(dmiPluginRegistrationResponse.getUpdatedCmHandles()));
130         dmiPluginRegistrationErrorResponse.setFailedRemovedCmHandles(
131             getFailedResponses(dmiPluginRegistrationResponse.getRemovedCmHandles()));
132         dmiPluginRegistrationErrorResponse.setFailedUpgradeCmHandles(
133                 getFailedResponses(dmiPluginRegistrationResponse.getUpgradedCmHandles()));
134         return dmiPluginRegistrationErrorResponse;
135     }
136
137     private List<CmHandlerRegistrationErrorResponse> getFailedResponses(
138             final List<CmHandleRegistrationResponse> cmHandleRegistrationResponseList) {
139         return cmHandleRegistrationResponseList.stream()
140                 .filter(cmHandleRegistrationResponse -> cmHandleRegistrationResponse.getStatus() == Status.FAILURE)
141                 .map(this::toCmHandleRegistrationErrorResponse).collect(Collectors.toList());
142     }
143
144     private CmHandlerRegistrationErrorResponse toCmHandleRegistrationErrorResponse(
145         final CmHandleRegistrationResponse registrationResponse) {
146         return new CmHandlerRegistrationErrorResponse()
147             .cmHandle(registrationResponse.getCmHandle())
148             .errorCode(registrationResponse.getNcmpResponseStatus().getCode())
149             .errorText(registrationResponse.getErrorText());
150     }
151
152 }