2096c3bdf83f671088a01fa91a571fb324ba1950
[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 /*
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 package org.opendaylight.controller.yang.model.parser.builder.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
18 import org.opendaylight.controller.yang.model.api.SchemaPath;
19 import org.opendaylight.controller.yang.model.api.UsesNode;
20 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
21 import org.opendaylight.controller.yang.model.parser.builder.api.Builder;
22 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
23
24 public class UsesNodeBuilderImpl implements UsesNodeBuilder, Builder {
25
26     private final UsesNodeImpl instance;
27     private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<AugmentationSchemaBuilder>();
28
29     UsesNodeBuilderImpl(String groupingPathStr) {
30         SchemaPath groupingPath = parseUsesPath(groupingPathStr);
31         instance = new UsesNodeImpl(groupingPath);
32     }
33
34     @Override
35     public UsesNode build() {
36         // AUGMENTATIONS
37         final Set<AugmentationSchema> augments = new HashSet<AugmentationSchema>();
38         for (AugmentationSchemaBuilder builder : addedAugments) {
39             augments.add(builder.build());
40         }
41         instance.setAugmentations(augments);
42
43         return instance;
44     }
45
46     @Override
47     public void addAugment(AugmentationSchemaBuilder augmentBuilder) {
48         addedAugments.add(augmentBuilder);
49     }
50
51     @Override
52     public void setAugmenting(boolean augmenting) {
53         instance.setAugmenting(augmenting);
54     }
55
56     private SchemaPath parseUsesPath(String augmentPath) {
57         String[] splittedPath = augmentPath.split("/");
58         List<QName> path = new ArrayList<QName>();
59         QName name;
60         for (String pathElement : splittedPath) {
61             String[] splittedElement = pathElement.split(":");
62             if (splittedElement.length == 1) {
63                 name = new QName(null, null, null, splittedElement[0]);
64             } else {
65                 name = new QName(null, null, splittedElement[0],
66                         splittedElement[1]);
67             }
68             path.add(name);
69         }
70         final boolean absolute = augmentPath.startsWith("/");
71         return new SchemaPath(path, absolute);
72     }
73
74     private static class UsesNodeImpl implements UsesNode {
75
76         private final SchemaPath groupingPath;
77         private Set<AugmentationSchema> augmentations = Collections.emptySet();
78         private boolean augmenting;
79
80         private UsesNodeImpl(SchemaPath groupingPath) {
81             this.groupingPath = groupingPath;
82         }
83
84         @Override
85         public SchemaPath getGroupingPath() {
86             return groupingPath;
87         }
88
89         @Override
90         public Set<AugmentationSchema> getAugmentations() {
91             return augmentations;
92         }
93
94         private void setAugmentations(Set<AugmentationSchema> augmentations) {
95             if (augmentations != null) {
96                 this.augmentations = augmentations;
97             }
98         }
99
100         @Override
101         public boolean isAugmenting() {
102             return augmenting;
103         }
104
105         private void setAugmenting(boolean augmenting) {
106             this.augmenting = augmenting;
107         }
108
109         @Override
110         public int hashCode() {
111             final int prime = 31;
112             int result = 1;
113             result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());
114             result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
115             result = prime * result + (augmenting ? 1231 : 1237);
116             return result;
117         }
118
119         @Override
120         public boolean equals(Object obj) {
121             if (this == obj) {
122                 return true;
123             }
124             if (obj == null) {
125                 return false;
126             }
127             if (getClass() != obj.getClass()) {
128                 return false;
129             }
130             UsesNodeImpl other = (UsesNodeImpl) obj;
131             if (groupingPath == null) {
132                 if (other.groupingPath != null) {
133                     return false;
134                 }
135             } else if (!groupingPath.equals(other.groupingPath)) {
136                 return false;
137             }
138             if (augmentations == null) {
139                 if (other.augmentations != null) {
140                     return false;
141                 }
142             } else if (!augmentations.equals(other.augmentations)) {
143                 return false;
144             }
145             if (augmenting != other.augmenting) {
146                 return false;
147             }
148             return true;
149         }
150
151         @Override
152         public String toString() {
153             StringBuilder sb = new StringBuilder(
154                     UsesNodeImpl.class.getSimpleName());
155             sb.append("[groupingPath=" + groupingPath +"]");
156             return sb.toString();
157         }
158
159     }
160
161 }