205c96129a3d3dbc3e95189e3034dece5a3f344e
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.odlux;
19
20 import java.util.ArrayList;
21 import java.util.Comparator;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.concurrent.ConcurrentSkipListSet;
25 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundle;
26 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleLoader;
27 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleResourceAccess;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class OdluxBundleLoaderImpl implements OdluxBundleLoader {
32
33     private final static Logger LOG = LoggerFactory.getLogger(OdluxBundleLoaderImpl.class);
34
35     private static OdluxBundleLoaderImpl singleton = null;
36
37     private Object lock;
38     // private List<OdluxBundle> bundles;
39     private ConcurrentSkipListSet<OdluxBundle> bundles2;
40     private Comparator<OdluxBundle> sortorderbundlecomparator;
41
42     public OdluxBundleLoaderImpl() {
43         if (singleton != null) {
44             LOG.error("Multiple intializations of singleton");
45         } else {
46             lock = new Object();
47             sortorderbundlecomparator = (arg0, arg1) -> {
48                 int diff = arg0.getIndex() - arg1.getIndex();
49                 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
50             };
51             bundles2 = new ConcurrentSkipListSet<>(sortorderbundlecomparator);
52
53             singleton = this;
54         }
55     }
56
57     public static OdluxBundleLoaderImpl getInstance() {
58         if (singleton == null) {
59             LOG.warn("Test initialization order");
60             new OdluxBundleLoaderImpl();
61         }
62         return singleton;
63     }
64
65     @Override
66     public int getNumberOfBundles() {
67         synchronized (lock) {
68             return this.bundles2.size();
69         }
70     }
71
72     @Override
73     public void addBundle(OdluxBundle bundle) {
74         LOG.debug("odlux bundle " + bundle.getBundleName() + " added");
75         this.bundles2.add(bundle);
76
77
78     }
79
80     @Override
81     public void removeBundle(OdluxBundle bundle) {
82         this.bundles2.remove(bundle);
83         LOG.debug("odlux bundle " + bundle.getBundleName() + " removed");
84
85     }
86
87     public List<String> getLoadedBundles(String myBundleName) {
88         List<String> list = new ArrayList<>();
89         for (OdluxBundle b : bundles2) {
90             list.add(b.getBundleName());
91         }
92         list.add(myBundleName);
93         return list;
94     }
95
96     private List<String> getBundleNames(String myBundleName) {
97         final List<String> names = new ArrayList<>();
98         for (OdluxBundle b : bundles2) {
99             names.add(b.getBundleName());
100         }
101         names.add(myBundleName);
102         return names;
103     }
104
105     public String getResourceContent(String fn, OdluxBundleResourceAccess indexBundle) {
106         String fileContent = null;
107
108         if (indexBundle.hasResource(fn)) {
109             fileContent = indexBundle.getResourceFileContent(fn, getBundleNames(indexBundle.getBundleName()));
110         } else {
111             LOG.debug("not found in framework res. try to find in applications");
112             final Iterator<OdluxBundle> it = bundles2.iterator();
113             while (it.hasNext()) {
114                 OdluxBundle b = it.next();
115                 if (b.hasResource(fn)) {
116                     LOG.debug("found res in " + b.getBundleName());
117                     fileContent = b.getResourceFileContent(fn);
118                     break;
119                 }
120             }
121         }
122         return fileContent;
123     }
124
125 }