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
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.spi.exceptions;
24 import java.util.Collection;
27 * Already defined exception. Indicates the cps object with same name already exists.
30 @SuppressWarnings("squid:S110") // Team agreed to accept 6 levels of inheritance for CPS Exceptions
31 public class AlreadyDefinedException extends CpsAdminException {
33 private static final long serialVersionUID = 501929839139881112L;
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
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);
52 * @param objectName the name of the object
53 * @param cause the cause of the exception
55 private AlreadyDefinedException(final String objectName, final Throwable cause) {
56 super("Already defined exception", String.format("%s already exists.", objectName), cause);
59 public static AlreadyDefinedException forDataspace(final String dataspaceName, final Throwable cause) {
60 return new AlreadyDefinedException(dataspaceName, cause);
63 public static AlreadyDefinedException forAnchor(final String anchorName, final String contextName,
64 final Throwable cause) {
65 return new AlreadyDefinedException("Anchor", anchorName, contextName, cause);
68 public static AlreadyDefinedException forSchemaSet(final String schemaSetName, final String contextName,
69 final Throwable cause) {
70 return new AlreadyDefinedException("Schema Set", schemaSetName, contextName, cause);
73 public static AlreadyDefinedException forDataNode(final String xpath, final String contextName,
74 final Throwable cause) {
75 return new AlreadyDefinedException("Data node", xpath, contextName, cause);
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);