X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fschema%2FEdgeRulesLoader.java;h=2c139a7ce276926392c5ba88270a80110fdc2bc8;hb=refs%2Fchanges%2F09%2F73809%2F2;hp=7f08949d787e48292774e760160ba127a34bc7f2;hpb=18f7af386802e750ad983ba2cdf4d9b84b4ed5ed;p=aai%2Fgizmo.git diff --git a/src/main/java/org/onap/schema/EdgeRulesLoader.java b/src/main/java/org/onap/schema/EdgeRulesLoader.java index 7f08949..2c139a7 100644 --- a/src/main/java/org/onap/schema/EdgeRulesLoader.java +++ b/src/main/java/org/onap/schema/EdgeRulesLoader.java @@ -31,35 +31,48 @@ import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; + import javax.ws.rs.core.Response.Status; + import org.apache.commons.io.IOUtils; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.edges.EdgeIngestor; import org.onap.aai.edges.EdgeRule; import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; import org.onap.aai.setup.ConfigTranslator; -import org.onap.aai.setup.SchemaLocationsBean; -import org.onap.aai.setup.Version; +import org.onap.aai.setup.SchemaVersion; import org.onap.crud.exception.CrudException; import org.onap.crud.logging.CrudServiceMsgs; -import org.onap.schema.util.SchemaIngestPropertyReader; +import org.springframework.beans.factory.annotation.Autowired; + import com.google.common.collect.Multimap; public class EdgeRulesLoader { + + private static ConfigTranslator configTranslator; + private static EdgeIngestor edgeIngestor; + + private static EdgePropsConfiguration edgePropsConfiguration; private static Map versionContextMap = new ConcurrentHashMap<> (); - static final Pattern versionPattern = Pattern.compile ( "V(\\d*)" ); + static final Pattern versionPattern = Pattern.compile("(?i)v(\\d*)"); static final String propsPrefix = "edge_properties_"; static final String propsSuffix = ".json"; final static Pattern propsFilePattern = Pattern.compile ( propsPrefix + "(.*)" + propsSuffix ); - final static Pattern propsVersionPattern = Pattern.compile ( "v\\d*" ); + final static Pattern propsVersionPattern = Pattern.compile("(?i)v(\\d*)"); private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance ().getLogger ( EdgeRulesLoader.class.getName () ); - private EdgeRulesLoader () { + private EdgeRulesLoader () { } + + @Autowired + public EdgeRulesLoader(ConfigTranslator configTranslator, EdgeIngestor edgeIngestor, EdgePropsConfiguration edgePropsConfiguration) { + EdgeRulesLoader.configTranslator = configTranslator; + EdgeRulesLoader.edgeIngestor = edgeIngestor; + EdgeRulesLoader.edgePropsConfiguration = edgePropsConfiguration; } /** @@ -68,20 +81,17 @@ public class EdgeRulesLoader { * @throws CrudException */ public static synchronized void loadModels () throws CrudException { - SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader (); - SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean (); - schemaLocationsBean.setEdgeDirectory ( schemaIngestPropertyReader.getEdgeDir () ); - ConfigTranslator configTranslator = new OxmModelConfigTranslator ( schemaLocationsBean ); - EdgeIngestor edgeIngestor = new EdgeIngestor ( configTranslator ); - Map propFiles = edgePropertyFiles(schemaIngestPropertyReader); - - if (logger.isDebugEnabled ()) { - logger.debug ( "Loading DB Edge Rules" ); + Map propFiles = edgePropertyFiles(edgePropsConfiguration); + + if (logger.isDebugEnabled()) { + logger.debug("Loading DB Edge Rules"); } - for (String version : OxmModelLoader.getLoadedOXMVersions ()) { + for (String version : OxmModelLoader.getLoadedOXMVersions()) { try { - loadModel ( Version.valueOf ( version ), edgeIngestor, propFiles ); + SchemaVersion schemaVersion = configTranslator.getSchemaVersions().getVersions().stream() + .filter(s -> s.toString().equalsIgnoreCase(version)).findAny().orElse(null); + loadModel(schemaVersion, edgeIngestor, propFiles); } catch (IOException | EdgeRuleNotFoundException e) { throw new CrudException(e.getMessage (), e); } @@ -95,20 +105,17 @@ public class EdgeRulesLoader { */ public static synchronized void loadModels ( String v ) throws CrudException { - SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader (); - SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean (); - schemaLocationsBean.setEdgeDirectory ( schemaIngestPropertyReader.getEdgeDir () ); - ConfigTranslator configTranslator = new OxmModelConfigTranslator ( schemaLocationsBean ); - EdgeIngestor edgeIngestor = new EdgeIngestor ( configTranslator ); - String version = v.toUpperCase (); - Map propFiles = edgePropertyFiles(schemaIngestPropertyReader); - - if (logger.isDebugEnabled ()) { - logger.debug ( "Loading DB Edge Rules " ); + Map propFiles = edgePropertyFiles(edgePropsConfiguration); + + if (logger.isDebugEnabled()) { + logger.debug("Loading DB Edge Rules "); } try { - loadModel ( Version.valueOf ( version ), edgeIngestor, propFiles ); + SchemaVersion schemaVersion = configTranslator.getSchemaVersions().getVersions().stream() + .filter(s -> s.toString().equalsIgnoreCase(v)).findAny().orElse(null); + + loadModel(schemaVersion, edgeIngestor, propFiles); } catch (IOException | EdgeRuleNotFoundException e) { throw new CrudException(e.getMessage (), Status.INTERNAL_SERVER_ERROR); } @@ -175,7 +182,7 @@ public class EdgeRulesLoader { String latestVersionStr = null; for (String versionKey : versionContextMap.keySet ()) { - Matcher matcher = versionPattern.matcher ( versionKey.toUpperCase () ); + Matcher matcher = versionPattern.matcher(versionKey); if (matcher.find ()) { int currentVersion = Integer.parseInt ( matcher.group ( 1 ) ); @@ -199,8 +206,8 @@ public class EdgeRulesLoader { versionContextMap = new ConcurrentHashMap<> (); } - private static synchronized void loadModel ( Version version, EdgeIngestor edgeIngestor, Map props) - throws IOException, CrudException, EdgeRuleNotFoundException { + private static synchronized void loadModel(SchemaVersion version, EdgeIngestor edgeIngestor, + Map props) throws IOException, CrudException, EdgeRuleNotFoundException { Multimap edges = edgeIngestor.getAllRules ( version ); String edgeProps; @@ -216,11 +223,12 @@ public class EdgeRulesLoader { } } - private static Map edgePropertyFiles ( SchemaIngestPropertyReader dir ) throws CrudException { - Map propsFiles = Arrays.stream ( new File ( dir.getEdgePropsDir () ) + private static Map edgePropertyFiles ( EdgePropsConfiguration edgePropsConfiguration ) throws CrudException { + Map propsFiles = Arrays.stream ( new File ( edgePropsConfiguration.getEdgePropsDir () ) .listFiles ( ( d, name ) -> propsFilePattern.matcher ( name ).matches () ) ) .collect ( Collectors.toMap ( new Function () { - public String apply ( File f ) { + @Override + public String apply ( File f ) { Matcher m1 = propsVersionPattern.matcher ( f.getName () ); m1.find (); return m1.group ( 0 );