*/\r
package org.onap.schema;\r
\r
-import com.google.common.collect.Multimap;\r
-import org.apache.commons.io.IOUtils;\r
-import org.onap.aai.cl.eelf.LoggerFactory;\r
-import org.onap.aai.edges.EdgeRule;\r
-import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;\r
-import org.onap.aai.edges.EdgeIngestor;\r
-import org.onap.aai.setup.ConfigTranslator;\r
-import org.onap.aai.setup.SchemaLocationsBean;\r
-import org.onap.aai.setup.Version;\r
-import org.onap.crud.exception.CrudException;\r
-import org.onap.crud.logging.CrudServiceMsgs;\r
-import org.onap.schema.util.SchemaIngestPropertyReader;\r
-import org.springframework.core.io.UrlResource;\r
-\r
-import javax.ws.rs.core.Response.Status;\r
-import java.io.*;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
import java.util.Arrays;\r
import java.util.Map;\r
import java.util.concurrent.ConcurrentHashMap;\r
import java.util.regex.Pattern;\r
import java.util.stream.Collectors;\r
\r
+import javax.annotation.PostConstruct;\r
+import javax.ws.rs.core.Response.Status;\r
+\r
+import org.apache.commons.io.IOUtils;\r
+import org.onap.aai.cl.eelf.LoggerFactory;\r
+import org.onap.aai.edges.EdgeIngestor;\r
+import org.onap.aai.edges.EdgeRule;\r
+import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;\r
+import org.onap.aai.setup.SchemaVersion;\r
+import org.onap.aai.setup.Translator;\r
+import org.onap.crud.exception.CrudException;\r
+import org.onap.crud.logging.CrudServiceMsgs;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+\r
+import com.google.common.collect.Multimap;\r
+\r
public class EdgeRulesLoader {\r
\r
+ private static Translator translator;\r
+ private static EdgeIngestor edgeIngestor;\r
+\r
+ private static EdgePropsConfiguration edgePropsConfiguration;\r
+\r
private static Map<String, RelationshipSchema> versionContextMap =\r
new ConcurrentHashMap<> ();\r
\r
- static final Pattern versionPattern = Pattern.compile ( "V(\\d*)" );\r
+ static final Pattern versionPattern = Pattern.compile("(?i)v(\\d*)");\r
static final String propsPrefix = "edge_properties_";\r
static final String propsSuffix = ".json";\r
final static Pattern propsFilePattern = Pattern.compile ( propsPrefix + "(.*)" + propsSuffix );\r
- final static Pattern propsVersionPattern = Pattern.compile ( "v\\d*" );\r
+ final static Pattern propsVersionPattern = Pattern.compile("(?i)v(\\d*)");\r
\r
private static org.onap.aai.cl.api.Logger logger =\r
LoggerFactory.getInstance ().getLogger ( EdgeRulesLoader.class.getName () );\r
\r
- private EdgeRulesLoader () {\r
+ private EdgeRulesLoader () { }\r
+\r
+ @Autowired\r
+ public EdgeRulesLoader(Translator translator, EdgeIngestor edgeIngestor, EdgePropsConfiguration edgePropsConfiguration) {\r
+ EdgeRulesLoader.translator = translator;\r
+ EdgeRulesLoader.edgeIngestor = edgeIngestor;\r
+ EdgeRulesLoader.edgePropsConfiguration = edgePropsConfiguration;\r
}\r
\r
+ @PostConstruct\r
+ public void init() throws CrudException {\r
+ EdgeRulesLoader.loadModels();\r
+ }\r
+ \r
/**\r
* Finds all DB Edge Rules and Edge Properties files for all OXM models.\r
*\r
* @throws CrudException\r
*/\r
public static synchronized void loadModels () throws CrudException {\r
- SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader ();\r
- SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean ();\r
- schemaLocationsBean.setEdgeDirectory ( schemaIngestPropertyReader.getEdgeDir () );\r
- ConfigTranslator configTranslator = new OxmModelConfigTranslator ( schemaLocationsBean );\r
- EdgeIngestor edgeIngestor = new EdgeIngestor ( configTranslator );\r
- Map<String, File> propFiles = edgePropertyFiles(schemaIngestPropertyReader);\r
-\r
- if (logger.isDebugEnabled ()) {\r
- logger.debug ( "Loading DB Edge Rules" );\r
+ Map<String, File> propFiles = edgePropertyFiles(edgePropsConfiguration);\r
+\r
+ if (logger.isDebugEnabled()) {\r
+ logger.debug("Loading DB Edge Rules");\r
}\r
\r
- for (String version : OxmModelLoader.getLoadedOXMVersions ()) {\r
+ for (String version : OxmModelLoader.getLoadedOXMVersions()) {\r
try {\r
- loadModel ( Version.valueOf ( version ), edgeIngestor, propFiles );\r
+ SchemaVersion schemaVersion = translator.getSchemaVersions().getVersions().stream()\r
+ .filter(s -> s.toString().equalsIgnoreCase(version)).findAny().orElse(null);\r
+ loadModel(schemaVersion, edgeIngestor, propFiles);\r
} catch (IOException | EdgeRuleNotFoundException e) {\r
throw new CrudException(e.getMessage (), e);\r
}\r
*/\r
\r
public static synchronized void loadModels ( String v ) throws CrudException {\r
- SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader ();\r
- SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean ();\r
- schemaLocationsBean.setEdgeDirectory ( schemaIngestPropertyReader.getEdgeDir () );\r
- ConfigTranslator configTranslator = new OxmModelConfigTranslator ( schemaLocationsBean );\r
- EdgeIngestor edgeIngestor = new EdgeIngestor ( configTranslator );\r
- String version = v.toUpperCase ();\r
- Map<String, File> propFiles = edgePropertyFiles(schemaIngestPropertyReader);\r
-\r
- if (logger.isDebugEnabled ()) {\r
- logger.debug ( "Loading DB Edge Rules " );\r
+ Map<String, File> propFiles = edgePropertyFiles(edgePropsConfiguration);\r
+\r
+ if (logger.isDebugEnabled()) {\r
+ logger.debug("Loading DB Edge Rules ");\r
}\r
\r
try {\r
- loadModel ( Version.valueOf ( version ), edgeIngestor, propFiles );\r
+ SchemaVersion schemaVersion = translator.getSchemaVersions().getVersions().stream()\r
+ .filter(s -> s.toString().equalsIgnoreCase(v)).findAny().orElse(null);\r
+\r
+ loadModel(schemaVersion, edgeIngestor, propFiles);\r
} catch (IOException | EdgeRuleNotFoundException e) {\r
throw new CrudException(e.getMessage (), Status.INTERNAL_SERVER_ERROR);\r
}\r
String latestVersionStr = null;\r
for (String versionKey : versionContextMap.keySet ()) {\r
\r
- Matcher matcher = versionPattern.matcher ( versionKey.toUpperCase () );\r
+ Matcher matcher = versionPattern.matcher(versionKey);\r
if (matcher.find ()) {\r
\r
int currentVersion = Integer.parseInt ( matcher.group ( 1 ) );\r
versionContextMap = new ConcurrentHashMap<> ();\r
}\r
\r
- private static synchronized void loadModel ( Version version, EdgeIngestor edgeIngestor, Map<String, File> props)\r
- throws IOException, CrudException, EdgeRuleNotFoundException {\r
+ private static synchronized void loadModel(SchemaVersion version, EdgeIngestor edgeIngestor,\r
+ Map<String, File> props) throws IOException, CrudException, EdgeRuleNotFoundException {\r
\r
Multimap<String, EdgeRule> edges = edgeIngestor.getAllRules ( version );\r
String edgeProps;\r
}\r
}\r
\r
- private static Map<String, File> edgePropertyFiles ( SchemaIngestPropertyReader dir ) throws CrudException {\r
- Map<String, File> propsFiles = Arrays.stream ( new File ( dir.getEdgePropsDir () )\r
+ private static Map<String, File> edgePropertyFiles ( EdgePropsConfiguration edgePropsConfiguration ) throws CrudException {\r
+ Map<String, File> propsFiles = Arrays.stream ( new File ( edgePropsConfiguration.getEdgePropsDir () )\r
.listFiles ( ( d, name ) -> propsFilePattern.matcher ( name ).matches () ) )\r
.collect ( Collectors.toMap ( new Function<File, String> () {\r
- public String apply ( File f ) {\r
+ @Override\r
+ public String apply ( File f ) {\r
Matcher m1 = propsVersionPattern.matcher ( f.getName () );\r
m1.find ();\r
return m1.group ( 0 );\r