Merge "Bug 1029: Remove dead code: sal-schema-repository-api"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / editconfig / EditStrategyType.java
1 /*
2  * Copyright (c) 2013 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.netconf.confignetconfconnector.operations.editconfig;
10
11 import java.util.EnumSet;
12 import java.util.Set;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.confignetconfconnector.exception.OperationNotPermittedException;
15
16 public enum EditStrategyType {
17     // can be default
18     merge, replace, none,
19     // additional per element
20     delete, remove;
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             return false;
35         case replace:
36             return true;
37
38         default:
39             throw new IllegalStateException("Default edit strategy can be only of value " + defaultStrats + " but was "
40                     + this);
41         }
42     }
43     public static void compareParsedStrategyToDefaultEnforcing(EditStrategyType parsedStrategy,
44                                                                   EditStrategyType defaultStrategy) throws OperationNotPermittedException {
45         if (defaultStrategy.isEnforcing()) {
46             if (parsedStrategy != defaultStrategy){
47                 throw new OperationNotPermittedException(String.format("With "
48                         + defaultStrategy
49                         + " as "
50                         + EditConfigXmlParser.DEFAULT_OPERATION_KEY
51                         + " operations on module elements are not permitted since the default option is restrictive"),
52                         NetconfDocumentedException.ErrorType.application,
53                         NetconfDocumentedException.ErrorTag.operation_failed,
54                         NetconfDocumentedException.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 none:
70             return new NoneEditConfigStrategy();
71         default:
72             throw new UnsupportedOperationException("Unimplemented edit config strategy" + this);
73         }
74     }
75 }