be271026d2cf0556a28b73b7f72369130cd69a45
[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     private Comparator<OdluxBundle> getBundleSorter() {
88         return this.sortorderbundlecomparator;
89     }
90
91
92     public List<String> getLoadedBundles(String myBundleName) {
93         List<String> list = new ArrayList<>();
94         for (OdluxBundle b : bundles2) {
95             list.add(b.getBundleName());
96         }
97         list.add(myBundleName);
98         return list;
99     }
100
101     private List<String> getBundleNames(String myBundleName) {
102         final List<String> names = new ArrayList<>();
103         for (OdluxBundle b : bundles2) {
104             names.add(b.getBundleName());
105         }
106         names.add(myBundleName);
107         return names;
108     }
109
110     public String getResource(String fn, OdluxBundleResourceAccess indexBundle) {
111         String fileContent = null;
112
113         if (indexBundle.hasResource(fn)) {
114             fileContent = indexBundle.getResourceFileContent(fn, getBundleNames(indexBundle.getBundleName()));
115         } else {
116             LOG.debug("not found in framework res. try to find in applications");
117             final Iterator<OdluxBundle> it = bundles2.iterator();
118             while (it.hasNext()) {
119                 OdluxBundle b = it.next();
120                 if (b.hasResource(fn)) {
121                     LOG.debug("found res in " + b.getBundleName());
122                     fileContent = b.getResourceFileContent(fn);
123                     break;
124                 }
125             }
126         }
127         return fileContent;
128     }
129
130 }