Editing of Nordix Licenses to ONAP guidelines
[cps.git] / cps-service / src / main / java / org / onap / cps / spi / exceptions / AlreadyDefinedException.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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
26 /**
27  * Already defined exception. Indicates the cps object with same name already exists.
28  */
29
30 @SuppressWarnings("squid:S110")  // Team agreed to accept 6 levels of inheritance for CPS Exceptions
31 public class AlreadyDefinedException extends CpsAdminException {
32
33     private static final long serialVersionUID = 501929839139881112L;
34
35     /**
36      * Constructor.
37      *
38      * @param objectType  the object type
39      * @param objectName  the name of the object
40      * @param contextName the context name e.g. Anchor or Dataspace
41      * @param cause       the cause of the exception
42      */
43     private AlreadyDefinedException(final String objectType, final String objectName, final String contextName,
44         final Throwable cause) {
45         super("Already defined exception",
46             String.format("%s with name %s already exists for %s.", objectType, objectName, contextName), cause);
47     }
48
49     /**
50      * Constructor.
51      *
52      * @param objectName the name of the object
53      * @param cause      the cause of the exception
54      */
55     private AlreadyDefinedException(final String objectName, final Throwable cause) {
56         super("Already defined exception", String.format("%s already exists.", objectName), cause);
57     }
58
59     public static AlreadyDefinedException forDataspace(final String dataspaceName, final Throwable cause) {
60         return new AlreadyDefinedException(dataspaceName, cause);
61     }
62
63     public static AlreadyDefinedException forAnchor(final String anchorName, final String contextName,
64         final Throwable cause) {
65         return new AlreadyDefinedException("Anchor", anchorName, contextName, cause);
66     }
67
68     public static AlreadyDefinedException forSchemaSet(final String schemaSetName, final String contextName,
69         final Throwable cause) {
70         return new AlreadyDefinedException("Schema Set", schemaSetName, contextName, cause);
71     }
72
73     public static AlreadyDefinedException forDataNode(final String xpath, final String contextName,
74         final Throwable cause) {
75         return new AlreadyDefinedException("Data node", xpath, contextName, cause);
76     }
77
78     public static AlreadyDefinedException forDataNodes(final Collection<String> xpaths, final String contextName,
79         final Throwable cause) {
80         final var name = String.format("(one or more) of %s", xpaths);
81         return new AlreadyDefinedException("Data node", name, contextName, cause);
82     }
83 }