070b18f1ab8670c8a1f000f6b7737ea8d873c0e9
[yangtools.git] / parser / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / SourceIdMismatchDetector.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.yangtools.yang.parser.repo;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Function;
13 import com.google.common.collect.ImmutableList;
14 import java.util.Iterator;
15 import java.util.LinkedHashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
20 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 @SuppressModernizer
26 final class SourceIdMismatchDetector implements Function<List<IRSchemaSource>, List<IRSchemaSource>> {
27     private static final Logger LOG = LoggerFactory.getLogger(SourceIdMismatchDetector.class);
28
29     private final Set<SourceIdentifier> sourceIdentifiers;
30
31     SourceIdMismatchDetector(final Set<SourceIdentifier> sourceIdentifiers) {
32         this.sourceIdentifiers = requireNonNull(sourceIdentifiers);
33     }
34
35     @Override
36     public List<IRSchemaSource> apply(final List<IRSchemaSource> input) {
37         final Iterator<SourceIdentifier> srcIt = sourceIdentifiers.iterator();
38         final Iterator<IRSchemaSource> it = input.iterator();
39
40         final Map<SourceIdentifier, IRSchemaSource> filtered = new LinkedHashMap<>();
41         while (it.hasNext()) {
42             final IRSchemaSource irSchemaSource = it.next();
43             final SourceIdentifier realSId = irSchemaSource.getIdentifier();
44             if (srcIt.hasNext()) {
45                 final SourceIdentifier expectedSId = srcIt.next();
46                 if (!expectedSId.equals(realSId)) {
47                     LOG.warn("Source identifier mismatch for module \"{}\", requested as {} but actually is {}. "
48                         + "Using actual id", expectedSId.getName(), expectedSId, realSId);
49                 }
50             }
51
52             final IRSchemaSource prev = filtered.put(realSId, irSchemaSource);
53             if (prev != null) {
54                 LOG.warn("Duplicate source for module {} detected in reactor", realSId);
55             }
56         }
57
58         return ImmutableList.copyOf(filtered.values());
59     }
60 }