Remove OSGiYangParserFactory
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangParserFactoryImpl.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import java.util.Collection;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.kohsuke.MetaInfServices;
19 import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
20 import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
21 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
22 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
23 import org.opendaylight.yangtools.yang.xpath.api.YangXPathParserFactory;
24 import org.osgi.service.component.annotations.Activate;
25 import org.osgi.service.component.annotations.Component;
26 import org.osgi.service.component.annotations.Reference;
27
28 /**
29  * Reference {@link YangParserFactory} implementation.
30  */
31 @Beta
32 @MetaInfServices
33 @Singleton
34 @Component(immediate = true)
35 public final class YangParserFactoryImpl implements YangParserFactory {
36     private static final ImmutableList<StatementParserMode> SUPPORTED_MODES = ImmutableList.of(
37         StatementParserMode.DEFAULT_MODE, StatementParserMode.SEMVER_MODE);
38     private final CrossSourceStatementReactor reactor;
39
40     private YangParserFactoryImpl(final @NonNull CrossSourceStatementReactor reactor) {
41         this.reactor = requireNonNull(reactor);
42     }
43
44     /**
45      * Construct a new {@link YangParserFactory} backed by {@link DefaultReactors#defaultReactor()}.
46      */
47     public YangParserFactoryImpl() {
48         this(DefaultReactors.defaultReactor());
49     }
50
51     @Inject
52     @Activate
53     public YangParserFactoryImpl(final @Reference YangXPathParserFactory xpathFactory) {
54         this(DefaultReactors.defaultReactorBuilder(xpathFactory).build());
55     }
56
57     /**
58      * Construct a new {@link YangParserFactory} backed by specified reactor.
59      *
60      * @param reactor Backing reactor
61      * @return A new YangParserFactory
62      */
63     public static @NonNull YangParserFactory forReactor(final @NonNull CrossSourceStatementReactor reactor) {
64         return new YangParserFactoryImpl(reactor);
65     }
66
67     @Override
68     public Collection<StatementParserMode> supportedParserModes() {
69         return SUPPORTED_MODES;
70     }
71
72     @Override
73     public YangParser createParser(final StatementParserMode parserMode) {
74         return new YangParserImpl(reactor.newBuild(parserMode));
75     }
76 }