Committed by
Gerrit Code Review
STC fixes to correctly support namespaces.
Change-Id: I196b3bc3878181bd7b3711bf8b7304f772e476db
Showing
2 changed files
with
36 additions
and
9 deletions
... | @@ -31,6 +31,8 @@ import java.util.Set; | ... | @@ -31,6 +31,8 @@ import java.util.Set; |
31 | 31 | ||
32 | import static com.google.common.base.Preconditions.checkArgument; | 32 | import static com.google.common.base.Preconditions.checkArgument; |
33 | import static com.google.common.base.Preconditions.checkNotNull; | 33 | import static com.google.common.base.Preconditions.checkNotNull; |
34 | +import static com.google.common.base.Preconditions.checkState; | ||
35 | +import static com.google.common.base.Strings.isNullOrEmpty; | ||
34 | import static org.onlab.stc.Scenario.loadScenario; | 36 | import static org.onlab.stc.Scenario.loadScenario; |
35 | 37 | ||
36 | /** | 38 | /** |
... | @@ -67,6 +69,7 @@ public class Compiler { | ... | @@ -67,6 +69,7 @@ public class Compiler { |
67 | 69 | ||
68 | private final Map<String, Step> steps = Maps.newHashMap(); | 70 | private final Map<String, Step> steps = Maps.newHashMap(); |
69 | private final Map<String, Step> inactiveSteps = Maps.newHashMap(); | 71 | private final Map<String, Step> inactiveSteps = Maps.newHashMap(); |
72 | + private final Map<String, String> requirements = Maps.newHashMap(); | ||
70 | private final Set<Dependency> dependencies = Sets.newHashSet(); | 73 | private final Set<Dependency> dependencies = Sets.newHashSet(); |
71 | private final List<Integer> parallels = Lists.newArrayList(); | 74 | private final List<Integer> parallels = Lists.newArrayList(); |
72 | 75 | ||
... | @@ -100,6 +103,7 @@ public class Compiler { | ... | @@ -100,6 +103,7 @@ public class Compiler { |
100 | */ | 103 | */ |
101 | public void compile() { | 104 | public void compile() { |
102 | compile(scenario.definition(), null, null); | 105 | compile(scenario.definition(), null, null); |
106 | + compileRequirements(); | ||
103 | 107 | ||
104 | // Produce the process flow | 108 | // Produce the process flow |
105 | processFlow = new ProcessFlow(ImmutableSet.copyOf(steps.values()), | 109 | processFlow = new ProcessFlow(ImmutableSet.copyOf(steps.values()), |
... | @@ -150,6 +154,7 @@ public class Compiler { | ... | @@ -150,6 +154,7 @@ public class Compiler { |
150 | String namespace, Group parentGroup) { | 154 | String namespace, Group parentGroup) { |
151 | String opfx = pfx; | 155 | String opfx = pfx; |
152 | pfx = pfx + ">"; | 156 | pfx = pfx + ">"; |
157 | + print("pfx=%s namespace=%s", pfx, namespace); | ||
153 | 158 | ||
154 | // Scan all imports | 159 | // Scan all imports |
155 | cfg.configurationsAt(IMPORT) | 160 | cfg.configurationsAt(IMPORT) |
... | @@ -175,6 +180,26 @@ public class Compiler { | ... | @@ -175,6 +180,26 @@ public class Compiler { |
175 | } | 180 | } |
176 | 181 | ||
177 | /** | 182 | /** |
183 | + * Compiles requirements for all steps and groups accrued during the | ||
184 | + * overall compilation process. | ||
185 | + */ | ||
186 | + private void compileRequirements() { | ||
187 | + requirements.forEach((name, requires) -> | ||
188 | + compileRequirements(getStep(name), requires)); | ||
189 | + } | ||
190 | + | ||
191 | + private void compileRequirements(Step src, String requires) { | ||
192 | + split(requires).forEach(n -> { | ||
193 | + boolean isSoft = n.startsWith("~"); | ||
194 | + String name = n.replaceFirst("^~", ""); | ||
195 | + Step dst = getStep(name); | ||
196 | + if (dst != null) { | ||
197 | + dependencies.add(new Dependency(src, dst, isSoft)); | ||
198 | + } | ||
199 | + }); | ||
200 | + } | ||
201 | + | ||
202 | + /** | ||
178 | * Processes an import directive. | 203 | * Processes an import directive. |
179 | * | 204 | * |
180 | * @param cfg hierarchical definition | 205 | * @param cfg hierarchical definition |
... | @@ -189,7 +214,7 @@ public class Compiler { | ... | @@ -189,7 +214,7 @@ public class Compiler { |
189 | print("import file=%s namespace=%s", file, newNamespace); | 214 | print("import file=%s namespace=%s", file, newNamespace); |
190 | try { | 215 | try { |
191 | Scenario importScenario = loadScenario(new FileInputStream(file)); | 216 | Scenario importScenario = loadScenario(new FileInputStream(file)); |
192 | - compile(importScenario.definition(), namespace, parentGroup); | 217 | + compile(importScenario.definition(), newNamespace, parentGroup); |
193 | } catch (IOException e) { | 218 | } catch (IOException e) { |
194 | throw new IllegalArgumentException("Unable to import scenario", e); | 219 | throw new IllegalArgumentException("Unable to import scenario", e); |
195 | } | 220 | } |
... | @@ -246,6 +271,7 @@ public class Compiler { | ... | @@ -246,6 +271,7 @@ public class Compiler { |
246 | */ | 271 | */ |
247 | private boolean registerStep(Step step, HierarchicalConfiguration cfg, | 272 | private boolean registerStep(Step step, HierarchicalConfiguration cfg, |
248 | String namespace, Group parentGroup) { | 273 | String namespace, Group parentGroup) { |
274 | + checkState(!steps.containsKey(step.name()), "Step %s already exists", step.name()); | ||
249 | String ifClause = expand(cfg.getString(IF)); | 275 | String ifClause = expand(cfg.getString(IF)); |
250 | String unlessClause = expand(cfg.getString(UNLESS)); | 276 | String unlessClause = expand(cfg.getString(UNLESS)); |
251 | 277 | ||
... | @@ -259,6 +285,7 @@ public class Compiler { | ... | @@ -259,6 +285,7 @@ public class Compiler { |
259 | if (parentGroup != null) { | 285 | if (parentGroup != null) { |
260 | parentGroup.addChild(step); | 286 | parentGroup.addChild(step); |
261 | } | 287 | } |
288 | + | ||
262 | steps.put(step.name(), step); | 289 | steps.put(step.name(), step); |
263 | processRequirements(step, expand(cfg.getString(REQUIRES)), namespace); | 290 | processRequirements(step, expand(cfg.getString(REQUIRES)), namespace); |
264 | previous = step.name(); | 291 | previous = step.name(); |
... | @@ -323,15 +350,15 @@ public class Compiler { | ... | @@ -323,15 +350,15 @@ public class Compiler { |
323 | * @param namespace optional namespace | 350 | * @param namespace optional namespace |
324 | */ | 351 | */ |
325 | private void processRequirements(Step src, String requires, String namespace) { | 352 | private void processRequirements(Step src, String requires, String namespace) { |
326 | - split(requires).forEach(n -> { | 353 | + String reqs = requirements.get(src.name()); |
354 | + for (String n : split(requires)) { | ||
327 | boolean isSoft = n.startsWith("~"); | 355 | boolean isSoft = n.startsWith("~"); |
328 | String name = n.replaceFirst("^~", ""); | 356 | String name = n.replaceFirst("^~", ""); |
329 | name = previous != null && name.equals("^") ? previous : name; | 357 | name = previous != null && name.equals("^") ? previous : name; |
330 | - Step dst = getStep(name, namespace); | 358 | + name = (isSoft ? "~" : "") + expand(prefix(name, namespace)); |
331 | - if (!inactiveSteps.containsValue(dst)) { | 359 | + reqs = reqs == null ? name : (reqs + "," + name); |
332 | - dependencies.add(new Dependency(src, dst, isSoft)); | 360 | + } |
333 | - } | 361 | + requirements.put(src.name(), reqs); |
334 | - }); | ||
335 | } | 362 | } |
336 | 363 | ||
337 | /** | 364 | /** |
... | @@ -357,7 +384,7 @@ public class Compiler { | ... | @@ -357,7 +384,7 @@ public class Compiler { |
357 | * @return composite name | 384 | * @return composite name |
358 | */ | 385 | */ |
359 | private String prefix(String name, String namespace) { | 386 | private String prefix(String name, String namespace) { |
360 | - return namespace != null ? namespace + "." + name : name; | 387 | + return isNullOrEmpty(namespace) ? name : namespace + "." + name; |
361 | } | 388 | } |
362 | 389 | ||
363 | /** | 390 | /** | ... | ... |
... | @@ -70,7 +70,7 @@ public class CompilerTest { | ... | @@ -70,7 +70,7 @@ public class CompilerTest { |
70 | 70 | ||
71 | assertSame("incorrect scenario", scenario, compiler.scenario()); | 71 | assertSame("incorrect scenario", scenario, compiler.scenario()); |
72 | assertEquals("incorrect step count", 24, flow.getVertexes().size()); | 72 | assertEquals("incorrect step count", 24, flow.getVertexes().size()); |
73 | - assertEquals("incorrect dependency count", 17, flow.getEdges().size()); | 73 | + assertEquals("incorrect dependency count", 16, flow.getEdges().size()); |
74 | assertEquals("incorrect logDir", | 74 | assertEquals("incorrect logDir", |
75 | new File(TEST_DIR.getAbsolutePath(), "foo"), compiler.logDir()); | 75 | new File(TEST_DIR.getAbsolutePath(), "foo"), compiler.logDir()); |
76 | 76 | ... | ... |
-
Please register or login to post a comment