String performance and maintenability
[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, 2017 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> DEFAULT_STRATS = 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(
41                     "Default edit strategy can be only of value " + DEFAULT_STRATS + " but was " + this);
42         }
43     }
44
45     public static void compareParsedStrategyToDefaultEnforcing(EditStrategyType parsedStrategy,
46             EditStrategyType defaultStrategy) throws OperationNotPermittedException {
47         if (defaultStrategy.isEnforcing()) {
48             if (parsedStrategy != defaultStrategy) {
49                 throw new OperationNotPermittedException("With " + defaultStrategy
50                         + " as default-operation operations on module elements are not permitted since the default "
51                         + "option is restrictive",
52                         DocumentedException.ErrorType.APPLICATION,
53                         DocumentedException.ErrorTag.OPERATION_FAILED,
54                         DocumentedException.ErrorSeverity.ERROR);
55             }
56         }
57     }
58
59     public EditConfigStrategy getFittingStrategy() {
60         switch (this) {
61             case merge:
62                 return new MergeEditConfigStrategy();
63             case replace:
64                 return new ReplaceEditConfigStrategy();
65             case delete:
66                 return new DeleteEditConfigStrategy();
67             case remove:
68                 return new RemoveEditConfigStrategy();
69             case recreate:
70                 return new ReCreateEditConfigStrategy();
71             case none:
72                 return new NoneEditConfigStrategy();
73             default:
74                 throw new UnsupportedOperationException("Unimplemented edit config strategy" + this);
75         }
76     }
77 }