e007491ce0806c7159ae74ad981c5ae4a17839c9
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / models / CmHandleRegistrationResponse.java
1 /*
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
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.api.models;
23
24 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNKNOWN_ERROR;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29 import lombok.Builder;
30 import lombok.Data;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.ncmp.api.NcmpResponseStatus;
33 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
34
35 @Data
36 @Builder
37 @Slf4j
38 public class CmHandleRegistrationResponse {
39
40     private final String cmHandle;
41     private final Status status;
42     private NcmpResponseStatus ncmpResponseStatus;
43     private String errorText;
44
45     /**
46      * Creates a failure response based on exception.
47      *
48      * @param cmHandleId  cmHandleId
49      * @param exception exception
50      * @return CmHandleRegistrationResponse
51      */
52     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
53                                                                      final Exception exception) {
54         return CmHandleRegistrationResponse.builder()
55             .cmHandle(cmHandleId)
56             .status(Status.FAILURE)
57             .ncmpResponseStatus(UNKNOWN_ERROR)
58             .errorText(exception.getMessage()).build();
59     }
60
61     /**
62      * Creates a failure response based on registration error.
63      *
64      * @param cmHandleId          cmHandleId
65      * @param ncmpResponseStatus registration error code and status
66      * @return CmHandleRegistrationResponse
67      */
68     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
69         final NcmpResponseStatus ncmpResponseStatus) {
70         return CmHandleRegistrationResponse.builder().cmHandle(cmHandleId)
71             .status(Status.FAILURE)
72             .ncmpResponseStatus(ncmpResponseStatus)
73             .errorText(ncmpResponseStatus.getMessage())
74             .build();
75     }
76
77     /**
78      * Creates a failure response based on registration error.
79      *
80      * @param failedXpaths       list of failed Xpaths
81      * @param ncmpResponseStatus enum describing the type of registration error
82      * @return CmHandleRegistrationResponse
83      */
84     public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> failedXpaths,
85             final NcmpResponseStatus ncmpResponseStatus) {
86         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failedXpaths.size());
87         for (final String xpath : failedXpaths) {
88             try {
89                 final String cmHandleId = YangDataConverter.extractCmHandleIdFromXpath(xpath);
90                 cmHandleRegistrationResponses.add(
91                         CmHandleRegistrationResponse.createFailureResponse(cmHandleId, ncmpResponseStatus));
92             } catch (IllegalArgumentException | IllegalStateException e) {
93                 log.warn("Unexpected xpath {}", xpath);
94             }
95         }
96         return cmHandleRegistrationResponses;
97     }
98
99     /**
100      * Creates a failure response based on other exception.
101      *
102      * @param cmHandleIds list of failed cmHandleIds
103      * @param exception   exception caught during the registration
104      * @return CmHandleRegistrationResponse
105      */
106     public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds,
107             final Exception exception) {
108         return cmHandleIds.stream()
109                 .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception))
110                 .toList();
111     }
112
113     public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
114         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
115             .status(Status.SUCCESS).build();
116     }
117
118     public static List<CmHandleRegistrationResponse> createSuccessResponses(final List<String> cmHandleIds) {
119         return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse).toList();
120     }
121
122     public enum Status {
123         SUCCESS, FAILURE;
124     }
125 }