e8c325c57365efe98b8295f981521e8e778115fe
[federation.git] / gateway / src / main / java / org / acumos / federation / gateway / service / impl / ServiceImpl.java
1 /*-
2  * ===============LICENSE_START=======================================================
3  * Acumos
4  * ===================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6  * ===================================================================================
7  * This Acumos software file is distributed by AT&T and Tech Mahindra
8  * under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *  
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *  
14  * This file is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ===============LICENSE_END=========================================================
19  */
20
21 package org.acumos.federation.gateway.service.impl;
22
23 import java.lang.invoke.MethodHandles;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.acumos.cds.domain.MLPSolution;
29 import org.acumos.cds.domain.MLPTag;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Some basic tooling for service implementation.
35  * Common functionality to be re-used across service implementations.
36  */
37 public abstract class ServiceImpl {
38
39         private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
40
41         private ServiceImpl() {
42         }
43
44         /**
45          * Bit of a primitive implementation
46          * @param theSolution solution
47          * @param theSelector selector
48          * @return Boolean
49          */
50         public static boolean isSelectable(MLPSolution theSolution, Map<String, ?> theSelector) /*throws ServiceException*/ {
51                 boolean res = true;
52
53                 log.trace("isSelectable {}", theSolution);
54                 if (theSelector == null || theSelector.isEmpty())
55                         return true;
56
57                 Object solutionId = theSelector.get("solutionId");
58                 if (solutionId != null) {
59                         log.trace("using solutionId based selection {}", solutionId);
60                         if (solutionId instanceof String) {
61                                 res &= theSolution.getSolutionId().equals(solutionId);
62                         }
63                         else {
64                                 log.debug("unknown solutionId criteria representation {}", solutionId.getClass().getName());
65                                 return false;
66                         }
67                 }
68
69                 Object modelTypeCode = theSelector.get("modelTypeCode");
70                 if (modelTypeCode != null) {
71                         log.trace("using modelTypeCode based selection {}", modelTypeCode);
72                         String solutionModelTypeCode = theSolution.getModelTypeCode();
73                         if (solutionModelTypeCode == null) {
74                                 return false;
75                         }
76                         else {
77                                 if (modelTypeCode instanceof String) {
78                                         res &= solutionModelTypeCode.equals(modelTypeCode);
79                                 }
80                                 else if (modelTypeCode instanceof List) {
81                                         res &= ((List)modelTypeCode).contains(solutionModelTypeCode);
82                                 }
83                                 else {
84                                         log.debug("unknown modelTypeCode criteria representation {}", modelTypeCode.getClass().getName());
85                                         return false;
86                                 }
87                         }
88                 }
89
90                 Object toolkitTypeCode = theSelector.get("toolkitTypeCode");
91                 if (toolkitTypeCode != null) {
92                         log.trace("using toolkitTypeCode based selection {}", toolkitTypeCode);
93                         String solutionToolkitTypeCode = theSolution.getToolkitTypeCode();
94                         if (solutionToolkitTypeCode == null) {
95                                 return false;
96                         }
97                         else {
98                                 if (toolkitTypeCode instanceof String) {
99                                         res &= solutionToolkitTypeCode.equals(toolkitTypeCode);
100                                 }
101                                 else if (toolkitTypeCode instanceof List) {
102                                         res &= ((List)toolkitTypeCode).contains(solutionToolkitTypeCode);
103                                 }
104                                 else {
105                                         log.debug("unknown toolkitTypeCode criteria representation {}", toolkitTypeCode.getClass().getName());
106                                         return false;
107                                 }
108                         }
109                 }
110
111                 Object tags = theSelector.get("tags");
112                 if (tags != null) {
113                         log.trace("using tags based selection {}", tags);
114                         Set<MLPTag> solutionTags = theSolution.getTags();
115                         if (solutionTags == null) {
116                                 return false;
117                         }
118                         else {
119                                 if (tags instanceof String) {
120                                         res &= solutionTags.stream().filter(solutionTag -> tags.equals(solutionTag.getTag())).findAny().isPresent();
121                                 }
122                                 else if (tags instanceof List) {
123                                         res &= solutionTags.stream().filter(solutionTag -> ((List)tags).contains(solutionTag.getTag())).findAny().isPresent();
124                                 }
125                                 else {
126                                         log.debug("unknown tags criteria representation {}", tags.getClass().getName());
127                                         return false; 
128                                 }
129                         }
130                 }
131
132                 Object name = theSelector.get("name");
133                 if (name != null) {
134                         log.debug("using name based selection {}", name);
135                         String solutionName = theSolution.getName();
136                         if (solutionName == null) {
137                                 return false;
138                         }
139                         else {
140                                 res &= solutionName.contains(name.toString());
141                         }
142                 }
143
144                 return res;
145         }
146
147
148 }