aboutsummaryrefslogtreecommitdiffstats
path: root/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicActivator.java
blob: 7c1fa8e44a8059ecbfb0ec4d45bd52fafa6ef71a (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*-
 * ============LICENSE_START=======================================================
 * ONAP : CCSDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 *                         reserved.
 * ================================================================================
 * 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.ccsdk.sli.core.sli.provider;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import org.onap.ccsdk.sli.core.sli.ConfigurationException;
import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SvcLogicActivator implements BundleActivator {

    private static final String SVCLOGIC_PROP_VAR = "SDNC_SLI_PROPERTIES";
    private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";

    private static final Map<String, SvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, SvcLogicNodeExecutor>() {
        {
            put("block", new BlockNodeExecutor());
            put("call", new CallNodeExecutor());
            put("configure", new ConfigureNodeExecutor());
            put("delete", new DeleteNodeExecutor());
            put("execute", new ExecuteNodeExecutor());
            put("exists", new ExistsNodeExecutor());
            put("for", new ForNodeExecutor());
            put("get-resource", new GetResourceNodeExecutor());
            put("is-available", new IsAvailableNodeExecutor());
            put("notify", new NotifyNodeExecutor());
            put("record", new RecordNodeExecutor());
            put("release", new ReleaseNodeExecutor());
            put("reserve", new ReserveNodeExecutor());
            put("return", new ReturnNodeExecutor());
            put("save", new SaveNodeExecutor());
            put("set", new SetNodeExecutor());
            put("switch", new SwitchNodeExecutor());
            put("update", new UpdateNodeExecutor());
            put("break", new BreakNodeExecutor());

        }
    };

    private static final Logger LOG = LoggerFactory.getLogger(SvcLogicActivator.class);

    private static LinkedList<ServiceRegistration> registrations = new LinkedList<>();

    private static HashMap<String, SvcLogicAdaptor> adaptorMap;

    private static Properties props;

    private static BundleContext bundleCtx;

    private static SvcLogicService svcLogicServiceImpl;

    @Override
    public void start(BundleContext ctx) throws Exception {

        LOG.info("Activating SLI");

        synchronized (SvcLogicActivator.class) {
            bundleCtx = ctx;
            props = new Properties();
        }

        // Read properties
        String propPath = System.getenv(SVCLOGIC_PROP_VAR);

        if (propPath == null) {
            String propDir = System.getenv(SDNC_CONFIG_DIR);
            if (propDir == null) {

                propDir = "/opt/sdnc/data/properties";
            }
            propPath = propDir + "/svclogic.properties";
            LOG.warn("Environment variable {} unset - defaulting to {}", SVCLOGIC_PROP_VAR, propPath);
        }

        File propFile = new File(propPath);

        if (!propFile.exists()) {
            throw new ConfigurationException("Missing configuration properties file : " + propFile);
        }

        try {
            props.load(new FileInputStream(propFile));
        } catch (Exception e) {
            throw new ConfigurationException("Could not load properties file " + propPath, e);

        }

        synchronized (SvcLogicActivator.class) {
            if (registrations == null) {
                registrations = new LinkedList<>();
            }
            // Advertise SvcLogicService
            svcLogicServiceImpl = new SvcLogicServiceImpl();
        }

        LOG.info("SLI: Registering service {} in bundle {}", SvcLogicService.NAME, ctx.getBundle().getSymbolicName());
        ServiceRegistration reg = ctx.registerService(SvcLogicService.NAME, svcLogicServiceImpl, null);
        registrations.add(reg);

        // Initialize SvcLogicStore
        try {
            SvcLogicStore store = getStore();
            registerNodeTypes(store);
        } catch (ConfigurationException e) {
            LOG.warn("Could not initialize SvcLogicScore", e);
        }

        LOG.info("SLI - done registering services");
    }

    @Override
    public void stop(BundleContext ctx) throws Exception {

        if (registrations != null) {
            for (ServiceRegistration reg : registrations) {
                ServiceReference regRef = reg.getReference();
                /* Don't bother to remove node types from table
                String nodeType = (String) regRef.getProperty("nodeType");
                if (nodeType != null) {
                    LOG.info("SLI - unregistering node type " + nodeType);
                    store.unregisterNodeType(nodeType);
                }
                */
                reg.unregister();
            }
            synchronized (SvcLogicActivator.class) {
                registrations = null;
            }
        }
    }

    public static SvcLogicStore getStore() throws SvcLogicException {
        // Create and initialize SvcLogicStore object - used to access
        // saved service logic.

        SvcLogicStore store;

        try {
            store = SvcLogicStoreFactory.getSvcLogicStore(props);
        } catch (Exception e) {
            throw new ConfigurationException("Could not get service logic store", e);

        }

        try {
            store.init(props);
        } catch (Exception e) {
            throw new ConfigurationException("Could not get service logic store", e);
        }

        return(store);
    }

    private static void registerNodeTypes(SvcLogicStore store) throws SvcLogicException {

        if (store == null) {
            return;
        }
        // Advertise built-in node executors
        LOG.info("SLI : Registering built-in node executors");
        Hashtable propTable = new Hashtable();

        for (String nodeType : BUILTIN_NODES.keySet()) {
            LOG.info("SLI - registering node type {}", nodeType);
            propTable.clear();
            propTable.put("nodeType", nodeType);

            ServiceRegistration reg = bundleCtx.registerService(SvcLogicNodeExecutor.class.getName(),
                    BUILTIN_NODES.get(nodeType), propTable);
            registrations.add(reg);

            store.registerNodeType(nodeType);

            LOG.info("SLI - registering node executor");

            ((SvcLogicServiceImpl)svcLogicServiceImpl).registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));

        }
    }
}