Combine alreadyDefinedException classes
[cps.git] / cps-service / src / main / java / org / onap / cps / spi / exceptions / AlreadyDefinedException.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
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.spi.exceptions;
23
24 import java.util.Collection;
25 import java.util.Collections;
26 import lombok.Getter;
27
28 /**
29  * Already defined exception. Indicates the cps object with same name already exists.
30  */
31
32 @SuppressWarnings("squid:S110")  // Team agreed to accept 6 levels of inheritance for CPS Exceptions
33 public class AlreadyDefinedException extends CpsAdminException {
34
35     private static final long serialVersionUID = 501929839139881112L;
36     public static final String ALREADY_DEFINED_EXCEPTION_MESSAGE = "Already defined exception";
37
38     @Getter
39     private final Collection<String> alreadyDefinedObjectNames;
40
41     private AlreadyDefinedException(final String objectType, final String objectName, final String contextName,
42         final Throwable cause) {
43         super(ALREADY_DEFINED_EXCEPTION_MESSAGE,
44             String.format("%s with name %s already exists for %s.", objectType, objectName, contextName), cause);
45         alreadyDefinedObjectNames = Collections.singletonList(objectName);
46     }
47
48     private AlreadyDefinedException(final String objectType, final Collection<String> objectNames,
49                                     final String contextName) {
50         super(ALREADY_DEFINED_EXCEPTION_MESSAGE,
51                 String.format("%d %s already exist for %s.", objectNames.size(), objectType, contextName));
52         alreadyDefinedObjectNames = objectNames;
53     }
54
55     private AlreadyDefinedException(final String objectName, final Throwable cause) {
56         super(ALREADY_DEFINED_EXCEPTION_MESSAGE, String.format("%s already exists.", objectName), cause);
57         alreadyDefinedObjectNames = Collections.singletonList(objectName);
58     }
59
60     public static AlreadyDefinedException forDataspace(final String dataspaceName, final Throwable cause) {
61         return new AlreadyDefinedException(dataspaceName, cause);
62     }
63
64     public static AlreadyDefinedException forAnchor(final String anchorName, final String contextName,
65         final Throwable cause) {
66         return new AlreadyDefinedException("Anchor", anchorName, contextName, cause);
67     }
68
69     public static AlreadyDefinedException forSchemaSet(final String schemaSetName, final String contextName,
70         final Throwable cause) {
71         return new AlreadyDefinedException("Schema Set", schemaSetName, contextName, cause);
72     }
73
74     public static AlreadyDefinedException forDataNodes(final Collection<String> xpaths, final String contextName) {
75         return new AlreadyDefinedException("data node(s)", xpaths, contextName);
76     }
77 }