blob: 55e2a06d7890c5695a038cf44bc3a820e070a42a [file] [log] [blame]
Thomas Vachuska73436b52017-03-22 19:50:47 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yang;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onosproject.yang.model.YangModel;
25import org.onosproject.yang.runtime.DefaultModelRegistrationParam;
26import org.onosproject.yang.runtime.ModelRegistrationParam;
27import org.onosproject.yang.runtime.YangModelRegistry;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import static org.onosproject.yang.runtime.helperutils.YangApacheUtils.getYangModel;
32
33/**
34 * Abstract base for self-registering YANG models.
35 */
36@Component
37public abstract class AbstractYangModelRegistrator {
38
39 private final Logger log = LoggerFactory.getLogger(getClass());
40 private final Class<?> loaderClass;
41
42 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
43 protected YangModelRegistry modelRegistry;
44
45 private YangModel model;
46 private ModelRegistrationParam registrationParam;
47
48 /**
49 * Creates a model registrator primed with the class-loader of the specified
50 * class.
51 *
52 * @param loaderClass class whose class loader is to be used for locating schema data
53 */
54 protected AbstractYangModelRegistrator(Class<?> loaderClass) {
55 this.loaderClass = loaderClass;
56 }
57
58 @Activate
59 protected void activate() {
60 model = getYangModel(loaderClass);
61 registrationParam = DefaultModelRegistrationParam.builder()
62 .setYangModel(model).build();
63 modelRegistry.registerModel(registrationParam);
64 log.info("Started");
65 }
66
67 @Deactivate
68 protected void deactivate() {
69 modelRegistry.unregisterModel(registrationParam);
70 log.info("Stopped");
71 }
72}