Implement support for v10 model entities.
[aai/model-loader.git] / src / main / java / org / openecomp / modelloader / service / ModelLoaderService.java
index 16aba7e..118cd18 100644 (file)
-/*-
- * ============LICENSE_START=======================================================
- * MODEL LOADER SERVICE
- * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.openecomp.modelloader.service;
-
-import org.openecomp.sdc.api.IDistributionClient;
-import org.openecomp.sdc.api.results.IDistributionClientResult;
-import org.openecomp.sdc.impl.DistributionClientFactory;
-import org.openecomp.sdc.utils.DistributionActionResultEnum;
-
-import org.openecomp.cl.api.Logger;
-import org.openecomp.cl.eelf.LoggerFactory;
-import org.openecomp.modelloader.config.ModelLoaderConfig;
-import org.openecomp.modelloader.entity.model.ModelArtifactHandler;
-import org.openecomp.modelloader.notification.EventCallback;
-
-import java.io.IOException;
-import java.util.Properties;
-
-import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
-import javax.servlet.http.HttpServletRequest;
-import javax.ws.rs.core.Response;
-
-/**
- * Service class in charge of managing the negotiating model loading
- * capabilities between AAI and an ASDC.
- */
-public class ModelLoaderService implements ModelLoaderInterface {
-
-  protected static String CONFIG_FILE = "model-loader.properties";
-
-  private IDistributionClient client;
-  private ModelLoaderConfig config;
-
-  static Logger logger = LoggerFactory.getInstance().getLogger(ModelLoaderService.class.getName());
-
-  /**
-   * Responsible for loading configuration files and calling initialization.
-   */
-  @PostConstruct
-  protected void start() {
-    // Load model loader system configuration
-    logger.info(ModelLoaderMsgs.LOADING_CONFIGURATION);
-    Properties configProperties = new Properties();
-    try {
-      configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
-    } catch (IOException e) {
-      String errorMsg = "Failed to load configuration: " + e.getMessage();
-      logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
-      shutdown();
-    }
-    
-    config = new ModelLoaderConfig(configProperties);
-    init();
-  }
-
-  /**
-   * Responsible for stopping the connection to the distribution client before
-   * the resource is destroyed.
-   */
-  @PreDestroy
-  protected void preShutdownOperations() {
-    logger.info(ModelLoaderMsgs.STOPPING_CLIENT);
-    if (client != null) {
-      client.stop();
-    }
-  }
-
-  /**
-   * Responsible for loading configuration files, initializing model
-   * distribution clients, and starting them.
-   */
-  protected void init() {
-    // Initialize distribution client
-    logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client...");
-    client = DistributionClientFactory.createDistributionClient();
-    IDistributionClientResult initResult = client.init(config, new EventCallback(client, config));
-    if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
-      String errorMsg = "Failed to initialize distribution client: "
-          + initResult.getDistributionMessageResult();
-      logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
-      shutdown();
-    }
-
-    // Start distribution client
-    logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");
-    IDistributionClientResult startResult = client.start();
-    if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
-      String errorMsg = "Failed to start distribution client: "
-          + startResult.getDistributionMessageResult();
-      logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
-      shutdown();
-    }
-
-    logger.debug(ModelLoaderMsgs.INITIALIZING,
-        "Succcessfully loaded service: " + this.getClass().getSimpleName());
-  }
-
-  /**
-   * Shut down the process.
-   */
-  private void shutdown() {
-    preShutdownOperations();
-
-    // TODO: Find a better way to shut down the model loader.
-    try {
-      // Give logs time to write to file
-      Thread.sleep(2000);
-    } catch (InterruptedException e) {
-      // Nothing we can do at this point
-    }
-
-    Runtime.getRuntime().halt(1);
-  }
-
-  /** (non-Javadoc)
-   * @see org.openecomp.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)
-   */
-  @Override
-  public Response loadModel(String modelid) {
-    Response response = Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();
-
-    return response;
-  }
-
-  /** (non-Javadoc)
-   * @see org.openecomp.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)
-   */
-  @Override
-  public Response saveModel(String modelid, String modelname) {
-    Response response = Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}")
-        .build();
-
-    return response;
-  }
-
-  @Override
-  public Response ingestModel(String modelid, HttpServletRequest req, String payload)
-      throws IOException {
-    Response response;
-
-    if (config.getIngestSimulatorEnabled()) {
-      logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact");
-
-      ModelArtifactHandler handler = new ModelArtifactHandler(config);
-      handler.loadModelTest(payload.getBytes());
-
-      response = Response.ok().build();
-    } else {
-      logger.debug("Simulation interface disabled");
-      response = Response.serverError().build();
-    }
-
-    return response;
-  }
-}
+/**\r
+ * ============LICENSE_START=======================================================\r
+ * Model Loader\r
+ * ================================================================================\r
+ * Copyright © 2017 AT&T Intellectual Property.\r
+ * Copyright © 2017 Amdocs\r
+ * All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ *\r
+ * ECOMP and OpenECOMP are trademarks\r
+ * and service marks of AT&T Intellectual Property.\r
+ */\r
+package org.openecomp.modelloader.service;\r
+\r
+import org.openecomp.sdc.api.IDistributionClient;\r
+import org.openecomp.sdc.api.results.IDistributionClientResult;\r
+import org.openecomp.sdc.impl.DistributionClientFactory;\r
+import org.openecomp.sdc.utils.DistributionActionResultEnum;\r
+\r
+import org.openecomp.cl.api.Logger;\r
+import org.openecomp.cl.eelf.LoggerFactory;\r
+import org.openecomp.modelloader.config.ModelLoaderConfig;\r
+import org.openecomp.modelloader.entity.model.ModelArtifactHandler;\r
+import org.openecomp.modelloader.notification.EventCallback;\r
+\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.util.Properties;\r
+\r
+import javax.annotation.PreDestroy;\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.ws.rs.core.Response;\r
+\r
+/**\r
+ * Service class in charge of managing the negotiating model loading\r
+ * capabilities between AAI and an ASDC.\r
+ */\r
+public class ModelLoaderService implements ModelLoaderInterface {\r
+       \r
+       protected static final String FILESEP = (System.getProperty("file.separator") == null) ? "/"\r
+            : System.getProperty("file.separator");\r
+\r
+       protected static final String CONFIG_DIR = System.getProperty("CONFIG_HOME") + FILESEP;\r
+       protected static final String CONFIG_AUTH_LOCATION = CONFIG_DIR + "auth" + FILESEP;\r
+       protected static final String CONFIG_FILE = CONFIG_DIR + "model-loader.properties";\r
+\r
+       private IDistributionClient client;\r
+       private ModelLoaderConfig config;\r
+\r
+       static Logger logger = LoggerFactory.getInstance().getLogger(ModelLoaderService.class.getName());\r
+\r
+       /**\r
+        * Responsible for loading configuration files and calling initialization.\r
+        */\r
+       public ModelLoaderService() {\r
+               start();\r
+       }\r
+\r
+       protected void start() {\r
+               // Load model loader system configuration\r
+               logger.info(ModelLoaderMsgs.LOADING_CONFIGURATION);\r
+               Properties configProperties = new Properties();\r
+               try {\r
+                       configProperties.load(new FileInputStream(CONFIG_FILE));\r
+               } catch (IOException e) {\r
+                       String errorMsg = "Failed to load configuration: " + e.getMessage();\r
+                       logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);\r
+                       shutdown();\r
+               }\r
+\r
+               config = new ModelLoaderConfig(configProperties, CONFIG_AUTH_LOCATION);\r
+               init();\r
+       }\r
+\r
+       @Override\r
+       public void finalize() {\r
+               preShutdownOperations();\r
+       }\r
+       \r
+       /**\r
+        * Responsible for stopping the connection to the distribution client before\r
+        * the resource is destroyed.\r
+        */\r
+       protected void preShutdownOperations() {\r
+               logger.info(ModelLoaderMsgs.STOPPING_CLIENT);\r
+               if (client != null) {\r
+                       client.stop();\r
+               }\r
+       }\r
+\r
+       /**\r
+        * Responsible for loading configuration files, initializing model\r
+        * distribution clients, and starting them.\r
+        */\r
+       protected void init() {\r
+               // Initialize distribution client\r
+               logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client...");\r
+               client = DistributionClientFactory.createDistributionClient();\r
+               IDistributionClientResult initResult = client.init(config, new EventCallback(client, config));\r
+               if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {\r
+                       String errorMsg = "Failed to initialize distribution client: "\r
+                                       + initResult.getDistributionMessageResult();\r
+                       logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);\r
+                       shutdown();\r
+               }\r
+\r
+               // Start distribution client\r
+               logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");\r
+               IDistributionClientResult startResult = client.start();\r
+               if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {\r
+                       String errorMsg = "Failed to start distribution client: "\r
+                                       + startResult.getDistributionMessageResult();\r
+                       logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);\r
+                       shutdown();\r
+               }\r
+\r
+               logger.debug(ModelLoaderMsgs.INITIALIZING,\r
+                               "Succcessfully loaded service: " + this.getClass().getSimpleName());\r
+       }\r
+\r
+       /**\r
+        * Shut down the process.\r
+        */\r
+       private void shutdown() {\r
+               preShutdownOperations();\r
+\r
+               // TODO: Find a better way to shut down the model loader.\r
+               try {\r
+                       // Give logs time to write to file\r
+                       Thread.sleep(2000);\r
+               } catch (InterruptedException e) {\r
+                       // Nothing we can do at this point\r
+               }\r
+\r
+               Runtime.getRuntime().halt(1);\r
+       }\r
+\r
+       /** (non-Javadoc)\r
+        * @see org.openecomp.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)\r
+        */\r
+       @Override\r
+       public Response loadModel(String modelid) {\r
+               Response response = Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();\r
+\r
+               return response;\r
+       }\r
+\r
+       /** (non-Javadoc)\r
+        * @see org.openecomp.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)\r
+        */\r
+       @Override\r
+       public Response saveModel(String modelid, String modelname) {\r
+               Response response = Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}")\r
+                               .build();\r
+\r
+               return response;\r
+       }\r
+\r
+       @Override\r
+       public Response ingestModel(String modelid, HttpServletRequest req, String payload)\r
+                       throws IOException {\r
+               Response response;\r
+\r
+               if (config.getIngestSimulatorEnabled()) {\r
+                       logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact");\r
+\r
+                       ModelArtifactHandler handler = new ModelArtifactHandler(config);\r
+                       handler.loadModelTest(payload.getBytes());\r
+\r
+                       response = Response.ok().build();\r
+               } else {\r
+                       logger.debug("Simulation interface disabled");\r
+                       response = Response.serverError().build();\r
+               }\r
+\r
+               return response;\r
+       }\r
+}\r