summaryrefslogtreecommitdiffstats
path: root/actn-interface-tools/global-control/app/src/main/java/org/onap/integration/actninterfacetools/globalapp/AbstractYangModelRegistrator.java
blob: 6dba1e51a6f964f02818c01cbd38d218674ebc08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 *   ============LICENSE_START=======================================================
 *   Actn Interface Tools
 *   ================================================================================
 *   Copyright (C) 2023 Huawei Canada Limited.
 *   ================================================================================
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *   ============LICENSE_END=========================================================
 */

package org.onap.integration.actninterfacetools.globalapp;

import org.onap.integration.actninterfacetools.globalapp.impl.GlobalServiceUtil;
import org.onosproject.yang.model.YangModel;
import org.onosproject.yang.model.YangModuleId;
import org.onosproject.yang.runtime.AppModuleInfo;
import org.onosproject.yang.runtime.DefaultModelRegistrationParam;
import org.onosproject.yang.runtime.ModelRegistrationParam;
import org.onosproject.yang.runtime.ModelRegistrationParam.Builder;
import org.onosproject.yang.runtime.YangModelRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * Abstract base for self-registering YANG models.
 */

public abstract class AbstractYangModelRegistrator {

    private final Logger log = LoggerFactory.getLogger(getClass());
    private final Class<?> loaderClass;

    private Map<YangModuleId, AppModuleInfo> appInfo;
    protected YangModel model;
    private ModelRegistrationParam registrationParam;

    protected YangModelRegistry modelRegistry = GlobalServiceUtil.getNewGlobalService();
   protected YangClassLoaderRegistry sourceResolver = GlobalServiceUtil.getNewGlobalService();

    /**
     * Creates a model registrator primed with the class-loader of the specified
     * class.
     *
     * @param loaderClass class whose class loader is to be used for locating
     *                    schema data
     */
    protected AbstractYangModelRegistrator(Class<?> loaderClass) {
        this.loaderClass = loaderClass;
    }

    /**
     * Creates a model registrator primed with the class-loader of the specified
     * class and application info.
     *
     * @param loaderClass class whose class loader is to be used for locating
     *                    schema data
     * @param appInfo     application information
     */
    protected AbstractYangModelRegistrator(Class<?> loaderClass,
                                           Map<YangModuleId, AppModuleInfo> appInfo) {
        this.loaderClass = loaderClass;
        this.appInfo = appInfo;
    }


    public void activate() throws URISyntaxException {
        model = YangManagerUtils.getYangModel(loaderClass);
        ModelRegistrationParam.Builder b =
                DefaultModelRegistrationParam.builder().setYangModel(model);
        registrationParam = getAppInfo(b).setYangModel(model).build();
        sourceResolver.registerClassLoader(model.getYangModelId(), loaderClass.getClassLoader());
        modelRegistry.registerModel(registrationParam);
        log.info("Yang Model Registration Finished");
    }

    protected ModelRegistrationParam.Builder getAppInfo(Builder b) {
        if (appInfo != null) {
            appInfo.entrySet().stream().filter(
                    entry -> model.getYangModule(entry.getKey()) != null).forEach(
                    entry -> b.addAppModuleInfo(entry.getKey(), entry.getValue()));
        }
        return b;
    }

    protected void deactivate() {
        modelRegistry.unregisterModel(registrationParam);
        sourceResolver.unregisterClassLoader(model.getYangModelId());
        log.info("Stopped");
    }
}