Merge "Fixed typo in SnapshotBackedWriteTransaction class"
[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 com.google.common.collect.Multimap;
12 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
13 import org.opendaylight.controller.netconf.confignetconfconnector.exception.OperationNotPermittedException;
14
15 import java.util.EnumSet;
16 import java.util.Set;
17
18 public enum EditStrategyType {
19     // can be default
20     merge, replace, none,
21     // additional per element
22     delete, remove;
23
24     private static final Set<EditStrategyType> defaultStrats = EnumSet.of(merge, replace, none);
25
26     public static EditStrategyType getDefaultStrategy() {
27         return merge;
28     }
29
30     public boolean isEnforcing() {
31         switch (this) {
32         case merge:
33         case none:
34         case remove:
35         case delete:
36             return false;
37         case replace:
38             return true;
39
40         default:
41             throw new IllegalStateException("Default edit strategy can be only of value " + defaultStrats + " but was "
42                     + this);
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(String.format("With "
50                         + defaultStrategy
51                         + " as "
52                         + EditConfigXmlParser.DEFAULT_OPERATION_KEY
53                         + " operations on module elements are not permitted since the default option is restrictive"),
54                         NetconfDocumentedException.ErrorType.application,
55                         NetconfDocumentedException.ErrorTag.operation_failed,
56                         NetconfDocumentedException.ErrorSeverity.error);
57             }
58         }
59
60     }
61     public EditConfigStrategy getFittingStrategy(Multimap<String, String> providedServices) {
62         switch (this) {
63         case merge:
64             return new MergeEditConfigStrategy(providedServices);
65         case replace:
66             return new ReplaceEditConfigStrategy(providedServices);
67         case delete:
68             return new DeleteEditConfigStrategy();
69         case remove:
70             return new RemoveEditConfigStrategy();
71         case none:
72             return new NoneEditConfigStrategy();
73         default:
74             throw new UnsupportedOperationException("Unimplemented edit config strategy" + this);
75         }
76     }
77 }