Add memory usage to integration tests [UPDATED]
[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 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 java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import java.util.stream.Collectors;
30 import lombok.Builder;
31 import lombok.Data;
32 import lombok.RequiredArgsConstructor;
33 import lombok.extern.slf4j.Slf4j;
34
35 @Data
36 @Builder
37 @Slf4j
38 public class CmHandleRegistrationResponse {
39
40     private final String cmHandle;
41     private final Status status;
42     private RegistrationError registrationError;
43     private String errorText;
44
45     private static final Pattern cmHandleIdInXpathPattern = Pattern.compile("\\[@id='(.*?)']");
46
47     /**
48      * Creates a failure response based on exception.
49      *
50      * @param cmHandleId  cmHandleId
51      * @param exception exception
52      * @return CmHandleRegistrationResponse
53      */
54     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
55                                                                      final Exception exception) {
56         return CmHandleRegistrationResponse.builder()
57             .cmHandle(cmHandleId)
58             .status(Status.FAILURE)
59             .registrationError(RegistrationError.UNKNOWN_ERROR)
60             .errorText(exception.getMessage()).build();
61     }
62
63     /**
64      * Creates a failure response based on registration error.
65      *
66      * @param cmHandleId          cmHandleId
67      * @param registrationError registrationError
68      * @return CmHandleRegistrationResponse
69      */
70     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
71         final RegistrationError registrationError) {
72         return CmHandleRegistrationResponse.builder().cmHandle(cmHandleId)
73             .status(Status.FAILURE)
74             .registrationError(registrationError)
75             .errorText(registrationError.errorText)
76             .build();
77     }
78
79     /**
80      * Creates a failure response based on registration error.
81      *
82      * @param failedXpaths       list of failed Xpaths
83      * @param registrationError enum describing the type of registration error
84      * @return CmHandleRegistrationResponse
85      */
86     public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> failedXpaths,
87             final RegistrationError registrationError) {
88         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failedXpaths.size());
89         for (final String xpath : failedXpaths) {
90             final Matcher matcher = cmHandleIdInXpathPattern.matcher(xpath);
91             if (matcher.find()) {
92                 cmHandleRegistrationResponses.add(
93                     CmHandleRegistrationResponse.createFailureResponse(matcher.group(1), registrationError));
94             } else {
95                 log.warn("Unexpected xpath {}", xpath);
96             }
97         }
98         return cmHandleRegistrationResponses;
99     }
100
101     /**
102      * Creates a failure response based on other exception.
103      *
104      * @param cmHandleIds list of failed cmHandleIds
105      * @param exception   exception caught during the registration
106      * @return CmHandleRegistrationResponse
107      */
108     public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds,
109             final Exception exception) {
110         return cmHandleIds.stream()
111                 .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception))
112                 .collect(Collectors.toList());
113     }
114
115     public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
116         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
117             .status(Status.SUCCESS).build();
118     }
119
120     public static List<CmHandleRegistrationResponse> createSuccessResponses(final List<String> cmHandleIds) {
121         return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse)
122                 .collect(Collectors.toList());
123     }
124
125     public enum Status {
126         SUCCESS, FAILURE;
127     }
128
129     @RequiredArgsConstructor
130     public enum RegistrationError {
131         UNKNOWN_ERROR("00", "Unknown error"),
132         CM_HANDLE_ALREADY_EXIST("01", "cm-handle already exists"),
133         CM_HANDLE_DOES_NOT_EXIST("02", "cm-handle does not exist"),
134         CM_HANDLE_INVALID_ID("03", "cm-handle has an invalid character(s) in id");
135
136         public final String errorCode;
137         public final String errorText;
138
139     }
140 }