diff options
Diffstat (limited to 'src/main/java')
31 files changed, 1104 insertions, 456 deletions
diff --git a/src/main/java/org/onap/clamp/authorization/AuthorizationController.java b/src/main/java/org/onap/clamp/authorization/AuthorizationController.java index 4a35f4583..2e43495b7 100644 --- a/src/main/java/org/onap/clamp/authorization/AuthorizationController.java +++ b/src/main/java/org/onap/clamp/authorization/AuthorizationController.java @@ -30,7 +30,7 @@ import com.att.eelf.configuration.EELFManager; import java.util.Date; -import javax.ws.rs.NotAuthorizedException; +import org.onap.clamp.clds.exception.NotAuthorizedException; import org.apache.camel.Exchange; import org.onap.clamp.clds.config.ClampProperties; @@ -57,7 +57,7 @@ public class AuthorizationController { @Autowired private ClampProperties refProp; - private static final String PERM_PREFIX = "security.permission.type."; + public static final String PERM_PREFIX = "security.permission.type."; private static final String PERM_INSTANCE = "security.permission.instance"; /** diff --git a/src/main/java/org/onap/clamp/clds/Application.java b/src/main/java/org/onap/clamp/clds/Application.java index f6dfdc0c3..bac328d6d 100644 --- a/src/main/java/org/onap/clamp/clds/Application.java +++ b/src/main/java/org/onap/clamp/clds/Application.java @@ -29,15 +29,21 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import java.io.IOException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.Enumeration; import org.apache.catalina.connector.Connector; import org.onap.clamp.clds.model.properties.Holmes; import org.onap.clamp.clds.model.properties.ModelProperties; import org.onap.clamp.clds.util.ClampVersioning; import org.onap.clamp.clds.util.ResourceFileUtil; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @@ -51,6 +57,7 @@ import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; +import org.springframework.core.env.Environment; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @@ -82,6 +89,9 @@ public class Application extends SpringBootServletInitializer { @Value("${server.ssl.key-store:none}") private String sslKeystoreFile; + @Autowired + private Environment env; + @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); @@ -102,14 +112,15 @@ public class Application extends SpringBootServletInitializer { * This method is used to declare the camel servlet. * * @return A servlet bean - * @throws IOException IO Exception + * @throws IOException + * IO Exception */ @Bean public ServletRegistrationBean camelServletRegistrationBean() throws IOException { - eelfLogger.info(ResourceFileUtil.getResourceAsString("boot-message.txt") + "(v" - + ClampVersioning.getCldsVersionFromProps() + ")" + System.getProperty("line.separator")); - ServletRegistrationBean registration = new ServletRegistrationBean(new ClampServlet(), - "/restservices/clds/*"); + eelfLogger.info( + ResourceFileUtil.getResourceAsString("boot-message.txt") + "(v" + ClampVersioning.getCldsVersionFromProps() + + ")" + System.getProperty("line.separator") + getSslExpirationDate()); + ServletRegistrationBean registration = new ServletRegistrationBean(new ClampServlet(), "/restservices/clds/*"); registration.setName("CamelServlet"); return registration; } @@ -135,9 +146,8 @@ public class Application extends SpringBootServletInitializer { private Connector createRedirectConnector(int redirectSecuredPort) { if (redirectSecuredPort <= 0) { - eelfLogger.warn( - "HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1" - + " (Connector disabled)"); + eelfLogger.warn("HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1" + + " (Connector disabled)"); return null; } Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); @@ -147,4 +157,33 @@ public class Application extends SpringBootServletInitializer { connector.setRedirectPort(redirectSecuredPort); return connector; } + + private String getSslExpirationDate() throws IOException { + StringBuilder result = new StringBuilder(" :: SSL Certificates :: "); + try { + if (env.getProperty("server.ssl.key-store") != null) { + + KeyStore keystore = KeyStore.getInstance(env.getProperty("server.ssl.key-store-type")); + keystore.load( + ResourceFileUtil + .getResourceAsStream(env.getProperty("server.ssl.key-store").replaceAll("classpath:", "")), + env.getProperty("server.ssl.key-store-password").toCharArray()); + Enumeration<String> aliases = keystore.aliases(); + while (aliases.hasMoreElements()) { + String alias = aliases.nextElement(); + if ("X.509".equals(keystore.getCertificate(alias).getType())) { + result.append("* " + alias + " expires " + + ((X509Certificate) keystore.getCertificate(alias)).getNotAfter() + + System.getProperty("line.separator")); + } + } + } else { + result.append("* NONE HAS been configured"); + } + } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException e) { + eelfLogger.warn("SSL certificate access error ", e); + + } + return result.toString(); + } } diff --git a/src/main/java/org/onap/clamp/clds/ClampServlet.java b/src/main/java/org/onap/clamp/clds/ClampServlet.java index 90d0693d1..86524d1c6 100644 --- a/src/main/java/org/onap/clamp/clds/ClampServlet.java +++ b/src/main/java/org/onap/clamp/clds/ClampServlet.java @@ -27,6 +27,15 @@ package org.onap.clamp.clds; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; + +import java.io.IOException; +import java.security.Principal; +import java.util.ArrayList; +import java.util.List; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + import org.apache.camel.component.servlet.CamelHttpTransportServlet; import org.onap.clamp.clds.service.SecureServicePermission; import org.springframework.context.ApplicationContext; @@ -39,14 +48,6 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.User; import org.springframework.web.context.support.WebApplicationContextUtils; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.security.Principal; -import java.util.ArrayList; -import java.util.List; - public class ClampServlet extends CamelHttpTransportServlet { /** @@ -100,7 +101,8 @@ public class ClampServlet extends CamelHttpTransportServlet { permissionList.add(SecureServicePermission .create(applicationContext.getEnvironment().getProperty(PERM_TOSCA), cldsPermissionInstance, READ)); permissionList.add(SecureServicePermission - .create(applicationContext.getEnvironment().getProperty(PERM_TOSCA), cldsPermissionInstance, UPDATE)); + .create(applicationContext.getEnvironment().getProperty(PERM_TOSCA), cldsPermissionInstance, + UPDATE)); } return permissionList; } @@ -122,8 +124,8 @@ public class ClampServlet extends CamelHttpTransportServlet { grantedAuths.add(new SimpleGrantedAuthority(permString)); } } - Authentication auth = new UsernamePasswordAuthenticationToken(new User(principal.getName(), "", grantedAuths), "", - grantedAuths); + Authentication auth = new UsernamePasswordAuthenticationToken(new User(principal.getName(), "", + grantedAuths), "", grantedAuths); SecurityContextHolder.getContext().setAuthentication(auth); } try { diff --git a/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java b/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java index 3a98788f5..271dc84ff 100644 --- a/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java +++ b/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java @@ -22,11 +22,34 @@ package org.onap.clamp.clds.config; +import java.io.IOException; +import java.net.URL; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; + import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.http4.HttpClientConfigurer; +import org.apache.camel.component.http4.HttpComponent; import org.apache.camel.model.rest.RestBindingMode; +import org.apache.http.config.Registry; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.scheme.SchemeRegistry; +import org.apache.http.conn.socket.ConnectionSocketFactory; +import org.apache.http.conn.socket.PlainConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLSocketFactory; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.BasicHttpClientConnectionManager; import org.onap.clamp.clds.util.ClampVersioning; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component @@ -35,15 +58,70 @@ public class CamelConfiguration extends RouteBuilder { @Autowired CamelContext camelContext; + @Autowired + private Environment env; + + private void configureDefaultSslProperties() { + if (env.getProperty("server.ssl.trust-store") != null) { + URL storeResource = Thread.currentThread().getContextClassLoader() + .getResource(env.getProperty("server.ssl.trust-store").replaceAll("classpath:", "")); + System.setProperty("javax.net.ssl.trustStore", storeResource.getPath()); + System.setProperty("javax.net.ssl.trustStorePassword", env.getProperty("server.ssl.trust-store-password")); + System.setProperty("javax.net.ssl.trustStoreType", "jks"); + System.setProperty("ssl.TrustManagerFactory.algorithm", "PKIX"); + storeResource = Thread.currentThread().getContextClassLoader() + .getResource(env.getProperty("server.ssl.key-store").replaceAll("classpath:", "")); + System.setProperty("javax.net.ssl.keyStore", storeResource.getPath()); + System.setProperty("javax.net.ssl.keyStorePassword", env.getProperty("server.ssl.key-store-password")); + System.setProperty("javax.net.ssl.keyStoreType", env.getProperty("server.ssl.key-store-type")); + } + } + + private void registerTrustStore() + throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException, CertificateException, IOException { + if (env.getProperty("server.ssl.trust-store") != null) { + KeyStore truststore = KeyStore.getInstance("JKS"); + truststore.load( + Thread.currentThread().getContextClassLoader() + .getResourceAsStream(env.getProperty("server.ssl.trust-store").replaceAll("classpath:", "")), + env.getProperty("server.ssl.trust-store-password").toCharArray()); + + TrustManagerFactory trustFactory = TrustManagerFactory.getInstance("PKIX"); + trustFactory.init(truststore); + SSLContext sslcontext = SSLContext.getInstance("TLS"); + sslcontext.init(null, trustFactory.getTrustManagers(), null); + SSLSocketFactory factory = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + SchemeRegistry registry = new SchemeRegistry(); + final Scheme scheme = new Scheme("https4", 443, factory); + registry.register(scheme); + ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory(); + HttpComponent http4 = camelContext.getComponent("https4", HttpComponent.class); + http4.setHttpClientConfigurer(new HttpClientConfigurer() { + + @Override + public void configureHttpClient(HttpClientBuilder builder) { + builder.setSSLSocketFactory(factory); + Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() + .register("https", factory).register("http", plainsf).build(); + builder.setConnectionManager(new BasicHttpClientConnectionManager(registry)); + } + }); + } + } + @Override - public void configure() { + public void configure() + throws KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { restConfiguration().component("servlet").bindingMode(RestBindingMode.json).jsonDataFormat("clamp-gson") .dataFormatProperty("prettyPrint", "true")// .enableCORS(true) // turn on swagger api-doc .apiContextPath("api-doc").apiVendorExtension(true).apiProperty("api.title", "Clamp Rest API") .apiProperty("api.version", ClampVersioning.getCldsVersionFromProps()) .apiProperty("base.path", "/restservices/clds/"); - // .apiProperty("cors", "true"); - camelContext.setTracing(true); + + // camelContext.setTracing(true); + + configureDefaultSslProperties(); + registerTrustStore(); } } diff --git a/src/main/java/org/onap/clamp/clds/dao/CldsDao.java b/src/main/java/org/onap/clamp/clds/dao/CldsDao.java index 44228b226..16a6a748c 100644 --- a/src/main/java/org/onap/clamp/clds/dao/CldsDao.java +++ b/src/main/java/org/onap/clamp/clds/dao/CldsDao.java @@ -352,7 +352,7 @@ public class CldsDao { } /** - * Helper method to setup the base template properties + * Helper method to setup the base template properties. * * @param template * the template @@ -474,7 +474,7 @@ public class CldsDao { } /** - * Helper method to setup the event prop to the CldsEvent class + * Helper method to setup the event prop to the CldsEvent class. * * @param event * the clds event @@ -742,12 +742,13 @@ public class CldsDao { String dictElementShortName) { SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); List<CldsDictionaryItem> dictionaryItems = new ArrayList<>(); - String dictionarySql = new StringBuilder("SELECT de.dict_element_id, de.dictionary_id, de.dict_element_name, " + - "de.dict_element_short_name, de.dict_element_description, de.dict_element_type, de.created_by, " + - "de.modified_by, de.timestamp FROM dictionary_elements de, " + - "dictionary d WHERE de.dictionary_id = d.dictionary_id") + String dictionarySql = new StringBuilder("SELECT de.dict_element_id, de.dictionary_id, de.dict_element_name, " + + "de.dict_element_short_name, de.dict_element_description, de.dict_element_type, de.created_by, " + + "de.modified_by, de.timestamp FROM dictionary_elements de, " + + "dictionary d WHERE de.dictionary_id = d.dictionary_id") .append((dictionaryId != null) ? (" AND d.dictionary_id = '" + dictionaryId + "'") : "") - .append((dictElementShortName != null) ? (" AND de.dict_element_short_name = '" + dictElementShortName + "'") : "") + .append((dictElementShortName != null) ? (" AND de.dict_element_short_name = '" + dictElementShortName + + "'") : "") .append((dictionaryName != null) ? (" AND dictionary_name = '" + dictionaryName + "'") : "").toString(); List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(dictionarySql); @@ -780,8 +781,8 @@ public class CldsDao { */ public Map<String, String> getDictionaryElementsByType(String dictionaryElementType) { Map<String, String> dictionaryItems = new HashMap<>(); - String dictionarySql = new StringBuilder("SELECT dict_element_name, dict_element_short_name " + - "FROM dictionary_elements WHERE dict_element_type = '") + String dictionarySql = new StringBuilder("SELECT dict_element_name, dict_element_short_name " + + "FROM dictionary_elements WHERE dict_element_type = '") .append(dictionaryElementType).append("'").toString(); List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(dictionarySql); diff --git a/src/main/java/org/onap/clamp/clds/model/dcae/DcaeInventoryResponse.java b/src/main/java/org/onap/clamp/clds/model/dcae/DcaeInventoryResponse.java index 74582a865..972450665 100644 --- a/src/main/java/org/onap/clamp/clds/model/dcae/DcaeInventoryResponse.java +++ b/src/main/java/org/onap/clamp/clds/model/dcae/DcaeInventoryResponse.java @@ -24,12 +24,17 @@ package org.onap.clamp.clds.model.dcae; +import com.google.gson.annotations.Expose; + /** * This class maps the DCAE inventory answer to a nice pojo. */ public class DcaeInventoryResponse { + @Expose private String typeName; + + @Expose private String typeId; public String getTypeName() { diff --git a/src/main/java/org/onap/clamp/clds/model/dcae/DcaeLinks.java b/src/main/java/org/onap/clamp/clds/model/dcae/DcaeLinks.java new file mode 100644 index 000000000..368e1b8e6 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/model/dcae/DcaeLinks.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.onap.clamp.clds.model.dcae; + +import com.google.gson.annotations.Expose; + +public class DcaeLinks { + @Expose + private String self; + @Expose + private String status; + @Expose + private String uninstall; + + public String getSelf() { + return self; + } + + public void setSelf(String self) { + this.self = self; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getUninstall() { + return uninstall; + } + + public void setUninstall(String uninstall) { + this.uninstall = uninstall; + } + +} diff --git a/src/main/java/org/onap/clamp/clds/model/dcae/DcaeOperationStatusResponse.java b/src/main/java/org/onap/clamp/clds/model/dcae/DcaeOperationStatusResponse.java new file mode 100644 index 000000000..aee7d0613 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/model/dcae/DcaeOperationStatusResponse.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.onap.clamp.clds.model.dcae; + +import com.google.gson.annotations.Expose; + +/** + * This class maps the DCAE deployment handler response to a nice pojo. + */ +public class DcaeOperationStatusResponse { + + @Expose + private String operationType; + + @Expose + private String status; + + @Expose + private String requestId; + + @Expose + private String error; + + @Expose + private DcaeLinks links; + + public String getOperationType() { + return operationType; + } + + public void setOperationType(String operationType) { + this.operationType = operationType; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getRequestId() { + return requestId; + } + + public void setRequestId(String requestId) { + this.requestId = requestId; + } + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } + + public DcaeLinks getLinks() { + return links; + } + + public void setLinks(DcaeLinks links) { + this.links = links; + } + +} diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java index 809904f22..3792c1720 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java @@ -29,6 +29,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import java.util.AbstractMap; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -52,21 +53,23 @@ public class BlueprintParser { private static final String TYPE = "type"; private static final String PROPERTIES = "properties"; private static final String NAME = "name"; - private static final String POLICYID = "policy_id"; - private static final String POLICY_TYPEID = "policy_type_id"; + private static final String INPUT = "inputs"; + private static final String GET_INPUT = "get_input"; + private static final String POLICY_MODELID = "policy_model_id"; private static final String RELATIONSHIPS = "relationships"; private static final String CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM = "clamp_node.relationships.gets_input_from"; private static final String TARGET = "target"; public Set<MicroService> getMicroServices(String blueprintString) { Set<MicroService> microServices = new HashSet<>(); - JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString); - JsonObject results = jsonObject.get(NODE_TEMPLATES).getAsJsonObject(); + JsonObject blueprintJson = BlueprintParser.convertToJson(blueprintString); + JsonObject nodeTemplateList = blueprintJson.get(NODE_TEMPLATES).getAsJsonObject(); + JsonObject inputList = blueprintJson.get(INPUT).getAsJsonObject(); - for (Entry<String, JsonElement> entry : results.entrySet()) { + for (Entry<String, JsonElement> entry : nodeTemplateList.entrySet()) { JsonObject nodeTemplate = entry.getValue().getAsJsonObject(); if (nodeTemplate.get(TYPE).getAsString().contains(DCAE_NODES)) { - MicroService microService = getNodeRepresentation(entry); + MicroService microService = getNodeRepresentation(entry, nodeTemplateList, inputList); microServices.add(microService); } } @@ -89,7 +92,7 @@ public class BlueprintParser { } String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA; return Collections - .singletonList(new MicroService(msName, "onap.policies.monitoring.cdap.tca.hi.lo.app", "", "", "")); + .singletonList(new MicroService(msName, "onap.policies.monitoring.cdap.tca.hi.lo.app", "", "")); } String getName(Entry<String, JsonElement> entry) { @@ -118,30 +121,48 @@ public class BlueprintParser { return ""; } - String getModelType(Entry<String, JsonElement> entry) { + String findModelTypeInTargetArray(JsonArray jsonArray, JsonObject nodeTemplateList, JsonObject inputList) { + for (JsonElement elem : jsonArray) { + String modelType = getModelType( + new AbstractMap.SimpleEntry<String, JsonElement>(elem.getAsJsonObject().get(TARGET).getAsString(), + nodeTemplateList.get(elem.getAsJsonObject().get(TARGET).getAsString()).getAsJsonObject()), + nodeTemplateList, inputList); + if (!modelType.isEmpty()) { + return modelType; + } + } + return ""; + } + + String getModelType(Entry<String, JsonElement> entry, JsonObject nodeTemplateList, JsonObject inputList) { JsonObject ob = entry.getValue().getAsJsonObject(); + // Search first in this node template if (ob.has(PROPERTIES)) { JsonObject properties = ob.get(PROPERTIES).getAsJsonObject(); - if (properties.has(POLICYID)) { - JsonObject policyIdObj = properties.get(POLICYID).getAsJsonObject(); - if (policyIdObj.has(POLICY_TYPEID)) { - return policyIdObj.get(POLICY_TYPEID).getAsString(); + if (properties.has(POLICY_MODELID)) { + if (properties.get(POLICY_MODELID).isJsonObject()) { + // it's a blueprint parameter + return inputList.get(properties.get(POLICY_MODELID).getAsJsonObject().get(GET_INPUT).getAsString()) + .getAsJsonObject().get("default").getAsString(); + } else { + // It's a direct value + return properties.get(POLICY_MODELID).getAsString(); } } } + // Or it's may be defined in a relationship + if (ob.has(RELATIONSHIPS)) { + return findModelTypeInTargetArray(ob.get(RELATIONSHIPS).getAsJsonArray(), nodeTemplateList, inputList); + } return ""; } - String getBlueprintName(Entry<String, JsonElement> entry) { - return entry.getKey(); - } - - MicroService getNodeRepresentation(Entry<String, JsonElement> entry) { + MicroService getNodeRepresentation(Entry<String, JsonElement> entry, JsonObject nodeTemplateList, + JsonObject inputList) { String name = getName(entry); String getInputFrom = getInput(entry); - String modelType = getModelType(entry); - String blueprintName = getBlueprintName(entry); - return new MicroService(name, modelType, getInputFrom, "", blueprintName); + String modelType = getModelType(entry, nodeTemplateList, inputList); + return new MicroService(name, modelType, getInputFrom, ""); } private String getTarget(JsonObject elementObject) { diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java index ac4daeffb..9bc7a022a 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java @@ -29,16 +29,14 @@ import java.util.Objects; public class MicroService { private final String name; private final String modelType; - private final String blueprintName; private final String inputFrom; private String mappedNameJpa; - public MicroService(String name, String modelType, String inputFrom, String mappedNameJpa, String blueprintName) { + public MicroService(String name, String modelType, String inputFrom, String mappedNameJpa) { this.name = name; this.inputFrom = inputFrom; this.mappedNameJpa = mappedNameJpa; - this.modelType = modelType; - this.blueprintName = blueprintName; + this.modelType = modelType; } public String getName() { @@ -53,15 +51,10 @@ public class MicroService { return inputFrom; } - public String getBlueprintName() { - return blueprintName; - } - @Override public String toString() { return "MicroService{" + "name='" + name + '\'' + ", modelType='" + modelType + '\'' + ", inputFrom='" - + inputFrom + '\'' + ", mappedNameJpa='" + mappedNameJpa + '\'' + ", blueprintName='" - + blueprintName + '\'' + '}'; + + inputFrom + '\'' + ", mappedNameJpa='" + mappedNameJpa + '\'' + '}'; } public String getMappedNameJpa() { @@ -81,11 +74,12 @@ public class MicroService { return false; } MicroService that = (MicroService) o; - return name.equals(that.name) && modelType.equals(that.modelType) && inputFrom.equals(that.inputFrom) && mappedNameJpa.equals(that.mappedNameJpa) && blueprintName.equals(that.blueprintName); + return name.equals(that.name) && modelType.equals(that.modelType) && inputFrom.equals(that.inputFrom) + && mappedNameJpa.equals(that.mappedNameJpa); } @Override public int hashCode() { - return Objects.hash(name, modelType, inputFrom, mappedNameJpa, blueprintName); + return Objects.hash(name, modelType, inputFrom, mappedNameJpa); } } diff --git a/src/main/java/org/onap/clamp/clds/service/CldsService.java b/src/main/java/org/onap/clamp/clds/service/CldsService.java index e81cc15f7..63a913314 100644 --- a/src/main/java/org/onap/clamp/clds/service/CldsService.java +++ b/src/main/java/org/onap/clamp/clds/service/CldsService.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights * reserved. * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ * 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 @@ -203,7 +205,7 @@ public class CldsService extends SecureServiceBase { public List<CldsMonitoringDetails> getCldsDetails() { util.entering(request, "CldsService: GET model details"); Date startTime = new Date(); - List<CldsMonitoringDetails> cldsMonitoringDetailsList = cldsDao.getCldsMonitoringDetails(); + final List<CldsMonitoringDetails> cldsMonitoringDetailsList = cldsDao.getCldsMonitoringDetails(); // audit log LoggingUtils.setTimeContext(startTime, new Date()); auditLogger.info("GET cldsDetails completed"); @@ -223,7 +225,7 @@ public class CldsService extends SecureServiceBase { LoggingUtils.setTimeContext(startTime, new Date()); CldsInfoProvider cldsInfoProvider = new CldsInfoProvider(this); - CldsInfo cldsInfo = cldsInfoProvider.getCldsInfo(); + final CldsInfo cldsInfo = cldsInfoProvider.getCldsInfo(); // audit log LoggingUtils.setTimeContext(startTime, new Date()); @@ -245,7 +247,7 @@ public class CldsService extends SecureServiceBase { Date startTime = new Date(); isAuthorized(permissionReadCl); logger.info("GET bpmnText for modelName={}", modelName); - CldsModel model = CldsModel.retrieve(cldsDao, modelName, false); + final CldsModel model = CldsModel.retrieve(cldsDao, modelName, false); // audit log LoggingUtils.setTimeContext(startTime, new Date()); auditLogger.info("GET model bpmn completed"); @@ -266,7 +268,7 @@ public class CldsService extends SecureServiceBase { Date startTime = new Date(); isAuthorized(permissionReadCl); logger.info("GET imageText for modelName={}", modelName); - CldsModel model = CldsModel.retrieve(cldsDao, modelName, false); + final CldsModel model = CldsModel.retrieve(cldsDao, modelName, false); // audit log LoggingUtils.setTimeContext(startTime, new Date()); auditLogger.info("GET model image completed"); @@ -282,7 +284,7 @@ public class CldsService extends SecureServiceBase { */ public CldsModel getModel(String modelName) { util.entering(request, "CldsService: GET model"); - Date startTime = new Date(); + final Date startTime = new Date(); isAuthorized(permissionReadCl); logger.debug("GET model for modelName={}", modelName); CldsModel cldsModel = CldsModel.retrieve(cldsDao, modelName, false); @@ -323,7 +325,7 @@ public class CldsService extends SecureServiceBase { */ public CldsModel putModel(String modelName, CldsModel cldsModel) { util.entering(request, "CldsService: PUT model"); - Date startTime = new Date(); + final Date startTime = new Date(); isAuthorized(permissionUpdateCl); isAuthorizedForVf(cldsModel); logger.info("PUT model for modelName={}", modelName); @@ -350,7 +352,7 @@ public class CldsService extends SecureServiceBase { Date startTime = new Date(); isAuthorized(permissionReadCl); logger.info("GET list of model names"); - List<ValueItem> names = cldsDao.getModelNames(); + final List<ValueItem> names = cldsDao.getModelNames(); // audit log LoggingUtils.setTimeContext(startTime, new Date()); auditLogger.info("GET model names completed"); @@ -409,11 +411,11 @@ public class CldsService extends SecureServiceBase { model.save(cldsDao, getUserId()); // get vars and format if necessary - String prop = model.getPropText(); - String bpmn = model.getBpmnText(); - String docText = model.getDocText(); - String controlName = model.getControlName(); - String bpmnJson = cldsBpmnTransformer.doXslTransformToString(bpmn); + final String prop = model.getPropText(); + final String bpmn = model.getBpmnText(); + final String docText = model.getDocText(); + final String controlName = model.getControlName(); + final String bpmnJson = cldsBpmnTransformer.doXslTransformToString(bpmn); logger.info("PUT bpmnJson={}", bpmnJson); // Test flag coming from UI or from Clamp config boolean isTest = Boolean.parseBoolean(test) @@ -471,7 +473,7 @@ public class CldsService extends SecureServiceBase { */ public String postDcaeEvent(String test, DcaeEvent dcaeEvent) { util.entering(request, "CldsService: Post dcae event"); - Date startTime = new Date(); + final Date startTime = new Date(); String userid = null; // TODO: allow auth checking to be turned off by removing the permission // type property diff --git a/src/main/java/org/onap/clamp/clds/service/CldsTemplateService.java b/src/main/java/org/onap/clamp/clds/service/CldsTemplateService.java index f60c63830..d107731b0 100644 --- a/src/main/java/org/onap/clamp/clds/service/CldsTemplateService.java +++ b/src/main/java/org/onap/clamp/clds/service/CldsTemplateService.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights * reserved. * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ * 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 @@ -79,7 +81,7 @@ public class CldsTemplateService extends SecureServiceBase { Date startTime = new Date(); isAuthorized(permissionReadTemplate); logger.info("GET bpmnText for templateName=" + templateName); - CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false); + final CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false); // audit log LoggingUtils.setTimeContext(startTime, new Date()); auditLogger.info("GET template bpmn completed"); @@ -100,7 +102,7 @@ public class CldsTemplateService extends SecureServiceBase { Date startTime = new Date(); isAuthorized(permissionReadTemplate); logger.info("GET imageText for templateName=" + templateName); - CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false); + final CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false); // audit log LoggingUtils.setTimeContext(startTime, new Date()); auditLogger.info("GET template image completed"); @@ -116,7 +118,7 @@ public class CldsTemplateService extends SecureServiceBase { */ public CldsTemplate getTemplate(String templateName) { util.entering(request, "CldsTemplateService: GET template"); - Date startTime = new Date(); + final Date startTime = new Date(); isAuthorized(permissionReadTemplate); logger.info("GET model for templateName=" + templateName); CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false); @@ -137,7 +139,7 @@ public class CldsTemplateService extends SecureServiceBase { */ public CldsTemplate putTemplate(String templateName, CldsTemplate cldsTemplate) { util.entering(request, "CldsTemplateService: PUT template"); - Date startTime = new Date(); + final Date startTime = new Date(); isAuthorized(permissionUpdateTemplate); logger.info("PUT Template for templateName=" + templateName); logger.info("PUT bpmnText=" + cldsTemplate.getBpmnText()); @@ -162,7 +164,7 @@ public class CldsTemplateService extends SecureServiceBase { Date startTime = new Date(); isAuthorized(permissionReadTemplate); logger.info("GET list of template names"); - List<ValueItem> names = cldsDao.getTemplateNames(); + final List<ValueItem> names = cldsDao.getTemplateNames(); // audit log LoggingUtils.setTimeContext(startTime, new Date()); auditLogger.info("GET template names completed"); diff --git a/src/main/java/org/onap/clamp/clds/service/CldsToscaService.java b/src/main/java/org/onap/clamp/clds/service/CldsToscaService.java index f2c75ead1..81bafef47 100644 --- a/src/main/java/org/onap/clamp/clds/service/CldsToscaService.java +++ b/src/main/java/org/onap/clamp/clds/service/CldsToscaService.java @@ -5,6 +5,8 @@ * Copyright (C) 2018 AT&T Intellectual Property. All rights * reserved. * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ * 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 @@ -84,7 +86,7 @@ public class CldsToscaService extends SecureServiceBase { * type */ public ResponseEntity<?> parseToscaModelAndSave(String toscaModelName, CldsToscaModel cldsToscaModel) { - Date startTime = new Date(); + final Date startTime = new Date(); LoggingUtils.setRequestContext("CldsToscaService: Parse Tosca model and save", getPrincipalName()); // TODO revisit based on new permissions isAuthorized(permissionUpdateTosca); @@ -107,7 +109,7 @@ public class CldsToscaService extends SecureServiceBase { LoggingUtils.setRequestContext("CldsToscaService: Get All tosca models", getPrincipalName()); // TODO revisit based on new permissions isAuthorized(permissionReadTosca); - List<CldsToscaModel> cldsToscaModels = Optional.ofNullable(cldsDao.getAllToscaModels()).get(); + final List<CldsToscaModel> cldsToscaModels = Optional.ofNullable(cldsDao.getAllToscaModels()).get(); LoggingUtils.setTimeContext(startTime, new Date()); LoggingUtils.setResponseContext("0", "Get All tosca models success", this.getClass().getName()); auditLogger.info("Get All tosca models"); @@ -128,7 +130,8 @@ public class CldsToscaService extends SecureServiceBase { LoggingUtils.setRequestContext("CldsToscaService: Get tosca models by model name", getPrincipalName()); // TODO revisit based on new permissions isAuthorized(permissionReadTosca); - List<CldsToscaModel> cldsToscaModels = Optional.ofNullable(cldsDao.getToscaModelByName(toscaModelName)).get(); + final List<CldsToscaModel> cldsToscaModels = Optional.ofNullable(cldsDao.getToscaModelByName(toscaModelName)) + .get(); LoggingUtils.setTimeContext(startTime, new Date()); LoggingUtils.setResponseContext("0", "Get tosca models by model name success", this.getClass().getName()); auditLogger.info("GET tosca models by model name completed"); @@ -140,6 +143,7 @@ public class CldsToscaService extends SecureServiceBase { * from the database. * * @param policyType + * The type of the policy * @return clds tosca model - CLDS tosca model for a given policy type */ public CldsToscaModel getToscaModelsByPolicyType(String policyType) { @@ -147,7 +151,8 @@ public class CldsToscaService extends SecureServiceBase { LoggingUtils.setRequestContext("CldsToscaService: Get tosca models by policyType", getPrincipalName()); // TODO revisit based on new permissions isAuthorized(permissionReadTosca); - List<CldsToscaModel> cldsToscaModels = Optional.ofNullable(cldsDao.getToscaModelByPolicyType(policyType)).get(); + final List<CldsToscaModel> cldsToscaModels = Optional.ofNullable(cldsDao.getToscaModelByPolicyType(policyType)) + .get(); LoggingUtils.setTimeContext(startTime, new Date()); LoggingUtils.setResponseContext("0", "Get tosca models by policyType success", this.getClass().getName()); auditLogger.info("GET tosca models by policyType completed"); diff --git a/src/main/java/org/onap/clamp/clds/util/CryptoUtils.java b/src/main/java/org/onap/clamp/clds/util/CryptoUtils.java index f08bf7b28..85aae0a5d 100644 --- a/src/main/java/org/onap/clamp/clds/util/CryptoUtils.java +++ b/src/main/java/org/onap/clamp/clds/util/CryptoUtils.java @@ -162,7 +162,7 @@ public final class CryptoUtils { private static SecretKeySpec readSecretKeySpec(String propertiesFileName) { Properties props = new Properties(); try { - //Workaround fix to make encryption key configurable + // Workaround fix to make encryption key configurable // System environment variable takes precedence for over clds/key.properties String encryptionKey = System.getenv(AES_ENCRYPTION_KEY); if(encryptionKey != null && encryptionKey.trim().length() > 0) { diff --git a/src/main/java/org/onap/clamp/clds/util/XmlTools.java b/src/main/java/org/onap/clamp/clds/util/XmlTools.java index a812fa127..a7d4ed9fb 100644 --- a/src/main/java/org/onap/clamp/clds/util/XmlTools.java +++ b/src/main/java/org/onap/clamp/clds/util/XmlTools.java @@ -24,6 +24,7 @@ package org.onap.clamp.clds.util; import java.io.StringWriter; +import javax.xml.XMLConstants; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; @@ -39,6 +40,12 @@ import org.w3c.dom.Document; public class XmlTools { /** + * Private constructor to avoid creating instances of util class. + */ + private XmlTools(){ + } + + /** * Transforms document to XML string. * * @param doc XML document @@ -47,6 +54,7 @@ public class XmlTools { public static String exportXmlDocumentAsString(Document doc) { try { TransformerFactory tf = TransformerFactory.newInstance(); + tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); diff --git a/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java b/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java index 1ece484b3..7a1f122ed 100755 --- a/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java +++ b/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java @@ -40,6 +40,7 @@ public class AwtUtils { private static final int FONT_STYLE = Font.PLAIN; private static final String FONT_FACE = "SansSerif"; private static final Color TRANSPARENT = new Color(0.0f, 0.0f, 0.0f, 0.0f); + private static final int TEXT_PADDING = 5; private AwtUtils() { } @@ -51,7 +52,7 @@ public class AwtUtils { g2d.setColor(TRANSPARENT); g2d.fill(rect); g2d.setColor(oldColor); - addText(g2d, text, point.x + width / 2, point.y + height / 2); + addText(g2d, text, rect); } static void drawArrow(Graphics2D g2d, Point from, Point to, int lineThickness) { @@ -61,17 +62,30 @@ public class AwtUtils { g2d.fillPolygon(new int[]{x2 - ARROW_W, x2 - ARROW_W, x2}, new int[]{to.y - ARROW_H, to.y + ARROW_H, to.y}, 3); } - private static void addText(Graphics2D g2d, String text, int abs, int ord) { + private static void addText(Graphics2D g2d, String text, Rectangle rect) { + int textBoundingBoxLimit = rect.width - 2* TEXT_PADDING; Font font = new Font(FONT_FACE, FONT_STYLE, FONT_SIZE); - g2d.setFont(font); - - FontMetrics fm1 = g2d.getFontMetrics(); - int w1 = fm1.stringWidth(text); - int x1 = abs - (w1 / 2); + font = scaleFontToFit(text, textBoundingBoxLimit, g2d, font); + Font oldFont = g2d.getFont(); g2d.setFont(font); g2d.setColor(Color.BLACK); - g2d.drawString(text, x1, ord); + FontMetrics fm1 = g2d.getFontMetrics(); + float x1 = rect.x + (float)(rect.width - fm1.stringWidth(text)) / 2; + float y1 = rect.y + (float)(rect.height - fm1.getHeight()) / 2 + fm1.getAscent(); + g2d.drawString(text, x1, y1); + + g2d.setFont(oldFont); + } + + private static Font scaleFontToFit(String text, int width, Graphics2D g2d, Font pFont) { + float fontSize = pFont.getSize(); + float fWidth = g2d.getFontMetrics(pFont).stringWidth(text); + if(fWidth <= width) { + return pFont; + } + fontSize = ((float)width / fWidth) * fontSize; + return pFont.deriveFont(fontSize); } } diff --git a/src/main/java/org/onap/clamp/clds/util/drawing/ImageBuilder.java b/src/main/java/org/onap/clamp/clds/util/drawing/ImageBuilder.java index ce21c4cfd..5d37701fb 100644 --- a/src/main/java/org/onap/clamp/clds/util/drawing/ImageBuilder.java +++ b/src/main/java/org/onap/clamp/clds/util/drawing/ImageBuilder.java @@ -36,6 +36,7 @@ public class ImageBuilder { public static final int POLICY_LINE_RATIO = 2; public static final int COLLECTOR_LINE_RATIO = 6; public static final float MS_LINE_TO_HEIGHT_RATIO = 0.75f; + public static final float ARROW_TO_BASELINE_RATIO = 0.75f; private Point currentPoint; private final int baseLength; @@ -68,7 +69,7 @@ public class ImageBuilder { ImageBuilder arrow() { String dataElementId = "Arrow-" + UUID.randomUUID().toString(); - Point to = new Point(currentPoint.x + baseLength, currentPoint.y); + Point to = new Point(currentPoint.x + (int)(baseLength*ARROW_TO_BASELINE_RATIO), currentPoint.y); AwtUtils.drawArrow(g2d, currentPoint, to, LINE_THICKNESS); documentBuilder.pushChangestoDocument(g2d, dataElementId); currentPoint = to; diff --git a/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java b/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java index fe2d5cb34..ebb267f7b 100755 --- a/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java +++ b/src/main/java/org/onap/clamp/clds/util/drawing/Painter.java @@ -43,6 +43,7 @@ public class Painter { private static final int THICK_LINE = 4; private static final double RECT_RATIO = 3.0 / 2.0; private static final int CIRCLE_RADIUS = 17; + private static final int MINIMUM_BASE_LENGTH = 120; /** * Constructor to create instance of Painter. @@ -60,11 +61,14 @@ public class Painter { int numOfRectangles = 2 + microServices.size(); int numOfArrows = numOfRectangles + 1; int baseLength = (canvasSize - 2 * CIRCLE_RADIUS) / (numOfArrows + numOfRectangles); + if(baseLength < MINIMUM_BASE_LENGTH) { + baseLength = MINIMUM_BASE_LENGTH; + } int rectHeight = (int) (baseLength / RECT_RATIO); adjustGraphics2DProperties(); - Point origin = new Point(0, rectHeight / 2); + Point origin = new Point(1, rectHeight / 2); ImageBuilder ib = new ImageBuilder(g2d, documentBuilder, origin, baseLength, rectHeight); doTheActualDrawing(collector, microServices, policy, ib); diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java index 0041c589e..2393f2498 100644 --- a/src/main/java/org/onap/clamp/loop/Loop.java +++ b/src/main/java/org/onap/clamp/loop/Loop.java @@ -23,16 +23,16 @@ package org.onap.clamp.loop; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; import java.io.Serializable; -import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; -import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; import javax.persistence.CascadeType; import javax.persistence.Column; @@ -46,11 +46,16 @@ import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.Table; +import javax.persistence.Transient; +import org.hibernate.annotations.SortNatural; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; +import org.onap.clamp.loop.components.external.DcaeComponent; +import org.onap.clamp.loop.components.external.ExternalComponent; +import org.onap.clamp.loop.components.external.PolicyComponent; import org.onap.clamp.loop.log.LoopLog; import org.onap.clamp.policy.microservice.MicroServicePolicy; import org.onap.clamp.policy.operational.OperationalPolicy; @@ -104,6 +109,10 @@ public class Loop implements Serializable { private LoopState lastComputedState; @Expose + @Transient + private final Map<String, ExternalComponent> components = new HashMap<>(); + + @Expose @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop") private Set<OperationalPolicy> operationalPolicies = new HashSet<>(); @@ -114,9 +123,16 @@ public class Loop implements Serializable { @Expose @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop") - private Set<LoopLog> loopLogs = new HashSet<>(); + @SortNatural + private SortedSet<LoopLog> loopLogs = new TreeSet<>(); + + private void initializeExternalComponents() { + this.addComponent(new PolicyComponent()); + this.addComponent(new DcaeComponent()); + } public Loop() { + initializeExternalComponents(); } /** @@ -128,6 +144,7 @@ public class Loop implements Serializable { this.blueprint = blueprint; this.lastComputedState = LoopState.DESIGN; this.globalPropertiesJson = new JsonObject(); + initializeExternalComponents(); } public String getName() { @@ -206,7 +223,7 @@ public class Loop implements Serializable { return loopLogs; } - void setLoopLogs(Set<LoopLog> loopLogs) { + void setLoopLogs(SortedSet<LoopLog> loopLogs) { this.loopLogs = loopLogs; } @@ -220,9 +237,9 @@ public class Loop implements Serializable { microServicePolicy.getUsedByLoops().add(this); } - void addLog(LoopLog log) { - loopLogs.add(log); + public void addLog(LoopLog log) { log.setLoop(this); + this.loopLogs.add(log); } public String getDcaeBlueprintId() { @@ -241,6 +258,18 @@ public class Loop implements Serializable { this.modelPropertiesJson = modelPropertiesJson; } + public Map<String, ExternalComponent> getComponents() { + return components; + } + + public ExternalComponent getComponent(String componentName) { + return this.components.get(componentName); + } + + public void addComponent(ExternalComponent component) { + this.components.put(component.getComponentName(), component); + } + /** * Generate the loop name. * @@ -261,45 +290,6 @@ public class Loop implements Serializable { return buffer.toString().replace('.', '_').replaceAll(" ", ""); } - /** - * Generates the Json that must be sent to policy to add all policies to Active - * PDP group. - * - * @return The json, payload to send - */ - public String createPoliciesPayloadPdpGroup() { - JsonObject jsonObject = new JsonObject(); - JsonArray jsonArray = new JsonArray(); - jsonObject.add("policies", jsonArray); - - for (String policyName : this.listPolicyNamesPdpGroup()) { - JsonObject policyNode = new JsonObject(); - jsonArray.add(policyNode); - policyNode.addProperty("policy-id", policyName); - } - return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject); - } - - /** - * Generates the list of policy names that must be send/remove to/from active - * PDP group. - * - * @return A list of policy names - */ - public List<String> listPolicyNamesPdpGroup() { - List<String> policyNamesList = new ArrayList<>(); - for (OperationalPolicy opPolicy : this.getOperationalPolicies()) { - policyNamesList.add(opPolicy.getName()); - for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) { - policyNamesList.add(guardName); - } - } - for (MicroServicePolicy microServicePolicy : this.getMicroServicePolicies()) { - policyNamesList.add(microServicePolicy.getName()); - } - return policyNamesList; - } - @Override public int hashCode() { final int prime = 31; diff --git a/src/main/java/org/onap/clamp/loop/LoopOperation.java b/src/main/java/org/onap/clamp/loop/LoopOperation.java deleted file mode 100644 index c3eb08be7..000000000 --- a/src/main/java/org/onap/clamp/loop/LoopOperation.java +++ /dev/null @@ -1,297 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2019 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.onap.clamp.loop; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; - -import java.io.IOException; -import java.lang.reflect.Array; -import java.util.Collection; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; -import org.onap.clamp.clds.config.ClampProperties; -import org.onap.clamp.policy.operational.OperationalPolicy; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import org.yaml.snakeyaml.Yaml; - -/** - * Closed loop operations. - */ -@Component -public class LoopOperation { - - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(LoopOperation.class); - protected static final EELFLogger auditLogger = EELFManager.getInstance().getMetricsLogger(); - private static final String DCAE_LINK_FIELD = "links"; - private static final String DCAE_STATUS_FIELD = "status"; - private static final String DCAE_DEPLOYMENT_TEMPLATE = "dcae.deployment.template"; - private static final String DCAE_SERVICETYPE_ID = "serviceTypeId"; - private static final String DCAE_INPUTS = "inputs"; - private static final String DCAE_DEPLOYMENT_PREFIX = "closedLoop_"; - private static final String DCAE_DEPLOYMENT_SUFIX = "_deploymentId"; - private final LoopService loopService; - private final ClampProperties refProp; - - public enum TempLoopState { - NOT_SUBMITTED, SUBMITTED, DEPLOYED, NOT_DEPLOYED, PROCESSING, IN_ERROR; - } - - /** - * The constructor. - * @param loopService The loop service - * @param refProp The clamp properties - */ - @Autowired - public LoopOperation(LoopService loopService, ClampProperties refProp) { - this.loopService = loopService; - this.refProp = refProp; - } - - /** - * Get the payload used to send the deploy closed loop request. - * - * @param loop The loop - * @return The payload used to send deploy closed loop request - * @throws IOException IOException - */ - public String getDeployPayload(Loop loop) throws IOException { - Yaml yaml = new Yaml(); - Map<String, Object> yamlMap = yaml.load(loop.getBlueprint()); - JsonObject bluePrint = wrapSnakeObject(yamlMap).getAsJsonObject(); - - String serviceTypeId = loop.getDcaeBlueprintId(); - - JsonObject rootObject = refProp.getJsonTemplate(DCAE_DEPLOYMENT_TEMPLATE).getAsJsonObject(); - rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId); - if (bluePrint != null) { - rootObject.add(DCAE_INPUTS, bluePrint); - } - String apiBodyString = rootObject.toString(); - logger.info("Dcae api Body String - " + apiBodyString); - - return apiBodyString; - } - - /** - * Get the deployment id. - * - * @param loop The loop - * @return The deployment id - * @throws IOException IOException - */ - public String getDeploymentId(Loop loop) { - // Set the deploymentId if not present yet - String deploymentId = ""; - // If model is already deployed then pass same deployment id - if (loop.getDcaeDeploymentId() != null && !loop.getDcaeDeploymentId().isEmpty()) { - deploymentId = loop.getDcaeDeploymentId(); - } else { - deploymentId = DCAE_DEPLOYMENT_PREFIX + loop.getName() + DCAE_DEPLOYMENT_SUFIX; - } - return deploymentId; - } - - /** - * Update the loop info. - * - * @param camelExchange The camel exchange - * @param loop The loop - * @param deploymentId The deployment id - * @throws ParseException The parse exception - */ - public void updateLoopInfo(Exchange camelExchange, Loop loop, String deploymentId) throws ParseException { - Message in = camelExchange.getIn(); - String msg = in.getBody(String.class); - - JSONParser parser = new JSONParser(); - Object obj0 = parser.parse(msg); - JSONObject jsonObj = (JSONObject) obj0; - - JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD); - String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD); - - // use http4 instead of http, because camel http4 component is used to do the http call - String newStatusUrl = statusUrl.replaceAll("http:", "http4:"); - - loop.setDcaeDeploymentId(deploymentId); - loop.setDcaeDeploymentStatusUrl(newStatusUrl); - loopService.saveOrUpdateLoop(loop); - } - - /** - * Get the Closed Loop status based on the reply from Policy. - * - * @param statusCode The status code - * @return The state based on policy response - * @throws ParseException The parse exception - */ - public String analysePolicyResponse(int statusCode) { - if (statusCode == 200) { - return TempLoopState.SUBMITTED.toString(); - } else if (statusCode == 404) { - return TempLoopState.NOT_SUBMITTED.toString(); - } - return TempLoopState.IN_ERROR.toString(); - } - - /** - * Get the name of the first Operational policy. - * - * @param loop The closed loop - * @return The name of the first operational policy - */ - public String getOperationalPolicyName(Loop loop) { - Set<OperationalPolicy> opSet = (Set<OperationalPolicy>)loop.getOperationalPolicies(); - Iterator<OperationalPolicy> iterator = opSet.iterator(); - while (iterator.hasNext()) { - OperationalPolicy policy = iterator.next(); - return policy.getName(); - } - return null; - } - - /** - * Get the Closed Loop status based on the reply from DCAE. - * - * @param camelExchange The camel exchange - * @return The state based on DCAE response - * @throws ParseException The parse exception - */ - public String analyseDcaeResponse(Exchange camelExchange, Integer statusCode) throws ParseException { - if (statusCode == null) { - return TempLoopState.NOT_DEPLOYED.toString(); - } - if (statusCode == 200) { - Message in = camelExchange.getIn(); - String msg = in.getBody(String.class); - - JSONParser parser = new JSONParser(); - Object obj0 = parser.parse(msg); - JSONObject jsonObj = (JSONObject) obj0; - - String opType = (String) jsonObj.get("operationType"); - String status = (String) jsonObj.get("status"); - - // status = processing/successded/failed - if (status.equals("succeeded")) { - if (opType.equals("install")) { - return TempLoopState.DEPLOYED.toString(); - } else if (opType.equals("uninstall")) { - return TempLoopState.NOT_DEPLOYED.toString(); - } - } else if (status.equals("processing")) { - return TempLoopState.PROCESSING.toString(); - } - } else if (statusCode == 404) { - return TempLoopState.NOT_DEPLOYED.toString(); - } - return TempLoopState.IN_ERROR.toString(); - } - - /** - * Update the status of the closed loop based on the response from Policy and DCAE. - * - * @param loop The closed loop - * @param policyState The state get from Policy - * @param dcaeState The state get from DCAE - * @throws ParseException The parse exception - */ - public LoopState updateLoopStatus(Loop loop, TempLoopState policyState, TempLoopState dcaeState) { - LoopState clState = LoopState.IN_ERROR; - if (policyState == TempLoopState.SUBMITTED) { - if (dcaeState == TempLoopState.DEPLOYED) { - clState = LoopState.DEPLOYED; - } else if (dcaeState == TempLoopState.PROCESSING) { - clState = LoopState.WAITING; - } else if (dcaeState == TempLoopState.NOT_DEPLOYED) { - clState = LoopState.SUBMITTED; - } - } else if (policyState == TempLoopState.NOT_SUBMITTED) { - if (dcaeState == TempLoopState.NOT_DEPLOYED) { - clState = LoopState.DESIGN; - } - } - loop.setLastComputedState(clState); - loopService.saveOrUpdateLoop(loop); - return clState; - } - - private JsonElement wrapSnakeObject(Object obj) { - // NULL => JsonNull - if (obj == null) { - return JsonNull.INSTANCE; - } - - // Collection => JsonArray - if (obj instanceof Collection) { - JsonArray array = new JsonArray(); - for (Object childObj : (Collection<?>) obj) { - array.add(wrapSnakeObject(childObj)); - } - return array; - } - - // Array => JsonArray - if (obj.getClass().isArray()) { - JsonArray array = new JsonArray(); - - int length = Array.getLength(array); - for (int i = 0; i < length; i++) { - array.add(wrapSnakeObject(Array.get(array, i))); - } - return array; - } - - // Map => JsonObject - if (obj instanceof Map) { - Map<?, ?> map = (Map<?, ?>) obj; - - JsonObject jsonObject = new JsonObject(); - for (final Map.Entry<?, ?> entry : map.entrySet()) { - final String name = String.valueOf(entry.getKey()); - final Object value = entry.getValue(); - jsonObject.add(name, wrapSnakeObject(value)); - } - return jsonObject; - } - - // otherwise take it as a string - return new JsonPrimitive(String.valueOf(obj)); - } - -} diff --git a/src/main/java/org/onap/clamp/loop/LoopService.java b/src/main/java/org/onap/clamp/loop/LoopService.java index 4c1392253..d1ab0e396 100644 --- a/src/main/java/org/onap/clamp/loop/LoopService.java +++ b/src/main/java/org/onap/clamp/loop/LoopService.java @@ -71,6 +71,17 @@ public class LoopService { loopsRepository.deleteById(loopName); } + public void updateDcaeDeploymentFields(Loop loop, String deploymentId, String deploymentUrl) { + loop.setDcaeDeploymentId(deploymentId); + loop.setDcaeDeploymentStatusUrl(deploymentUrl); + loopsRepository.save(loop); + } + + public void updateLoopState(Loop loop, String newState) { + loop.setLastComputedState(LoopState.valueOf(newState)); + loopsRepository.save(loop); + } + Loop updateAndSaveOperationalPolicies(String loopName, List<OperationalPolicy> newOperationalPolicies) { Loop loop = findClosedLoopByName(loopName); Set<OperationalPolicy> newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies); @@ -93,9 +104,7 @@ public class LoopService { MicroServicePolicy updateMicroservicePolicy(String loopName, MicroServicePolicy newMicroservicePolicy) { Loop loop = findClosedLoopByName(loopName); - MicroServicePolicy newPolicies = microservicePolicyService.getAndUpdateMicroServicePolicy(loop, - newMicroservicePolicy); - return newPolicies; + return microservicePolicyService.getAndUpdateMicroServicePolicy(loop, newMicroservicePolicy); } private Loop findClosedLoopByName(String loopName) { diff --git a/src/main/java/org/onap/clamp/loop/components/external/DcaeComponent.java b/src/main/java/org/onap/clamp/loop/components/external/DcaeComponent.java new file mode 100644 index 000000000..35b3a454b --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/components/external/DcaeComponent.java @@ -0,0 +1,162 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.onap.clamp.loop.components.external; + +import com.google.gson.JsonObject; + +import java.util.UUID; + +import org.apache.camel.Exchange; +import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse; +import org.onap.clamp.clds.util.JsonUtils; +import org.onap.clamp.loop.Loop; + +public class DcaeComponent extends ExternalComponent { + + private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_"; + private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters"; + private static final String DCAE_SERVICETYPE_ID = "serviceTypeId"; + private static final String DCAE_INPUTS = "inputs"; + + public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED", + "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop"); + public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState( + "PROCESSING_MICROSERVICE_INSTALLATION", + "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it's currently processing the request"); + public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState( + "MICROSERVICE_INSTALLATION_FAILED", + "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed"); + public static final ExternalComponentState MICROSERVICE_INSTALLED_SUCCESSFULLY = new ExternalComponentState( + "MICROSERVICE_INSTALLED_SUCCESSFULLY", + "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully"); + public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState( + "PROCESSING_MICROSERVICE_UNINSTALLATION", + "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it's currently processing the request"); + public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState( + "MICROSERVICE_UNINSTALLATION_FAILED", + "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed"); + public static final ExternalComponentState MICROSERVICE_UNINSTALLED_SUCCESSFULLY = new ExternalComponentState( + "MICROSERVICE_UNINSTALLED_SUCCESSFULLY", + "Clamp has requested DCAE to uninstall the DCAE blueprint and it has been uninstalled successfully"); + public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR", + "There was an error during the request done to DCAE, look at the logs or try again"); + + public DcaeComponent() { + super(BLUEPRINT_DEPLOYED); + } + + @Override + public String getComponentName() { + return "DCAE"; + } + + public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) { + if (responseBody != null && !responseBody.isEmpty()) { + return JsonUtils.GSON_JPA_MODEL.fromJson(responseBody, DcaeOperationStatusResponse.class); + } else { + return null; + } + } + + /** + * Generate the deployment id, it's random + * + * @return The deployment id + */ + public static String generateDeploymentId() { + return DCAE_DEPLOYMENT_PREFIX + UUID.randomUUID(); + } + + /** + * This method prepare the url returned by DCAE to check the status if fine. + * + * @param statusUrl + * @return the Right Url modified if needed + */ + public static String getStatusUrl(DcaeOperationStatusResponse dcaeResponse) { + return dcaeResponse.getLinks().getStatus().replaceAll("http:", "http4:").replaceAll("https:", "https4:"); + } + + /** + * Return the deploy payload for DCAE. + * + * @param loop + * The loop object + * @return The payload used to send deploy closed loop request + */ + public static String getDeployPayload(Loop loop) { + JsonObject globalProp = loop.getGlobalPropertiesJson(); + JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER); + + String serviceTypeId = loop.getDcaeBlueprintId(); + + JsonObject rootObject = new JsonObject(); + rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId); + if (deploymentProp != null) { + rootObject.add(DCAE_INPUTS, deploymentProp); + } + return rootObject.toString(); + } + + /** + * Return the uninstallation payload for DCAE. + * + * @param loop + * The loop object + * @return The payload in string (json) + */ + public static String getUndeployPayload(Loop loop) { + JsonObject rootObject = new JsonObject(); + rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getDcaeBlueprintId()); + return rootObject.toString(); + } + + @Override + public ExternalComponentState computeState(Exchange camelExchange) { + + DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange() + .getProperty("dcaeResponse"); + + if (dcaeResponse == null) { + setState(BLUEPRINT_DEPLOYED); + } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) { + setState(MICROSERVICE_INSTALLED_SUCCESSFULLY); + } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) { + setState(PROCESSING_MICROSERVICE_INSTALLATION); + } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) { + setState(MICROSERVICE_INSTALLATION_FAILED); + } else if (dcaeResponse.getOperationType().equals("uninstall") + && dcaeResponse.getStatus().equals("succeeded")) { + setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY); + } else if (dcaeResponse.getOperationType().equals("uninstall") + && dcaeResponse.getStatus().equals("processing")) { + setState(PROCESSING_MICROSERVICE_UNINSTALLATION); + } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) { + setState(MICROSERVICE_UNINSTALLATION_FAILED); + } else { + setState(IN_ERROR); + } + return this.getState(); + } +} diff --git a/src/main/java/org/onap/clamp/loop/components/external/ExternalComponent.java b/src/main/java/org/onap/clamp/loop/components/external/ExternalComponent.java new file mode 100644 index 000000000..a8aae2038 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/components/external/ExternalComponent.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.onap.clamp.loop.components.external; + +import com.google.gson.annotations.Expose; + +import org.apache.camel.Exchange; + +/** + * + * SHould be abstract but Gson can't instantiate it if it's an abstract + * + */ +public class ExternalComponent { + @Expose + private ExternalComponentState componentState; + + public void setState(ExternalComponentState newState) { + this.componentState = newState; + } + + public ExternalComponentState getState() { + return this.componentState; + } + + public String getComponentName() { + return null; + } + + public ExternalComponentState computeState(Exchange camelExchange) { + return new ExternalComponentState("INIT", "no desc"); + } + + public ExternalComponent(ExternalComponentState initialState) { + setState(initialState); + } + + public ExternalComponent() { + } +} diff --git a/src/main/java/org/onap/clamp/loop/components/external/ExternalComponentState.java b/src/main/java/org/onap/clamp/loop/components/external/ExternalComponentState.java new file mode 100644 index 000000000..6a723c24e --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/components/external/ExternalComponentState.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.onap.clamp.loop.components.external; + +import com.google.gson.annotations.Expose; + +/** + * This is a transient state reflecting the deployment status of a component. It + * can be Policy, DCAE, or whatever... This is object is generic. Clamp is now + * stateless, so it triggers the different components at runtime, the status per + * component is stored here. + * + */ +public class ExternalComponentState { + @Expose + private String stateName; + @Expose + private String description; + + public ExternalComponentState(String stateName, String description) { + this.stateName = stateName; + this.description = description; + } + + public ExternalComponentState() { + } + + public String getStateName() { + return stateName; + } + + public String getDescription() { + return description; + } + + @Override + public String toString() { + return stateName; + } +} diff --git a/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java new file mode 100644 index 000000000..acd6115fe --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java @@ -0,0 +1,123 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.onap.clamp.loop.components.external; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Transient; + +import org.apache.camel.Exchange; +import org.onap.clamp.loop.Loop; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.operational.OperationalPolicy; + +public class PolicyComponent extends ExternalComponent { + + @Transient + private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyComponent.class); + + public static final ExternalComponentState NOT_SENT = new ExternalComponentState("NOT_SENT", + "The policies defined have NOT yet been created on the policy engine"); + public static final ExternalComponentState SENT = new ExternalComponentState("SENT", + "The policies defined have been created but NOT deployed on the policy engine"); + public static final ExternalComponentState SENT_AND_DEPLOYED = new ExternalComponentState("SENT_AND_DEPLOYED", + "The policies defined have been created and deployed on the policy engine"); + public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR", + "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent"); + + public PolicyComponent() { + super(NOT_SENT); + } + + @Override + public String getComponentName() { + return "POLICY"; + } + + /** + * Generates the Json that must be sent to policy to add all policies to Active + * PDP group. + * + * @return The json, payload to send + */ + public static String createPoliciesPayloadPdpGroup(Loop loop) { + JsonObject jsonObject = new JsonObject(); + JsonArray jsonArray = new JsonArray(); + jsonObject.add("policies", jsonArray); + + for (String policyName : PolicyComponent.listPolicyNamesPdpGroup(loop)) { + JsonObject policyNode = new JsonObject(); + jsonArray.add(policyNode); + policyNode.addProperty("policy-id", policyName); + } + String payload = new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject); + logger.info("PdpGroup policy payload: " + payload); + return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject); + } + + /** + * Generates the list of policy names that must be send/remove to/from active + * PDP group. + * + * @return A list of policy names + */ + public static List<String> listPolicyNamesPdpGroup(Loop loop) { + List<String> policyNamesList = new ArrayList<>(); + for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) { + policyNamesList.add(opPolicy.getName()); + for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) { + policyNamesList.add(guardName); + } + } + for (MicroServicePolicy microServicePolicy : loop.getMicroServicePolicies()) { + policyNamesList.add(microServicePolicy.getName()); + } + return policyNamesList; + } + + @Override + public ExternalComponentState computeState(Exchange camelExchange) { + boolean oneNotFound = (boolean) camelExchange.getIn().getExchange().getProperty("atLeastOnePolicyNotFound"); + boolean oneNotDeployed = (boolean) camelExchange.getIn().getExchange() + .getProperty("atLeastOnePolicyNotDeployed"); + + if (oneNotFound && oneNotDeployed) { + this.setState(NOT_SENT); + } else if (!oneNotFound && oneNotDeployed) { + this.setState(SENT); + } else if (!oneNotFound && !oneNotDeployed) { + this.setState(SENT_AND_DEPLOYED); + } else { + this.setState(IN_ERROR); + } + return this.getState(); + } +} diff --git a/src/main/java/org/onap/clamp/loop/log/LoopLog.java b/src/main/java/org/onap/clamp/loop/log/LoopLog.java index cea495712..3feff254d 100644 --- a/src/main/java/org/onap/clamp/loop/log/LoopLog.java +++ b/src/main/java/org/onap/clamp/loop/log/LoopLog.java @@ -52,7 +52,7 @@ import org.onap.clamp.loop.Loop; */ @Entity @Table(name = "loop_logs") -public class LoopLog implements Serializable { +public class LoopLog implements Serializable, Comparable<LoopLog> { /** * The serial version ID. */ @@ -69,6 +69,10 @@ public class LoopLog implements Serializable { private LogType logType; @Expose + @Column(name = "log_component", nullable = false) + private String logComponent; + + @Expose @Column(name = "message", columnDefinition = "MEDIUMTEXT", nullable = false) private String message; @@ -83,10 +87,11 @@ public class LoopLog implements Serializable { public LoopLog() { } - public LoopLog(String message, LogType logType, Loop loop) { + public LoopLog(String message, LogType logType, String logComponent, Loop loop) { this.message = message; this.logType = logType; this.loop = loop; + this.logComponent = logComponent; } public Long getId() { @@ -129,6 +134,14 @@ public class LoopLog implements Serializable { this.logInstant = logInstant.truncatedTo(ChronoUnit.SECONDS); } + public String getLogComponent() { + return logComponent; + } + + public void setLogComponent(String logComponent) { + this.logComponent = logComponent; + } + @Override public int hashCode() { final int prime = 31; @@ -159,4 +172,18 @@ public class LoopLog implements Serializable { return true; } + @Override + public int compareTo(LoopLog arg0) { + // Reverse it, so that by default we have the latest + if (getId() == null) { + return 1; + } + if (arg0.getId() == null) { + return -1; + } + + return arg0.getId().compareTo(this.getId()); + + } + } diff --git a/src/main/java/org/onap/clamp/loop/log/LoopLogService.java b/src/main/java/org/onap/clamp/loop/log/LoopLogService.java index b02bc11c4..d02d0b278 100644 --- a/src/main/java/org/onap/clamp/loop/log/LoopLogService.java +++ b/src/main/java/org/onap/clamp/loop/log/LoopLogService.java @@ -38,7 +38,11 @@ public class LoopLogService { } public void addLog(String message, String logType, Loop loop) { - repository.save(new LoopLog(message, LogType.valueOf(logType), loop)); + this.addLogForComponent(message, logType, "CLAMP", loop); + } + + public void addLogForComponent(String message, String logType, String component, Loop loop) { + loop.addLog(repository.save(new LoopLog(message, LogType.valueOf(logType), component, loop))); } public boolean isExisting(Long logId) { diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java index 2bbb91183..d8d15a5b2 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -23,6 +23,8 @@ package org.onap.clamp.policy.microservice; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; @@ -40,6 +42,7 @@ import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; +import javax.persistence.Transient; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; @@ -61,6 +64,9 @@ public class MicroServicePolicy implements Serializable, Policy { */ private static final long serialVersionUID = 6271238288583332616L; + @Transient + private static final EELFLogger logger = EELFManager.getInstance().getLogger(MicroServicePolicy.class); + @Expose @Id @Column(nullable = false, name = "name", unique = true) @@ -271,7 +277,9 @@ public class MicroServicePolicy implements Serializable, Policy { JsonObject policyProperties = new JsonObject(); policyDetails.add("properties", policyProperties); policyProperties.add(this.getMicroServicePropertyNameFromTosca(toscaJson), this.getProperties()); - return new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult); + String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult); + logger.info("Micro service policy payload: " + policyPayload); + return policyPayload; } } diff --git a/src/main/java/org/onap/clamp/policy/operational/LegacyOperationalPolicy.java b/src/main/java/org/onap/clamp/policy/operational/LegacyOperationalPolicy.java new file mode 100644 index 000000000..33148f0b6 --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/operational/LegacyOperationalPolicy.java @@ -0,0 +1,150 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.onap.clamp.policy.operational; + +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +import org.apache.commons.lang3.math.NumberUtils; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.DumperOptions.ScalarStyle; +import org.yaml.snakeyaml.Yaml; + +/** + * + * This class contains the code required to support the sending of Legacy + * operational payload to policy engine. This will probably disappear in El + * Alto. + * + */ +public class LegacyOperationalPolicy { + + private LegacyOperationalPolicy() { + + } + + private static void translateStringValues(String jsonKey, String stringValue, JsonElement parentJsonElement) { + if (stringValue.equalsIgnoreCase("true") || stringValue.equalsIgnoreCase("false")) { + parentJsonElement.getAsJsonObject().addProperty(jsonKey, Boolean.valueOf(stringValue)); + + } else if (NumberUtils.isParsable(stringValue)) { + parentJsonElement.getAsJsonObject().addProperty(jsonKey, Long.parseLong(stringValue)); + } + } + + private static JsonElement removeAllQuotes(JsonElement jsonElement) { + if (jsonElement.isJsonArray()) { + for (JsonElement element : jsonElement.getAsJsonArray()) { + removeAllQuotes(element); + } + } else if (jsonElement.isJsonObject()) { + for (Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) { + if (entry.getValue().isJsonPrimitive() && entry.getValue().getAsJsonPrimitive().isString()) { + translateStringValues(entry.getKey(), entry.getValue().getAsString(), jsonElement); + } else { + removeAllQuotes(entry.getValue()); + } + } + } + return jsonElement; + } + + public static JsonElement reworkPayloadAttributes(JsonElement policyJson) { + for (JsonElement policy : policyJson.getAsJsonObject().get("policies").getAsJsonArray()) { + JsonElement payloadElem = policy.getAsJsonObject().get("payload"); + String payloadString = payloadElem != null ? payloadElem.getAsString() : ""; + if (!payloadString.isEmpty()) { + Map<String, String> testMap = new Yaml().load(payloadString); + String json = new GsonBuilder().create().toJson(testMap); + policy.getAsJsonObject().add("payload", new GsonBuilder().create().fromJson(json, JsonElement.class)); + } + } + return policyJson; + } + + private static void replacePropertiesIfEmpty(JsonElement policy, String key, String valueIfEmpty) { + JsonElement payloadElem = policy.getAsJsonObject().get(key); + String payloadString = payloadElem != null ? payloadElem.getAsString() : ""; + if (payloadString.isEmpty()) { + policy.getAsJsonObject().addProperty(key, valueIfEmpty); + } + } + + private static JsonElement fulfillPoliciesTreeField(JsonElement policyJson) { + for (JsonElement policy : policyJson.getAsJsonObject().get("policies").getAsJsonArray()) { + replacePropertiesIfEmpty(policy, "success", "final_success"); + replacePropertiesIfEmpty(policy, "failure", "final_failure"); + replacePropertiesIfEmpty(policy, "failure_timeout", "final_failure_timeout"); + replacePropertiesIfEmpty(policy, "failure_retries", "final_failure_retries"); + replacePropertiesIfEmpty(policy, "failure_exception", "final_failure_exception"); + replacePropertiesIfEmpty(policy, "failure_guard", "final_failure_guard"); + } + return policyJson; + } + + private static Map<String, Object> createMap(JsonElement jsonElement) { + Map<String, Object> mapResult = new TreeMap<>(); + + if (jsonElement.isJsonObject()) { + for (Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) { + if (entry.getValue().isJsonPrimitive() && entry.getValue().getAsJsonPrimitive().isString()) { + mapResult.put(entry.getKey(), entry.getValue().getAsString()); + } else if (entry.getValue().isJsonPrimitive() && entry.getValue().getAsJsonPrimitive().isBoolean()) { + mapResult.put(entry.getKey(), entry.getValue().getAsBoolean()); + } else if (entry.getValue().isJsonPrimitive() && entry.getValue().getAsJsonPrimitive().isNumber()) { + // Only int ro long normally, we don't need float here + mapResult.put(entry.getKey(), entry.getValue().getAsLong()); + } else if (entry.getValue().isJsonArray()) { + List<Map<String, Object>> newArray = new ArrayList<>(); + mapResult.put(entry.getKey(), newArray); + for (JsonElement element : entry.getValue().getAsJsonArray()) { + newArray.add(createMap(element)); + } + } else if (entry.getValue().isJsonObject()) { + mapResult.put(entry.getKey(), createMap(entry.getValue())); + } + } + } + return mapResult; + } + + public static String createPolicyPayloadYamlLegacy(JsonElement operationalPolicyJsonElement) { + JsonElement opPolicy = fulfillPoliciesTreeField( + removeAllQuotes(reworkPayloadAttributes(operationalPolicyJsonElement.getAsJsonObject().deepCopy()))); + Map<?, ?> jsonMap = createMap(opPolicy); + DumperOptions options = new DumperOptions(); + options.setDefaultScalarStyle(ScalarStyle.PLAIN); + options.setIndent(2); + options.setPrettyFlow(true); + // Policy can't support { } in the yaml + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + return (new Yaml(options)).dump(jsonMap); + } +} diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java index b2f6109f7..62c5a1e9f 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java @@ -23,6 +23,8 @@ package org.onap.clamp.policy.operational; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; @@ -45,6 +47,7 @@ import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; +import javax.persistence.Transient; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; @@ -52,6 +55,7 @@ import org.hibernate.annotations.TypeDefs; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.Loop; import org.onap.clamp.policy.Policy; +import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @Entity @@ -63,6 +67,9 @@ public class OperationalPolicy implements Serializable, Policy { */ private static final long serialVersionUID = 6117076450841538255L; + @Transient + private static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicy.class); + @Id @Expose @Column(nullable = false, name = "name", unique = true) @@ -176,21 +183,33 @@ public class OperationalPolicy implements Serializable, Policy { operationalPolicyDetails.add("metadata", metadata); metadata.addProperty("policy-id", this.name); - operationalPolicyDetails.add("properties", this.configurationsJson.get("operational_policy")); + operationalPolicyDetails.add("properties", LegacyOperationalPolicy + .reworkPayloadAttributes(this.configurationsJson.get("operational_policy").deepCopy())); Gson gson = new GsonBuilder().create(); + Map<?, ?> jsonMap = gson.fromJson(gson.toJson(policyPayloadResult), Map.class); - return (new Yaml()).dump(jsonMap); + + DumperOptions options = new DumperOptions(); + options.setIndent(2); + options.setPrettyFlow(true); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + + return (new Yaml(options)).dump(jsonMap); } @Override public String createPolicyPayload() throws UnsupportedEncodingException { - // Now the Yaml payload must be injected in a json ... + // Now using the legacy payload fo Dublin JsonObject payload = new JsonObject(); payload.addProperty("policy-id", this.getName()); - payload.addProperty("content", URLEncoder.encode(createPolicyPayloadYaml(), StandardCharsets.UTF_8.toString())); - return new GsonBuilder().setPrettyPrinting().create().toJson(payload); + payload.addProperty("content", URLEncoder.encode( + LegacyOperationalPolicy.createPolicyPayloadYamlLegacy(this.configurationsJson.get("operational_policy")), + StandardCharsets.UTF_8.toString())); + String opPayload = new GsonBuilder().setPrettyPrinting().create().toJson(payload); + logger.info("Operational policy payload: " + opPayload); + return opPayload; } /** @@ -206,10 +225,11 @@ public class OperationalPolicy implements Serializable, Policy { for (Entry<String, JsonElement> guardElem : guardsList.getAsJsonObject().entrySet()) { JsonObject guard = new JsonObject(); guard.addProperty("policy-id", guardElem.getKey()); - guard.add("contents", guardElem.getValue()); + guard.add("content", guardElem.getValue()); result.put(guardElem.getKey(), new GsonBuilder().create().toJson(guard)); } } + logger.info("Guard policy payload: " + result); return result; } diff --git a/src/main/java/org/onap/clamp/util/PrincipalUtils.java b/src/main/java/org/onap/clamp/util/PrincipalUtils.java index d6b20f30b..d6dfacbdb 100644 --- a/src/main/java/org/onap/clamp/util/PrincipalUtils.java +++ b/src/main/java/org/onap/clamp/util/PrincipalUtils.java @@ -38,6 +38,12 @@ public class PrincipalUtils { private static SecurityContext securityContext = SecurityContextHolder.getContext(); /** + * Private constructor to avoid creating instances of util class. + */ + private PrincipalUtils(){ + } + + /** * Get the Full name. * * @return The user name |