Initial code drop of netconf protocol implementation
[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
14 import com.google.common.base.Preconditions;
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     private static EditStrategyType defaultStrat = merge;
25
26     public static EditStrategyType defaultStrategy() {
27         return defaultStrat;
28     }
29
30     public static void setDefaultStrategy(EditStrategyType strat) {
31         Preconditions.checkArgument(strat.canBeDefault(), "Default edit strategy can be only of value " + defaultStrats
32                 + ", but was " + strat);
33         defaultStrat = strat;
34     }
35
36     public static void resetDefaultStrategy() {
37         setDefaultStrategy(EditStrategyType.merge);
38     }
39
40     public boolean isEnforcing() {
41         switch (this) {
42         case merge:
43         case none:
44         case remove:
45         case delete:
46             return false;
47         case replace:
48             return true;
49
50         default:
51             throw new IllegalStateException("Default edit strategy can be only of value " + defaultStrats + " but was "
52                     + this);
53         }
54     }
55
56     private static final EnumSet<EditStrategyType> defaults;
57
58     static {
59         defaults = EnumSet.of(merge, replace, none);
60     }
61
62     private boolean canBeDefault() {
63         return defaults.contains(this);
64     }
65
66     public EditConfigStrategy getFittingStrategy() {
67         switch (this) {
68         case merge:
69             return new MergeEditConfigStrategy();
70         case replace:
71             return new ReplaceEditConfigStrategy();
72         case delete:
73             return new DeleteEditConfigStrategy();
74         case remove:
75             return new RemoveEditConfigStrategy();
76         case none:
77             return new NoneEditConfigStrategy();
78         default:
79             throw new UnsupportedOperationException("Unimplemented edit config strategy" + this);
80         }
81     }
82 }