Remove remoterpc dead code.
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / DataNodeContainerModificationStrategy.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.md.sal.dom.store.impl.tree.data;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import java.util.HashSet;
13 import java.util.Set;
14 import java.util.concurrent.ExecutionException;
15
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
29 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
30 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
35
36 import com.google.common.base.Function;
37 import com.google.common.base.Optional;
38 import com.google.common.cache.CacheBuilder;
39 import com.google.common.cache.CacheLoader;
40 import com.google.common.cache.LoadingCache;
41
42 /**
43  * Base strategy for applying changes to a ContainerNode, irrespective of its
44  * actual type.
45  *
46  * @param <T> Type of the container node
47  */
48 abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends NormalizedNodeContainerModificationStrategy {
49
50     private final T schema;
51     private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
52             .build(CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
53
54                 @Override
55                 public ModificationApplyOperation apply(final PathArgument identifier) {
56                     if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
57                         return from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
58                     }
59
60                     DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
61                     if (child == null) {
62                         return null;
63                     }
64                     return from(child);
65                 }
66             }));
67
68     protected DataNodeContainerModificationStrategy(final T schema,
69             final Class<? extends NormalizedNode<?, ?>> nodeClass) {
70         super(nodeClass);
71         this.schema = schema;
72     }
73
74     protected T getSchema() {
75         return schema;
76     }
77
78     @Override
79     public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
80         try {
81             return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
82         } catch (ExecutionException e) {
83             return Optional.absent();
84         }
85     }
86
87     @Override
88     @SuppressWarnings("rawtypes")
89     protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
90
91     @Override
92     public String toString() {
93         return getClass().getSimpleName() + " [" + schema + "]";
94     }
95
96     public static class AugmentationModificationStrategy extends DataNodeContainerModificationStrategy<AugmentationSchema> {
97
98         protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
99             super(createAugmentProxy(schema,resolved), AugmentationNode.class);
100         }
101
102         @Override
103         @SuppressWarnings("rawtypes")
104         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
105             checkArgument(original instanceof AugmentationNode);
106             return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
107         }
108
109
110         private static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
111             Set<DataSchemaNode> realChildSchemas = new HashSet<>();
112             for(DataSchemaNode augChild : schema.getChildNodes()) {
113                 realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
114             }
115             return new AugmentationSchemaProxy(schema, realChildSchemas);
116         }
117     }
118
119     public static class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerSchemaNode> {
120
121         public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
122             super(schemaNode, ContainerNode.class);
123         }
124
125         @Override
126         @SuppressWarnings("rawtypes")
127         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
128             checkArgument(original instanceof ContainerNode);
129             return ImmutableContainerNodeBuilder.create((ContainerNode) original);
130         }
131     }
132
133     public static class ListEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
134
135         protected ListEntryModificationStrategy(final ListSchemaNode schema) {
136             super(schema, MapEntryNode.class);
137         }
138
139         @Override
140         @SuppressWarnings("rawtypes")
141         protected final DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
142             checkArgument(original instanceof MapEntryNode);
143             return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
144         }
145     }
146
147     public static class UnkeyedListItemModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
148
149         public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) {
150             super(schemaNode, UnkeyedListEntryNode.class);
151         }
152
153         @Override
154         @SuppressWarnings("rawtypes")
155         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
156             checkArgument(original instanceof UnkeyedListEntryNode);
157             return ImmutableUnkeyedListEntryNodeBuilder.create((UnkeyedListEntryNode) original);
158         }
159     }
160 }