2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada
4 * Modifications Copyright (C) 2022-2023 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.models;
24 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNKNOWN_ERROR;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.stream.Collectors;
30 import lombok.Builder;
32 import lombok.extern.slf4j.Slf4j;
33 import org.onap.cps.ncmp.api.NcmpResponseStatus;
34 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
39 public class CmHandleRegistrationResponse {
41 private final String cmHandle;
42 private final Status status;
43 private NcmpResponseStatus ncmpResponseStatus;
44 private String errorText;
47 * Creates a failure response based on exception.
49 * @param cmHandleId cmHandleId
50 * @param exception exception
51 * @return CmHandleRegistrationResponse
53 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
54 final Exception exception) {
55 return CmHandleRegistrationResponse.builder()
57 .status(Status.FAILURE)
58 .ncmpResponseStatus(UNKNOWN_ERROR)
59 .errorText(exception.getMessage()).build();
63 * Creates a failure response based on registration error.
65 * @param cmHandleId cmHandleId
66 * @param ncmpResponseStatus registration error code and status
67 * @return CmHandleRegistrationResponse
69 public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
70 final NcmpResponseStatus ncmpResponseStatus) {
71 return CmHandleRegistrationResponse.builder().cmHandle(cmHandleId)
72 .status(Status.FAILURE)
73 .ncmpResponseStatus(ncmpResponseStatus)
74 .errorText(ncmpResponseStatus.getMessage())
79 * Creates a failure response based on registration error.
81 * @param failedXpaths list of failed Xpaths
82 * @param ncmpResponseStatus enum describing the type of registration error
83 * @return CmHandleRegistrationResponse
85 public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> failedXpaths,
86 final NcmpResponseStatus ncmpResponseStatus) {
87 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failedXpaths.size());
88 for (final String xpath : failedXpaths) {
90 final String cmHandleId = YangDataConverter.extractCmHandleIdFromXpath(xpath);
91 cmHandleRegistrationResponses.add(
92 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, ncmpResponseStatus));
93 } catch (IllegalArgumentException | IllegalStateException e) {
94 log.warn("Unexpected xpath {}", xpath);
97 return cmHandleRegistrationResponses;
101 * Creates a failure response based on other exception.
103 * @param cmHandleIds list of failed cmHandleIds
104 * @param exception exception caught during the registration
105 * @return CmHandleRegistrationResponse
107 public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds,
108 final Exception exception) {
109 return cmHandleIds.stream()
110 .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception))
114 public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
115 return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
116 .status(Status.SUCCESS).build();
119 public static List<CmHandleRegistrationResponse> createSuccessResponses(final Collection<String> cmHandleIds) {
120 return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse)
121 .collect(Collectors.toList());