Fix sonar warnings in config-util.
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / strategy / EditStrategyType.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.config.facade.xml.strategy;
10
11 import java.util.EnumSet;
12 import java.util.Set;
13 import org.opendaylight.controller.config.facade.xml.exception.OperationNotPermittedException;
14 import org.opendaylight.controller.config.util.xml.DocumentedException;
15
16 public enum EditStrategyType {
17     // can be default
18     merge, replace, none,
19     // additional per element
20     delete, remove, recreate;
21
22     private static final Set<EditStrategyType> defaultStrats = EnumSet.of(merge, replace, none);
23
24     public static EditStrategyType getDefaultStrategy() {
25         return merge;
26     }
27
28     public boolean isEnforcing() {
29         switch (this) {
30         case merge:
31         case none:
32         case remove:
33         case delete:
34         case recreate:
35             return false;
36         case replace:
37             return true;
38
39         default:
40             throw new IllegalStateException("Default edit strategy can be only of value " + defaultStrats + " but was "
41                     + this);
42         }
43     }
44     public static void compareParsedStrategyToDefaultEnforcing(EditStrategyType parsedStrategy,
45                                                                   EditStrategyType defaultStrategy) throws OperationNotPermittedException {
46         if (defaultStrategy.isEnforcing()) {
47             if (parsedStrategy != defaultStrategy){
48                 throw new OperationNotPermittedException(String.format("With "
49                         + defaultStrategy
50                         + " as default-operation operations on module elements are not permitted since the default option is restrictive"),
51                         DocumentedException.ErrorType.APPLICATION,
52                         DocumentedException.ErrorTag.OPERATION_FAILED,
53                         DocumentedException.ErrorSeverity.ERROR);
54             }
55         }
56
57     }
58     public EditConfigStrategy getFittingStrategy() {
59         switch (this) {
60         case merge:
61             return new MergeEditConfigStrategy();
62         case replace:
63             return new ReplaceEditConfigStrategy();
64         case delete:
65             return new DeleteEditConfigStrategy();
66         case remove:
67             return new RemoveEditConfigStrategy();
68         case recreate:
69             return new ReCreateEditConfigStrategy();
70         case none:
71             return new NoneEditConfigStrategy();
72         default:
73             throw new UnsupportedOperationException("Unimplemented edit config strategy" + this);
74         }
75     }
76 }