Update the API generation code and code generation sample
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / parser / builder / impl / UsesNodeBuilderImpl.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.yang.model.parser.builder.impl;\r
9 \r
10 import java.util.ArrayList;\r
11 import java.util.Collections;\r
12 import java.util.HashSet;\r
13 import java.util.List;\r
14 import java.util.Map;\r
15 import java.util.Set;\r
16 \r
17 import org.opendaylight.controller.yang.common.QName;\r
18 import org.opendaylight.controller.yang.model.api.AugmentationSchema;\r
19 import org.opendaylight.controller.yang.model.api.SchemaNode;\r
20 import org.opendaylight.controller.yang.model.api.SchemaPath;\r
21 import org.opendaylight.controller.yang.model.api.UsesNode;\r
22 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;\r
23 import org.opendaylight.controller.yang.model.parser.builder.api.Builder;\r
24 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;\r
25 \r
26 public class UsesNodeBuilderImpl implements UsesNodeBuilder, Builder {\r
27 \r
28     private final UsesNodeImpl instance;\r
29     private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<AugmentationSchemaBuilder>();\r
30 \r
31     UsesNodeBuilderImpl(String groupingPathStr) {\r
32         SchemaPath groupingPath = parseUsesPath(groupingPathStr);\r
33         instance = new UsesNodeImpl(groupingPath);\r
34     }\r
35 \r
36     @Override\r
37     public UsesNode build() {\r
38         // AUGMENTATIONS\r
39         final Set<AugmentationSchema> augments = new HashSet<AugmentationSchema>();\r
40         for (AugmentationSchemaBuilder builder : addedAugments) {\r
41             augments.add(builder.build());\r
42         }\r
43         instance.setAugmentations(augments);\r
44 \r
45         return instance;\r
46     }\r
47 \r
48     @Override\r
49     public void addAugment(AugmentationSchemaBuilder augmentBuilder) {\r
50         addedAugments.add(augmentBuilder);\r
51     }\r
52 \r
53     @Override\r
54     public void setAugmenting(boolean augmenting) {\r
55         instance.setAugmenting(augmenting);\r
56     }\r
57 \r
58     private SchemaPath parseUsesPath(String augmentPath) {\r
59         String[] splittedPath = augmentPath.split("/");\r
60         List<QName> path = new ArrayList<QName>();\r
61         QName name;\r
62         for (String pathElement : splittedPath) {\r
63             String[] splittedElement = pathElement.split(":");\r
64             if (splittedElement.length == 1) {\r
65                 name = new QName(null, null, null, splittedElement[0]);\r
66             } else {\r
67                 name = new QName(null, null, splittedElement[0],\r
68                         splittedElement[1]);\r
69             }\r
70             path.add(name);\r
71         }\r
72         final boolean absolute = augmentPath.startsWith("/");\r
73         return new SchemaPath(path, absolute);\r
74     }\r
75 \r
76     private static class UsesNodeImpl implements UsesNode {\r
77 \r
78         private final SchemaPath groupingPath;\r
79         private Set<AugmentationSchema> augmentations = Collections.emptySet();\r
80         private boolean augmenting;\r
81 \r
82         private UsesNodeImpl(SchemaPath groupingPath) {\r
83             this.groupingPath = groupingPath;\r
84         }\r
85 \r
86         @Override\r
87         public SchemaPath getGroupingPath() {\r
88             return groupingPath;\r
89         }\r
90 \r
91         @Override\r
92         public Set<AugmentationSchema> getAugmentations() {\r
93             return augmentations;\r
94         }\r
95 \r
96         private void setAugmentations(Set<AugmentationSchema> augmentations) {\r
97             if (augmentations != null) {\r
98                 this.augmentations = augmentations;\r
99             }\r
100         }\r
101 \r
102         @Override\r
103         public boolean isAugmenting() {\r
104             return augmenting;\r
105         }\r
106         \r
107         private void setAugmenting(boolean augmenting) {\r
108             this.augmenting = augmenting;\r
109         }\r
110         \r
111 \r
112         @Override\r
113         public Map<SchemaPath, SchemaNode> getRefines() {\r
114             // TODO Auto-generated method stub\r
115             return null;\r
116         }\r
117         \r
118         @Override\r
119         public int hashCode() {\r
120             final int prime = 31;\r
121             int result = 1;\r
122             result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());\r
123             result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());\r
124             result = prime * result + (augmenting ? 1231 : 1237);\r
125             return result;\r
126         }\r
127 \r
128         @Override\r
129         public boolean equals(Object obj) {\r
130             if (this == obj) {\r
131                 return true;\r
132             }\r
133             if (obj == null) {\r
134                 return false;\r
135             }\r
136             if (getClass() != obj.getClass()) {\r
137                 return false;\r
138             }\r
139             UsesNodeImpl other = (UsesNodeImpl) obj;\r
140             if (groupingPath == null) {\r
141                 if (other.groupingPath != null) {\r
142                     return false;\r
143                 }\r
144             } else if (!groupingPath.equals(other.groupingPath)) {\r
145                 return false;\r
146             }\r
147             if (augmentations == null) {\r
148                 if (other.augmentations != null) {\r
149                     return false;\r
150                 }\r
151             } else if (!augmentations.equals(other.augmentations)) {\r
152                 return false;\r
153             }\r
154             if (augmenting != other.augmenting) {\r
155                 return false;\r
156             }\r
157             return true;\r
158         }\r
159 \r
160         @Override\r
161         public String toString() {\r
162             StringBuilder sb = new StringBuilder(\r
163                     UsesNodeImpl.class.getSimpleName());\r
164             sb.append("[groupingPath=" + groupingPath +"]");\r
165             return sb.toString();\r
166         }\r
167     }\r
168 }\r

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.