Bump odlparent to 13.1.3
[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.LinkedHashMap;
15 import java.util.List;
16 import java.util.Set;
17 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
18 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
19 import org.opendaylight.yangtools.yang.model.spi.source.YangIRSource;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 @SuppressModernizer
24 final class SourceIdMismatchDetector implements Function<List<YangIRSource>, List<YangIRSource>> {
25     private static final Logger LOG = LoggerFactory.getLogger(SourceIdMismatchDetector.class);
26
27     private final Set<SourceIdentifier> sourceIdentifiers;
28
29     SourceIdMismatchDetector(final Set<SourceIdentifier> sourceIdentifiers) {
30         this.sourceIdentifiers = requireNonNull(sourceIdentifiers);
31     }
32
33     @Override
34     public List<YangIRSource> apply(final List<YangIRSource> input) {
35         final var srcIt = sourceIdentifiers.iterator();
36         final var filtered = new LinkedHashMap<SourceIdentifier, YangIRSource>();
37         for (var irSchemaSource : input) {
38             final SourceIdentifier realSId = irSchemaSource.sourceId();
39             if (srcIt.hasNext()) {
40                 final SourceIdentifier expectedSId = srcIt.next();
41                 if (!expectedSId.equals(realSId)) {
42                     LOG.warn("Source identifier mismatch for module \"{}\", requested as {} but actually is {}. "
43                         + "Using actual id", expectedSId.name().getLocalName(), expectedSId, realSId);
44                 }
45             }
46
47             final var prev = filtered.put(realSId, irSchemaSource);
48             if (prev != null) {
49                 LOG.warn("Duplicate source for module {} detected in reactor", realSId);
50             }
51         }
52
53         return ImmutableList.copyOf(filtered.values());
54     }
55 }