diff options
Diffstat (limited to 'src/main/java/org/onap')
11 files changed, 853 insertions, 0 deletions
diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/Application.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/Application.java new file mode 100644 index 0000000..9986266 --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/Application.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * 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.pomba.contextbuilder.networkdiscovery; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.scheduling.annotation.EnableAsync; + + +@SpringBootApplication +@ComponentScan(basePackages = {"org.onap.pomba.contextbuilder.networkdiscovery"}) +@EnableAsync +@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) +public class Application extends SpringBootServletInitializer { + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(Application.class); + } + + public static void main(String[] args) throws Exception { + SpringApplication.run(Application.class, args); + } + +} diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/JerseyConfiguration.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/JerseyConfiguration.java new file mode 100644 index 0000000..df7c8a8 --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/JerseyConfiguration.java @@ -0,0 +1,69 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * 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.pomba.contextbuilder.networkdiscovery; + +import java.util.logging.Logger; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.logging.LoggingFeature; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.servlet.ServletProperties; +import org.onap.pomba.contextbuilder.networkdiscovery.service.rs.RestServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +@Component +@ApplicationPath("/") +public class JerseyConfiguration extends ResourceConfig { + private static final Logger log = Logger.getLogger(JerseyConfiguration.class.getName()); + + @Autowired + public JerseyConfiguration() { + register(RestServiceImpl.class); + property(ServletProperties.FILTER_FORWARD_ON_404, true); + register(new LoggingFeature(log)); + } + + @Bean + @Primary + public ObjectMapper objectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES); + objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); + return objectMapper; + } + + @Bean + public Client jerseyClient() { + return ClientBuilder.newClient(new ClientConfig()); + } +} diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/NdctxbConfiguration.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/NdctxbConfiguration.java new file mode 100644 index 0000000..d103411 --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/NdctxbConfiguration.java @@ -0,0 +1,79 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * 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.pomba.contextbuilder.networkdiscovery; + +import java.util.Base64; +import org.eclipse.jetty.util.security.Password; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +@Component +public class NdctxbConfiguration { + + // Network Discover Configuration values + + @Value("${networkDiscoveryCtxBuilder.userId:admin}") + private String networkDiscoveryCtxBuilderUserId; + + @Value("${networkDiscoveryCtxBuilder.password:admin}") + private String networkDiscoveryCtxBuilderPassword; + + // Service Decomposition Configuration values + + @Value("${serviceDecomposition.host:127.0.0.1}") + private String serviceDecompositionHost; + + @Value("${serviceDecomposition.port}") + private String serviceDecompositionPort; + + @Value("${serviceDecomposition.httpProtocol}") + private String serviceDecompositionHttpProtocol; + + @Value("${serviceDecomposition.serviceInstancePath}") + private String serviceDecompositionServiceInstancePath; + + @Value("${serviceDecomposition.userId:admin}") + private String serviceDecompositionUserId; + + @Value("${serviceDecomposition.password:admin}") + private String serviceDecompositionPassword; + + @Bean(name="serviceDecompositionBaseUrl") + public String getURL() { + String url = this.serviceDecompositionHttpProtocol + "://" + + this.serviceDecompositionHost + ":" + + this.serviceDecompositionPort + + this.serviceDecompositionServiceInstancePath; + return url; + } + + @Bean(name="serviceDecompositionBasicAuthorization") + public String getSdBasicAuth() { + String auth = new String(this.serviceDecompositionUserId + ":" + Password.deobfuscate(this.serviceDecompositionPassword)); + return ("Basic " + Base64.getEncoder().encodeToString(auth.getBytes())); + } + + @Bean(name="networkDiscoveryCtxBuilderBasicAuthorization") + public String getNdBasicAuth() { + String auth = this.networkDiscoveryCtxBuilderUserId + ":" + Password.deobfuscate(this.networkDiscoveryCtxBuilderPassword); + return ("Basic " + Base64.getEncoder().encodeToString(auth.getBytes())); + } + +} diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/WebConfiguration.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/WebConfiguration.java new file mode 100644 index 0000000..ed779b8 --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/WebConfiguration.java @@ -0,0 +1,40 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * 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.pomba.contextbuilder.networkdiscovery; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@Configuration +public class WebConfiguration { + + @Bean + public WebMvcConfigurerAdapter forwardToIndex() { + return new WebMvcConfigurerAdapter() { + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/swagger").setViewName("redirect:/swagger/index.html"); + registry.addViewController("/swagger/").setViewName("redirect:/swagger/index.html"); + registry.addViewController("/docs").setViewName("redirect:/docs/html/index.html"); + registry.addViewController("/docs/").setViewName("redirect:/docs/html/index.html"); + } + }; + } +} diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/exception/DiscoveryException.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/exception/DiscoveryException.java new file mode 100644 index 0000000..3f46324 --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/exception/DiscoveryException.java @@ -0,0 +1,49 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * 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.pomba.contextbuilder.networkdiscovery.exception; + +import javax.ws.rs.core.Response.Status; + +public class DiscoveryException extends Exception { + + private static final long serialVersionUID = -4874149714911165454L; + + private final Status httpStatus; + + public DiscoveryException(String message) { + this(message, Status.INTERNAL_SERVER_ERROR); + } + + public DiscoveryException(String message, Status httpStatus) { + super(message); + if (httpStatus == null) { + throw new NullPointerException("httpStatus"); + } + this.httpStatus = httpStatus; + } + + public DiscoveryException(String message, Exception cause) { + super(message, cause); + this.httpStatus = Status.INTERNAL_SERVER_ERROR; + } + + public Status getHttpStatus() { + return this.httpStatus; + } + +} diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/exception/ErrorMessage.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/exception/ErrorMessage.java new file mode 100644 index 0000000..df77fd5 --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/exception/ErrorMessage.java @@ -0,0 +1,24 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * 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.pomba.contextbuilder.networkdiscovery.exception; + +public class ErrorMessage { + public static String CONTEXT_BUILDER_FAILED = "Context Builder Failed. "; + public static String INVALID_REQUEST_URL = "Invalid Request URL. "; + public static String MISSING_PARAMTER = "Missing Parameter: "; +} diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/SpringService.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/SpringService.java new file mode 100644 index 0000000..b103544 --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/SpringService.java @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * 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.pomba.contextbuilder.networkdiscovery.service; + +import javax.servlet.http.HttpServletRequest; + +import org.onap.pomba.common.datatypes.ModelContext; +import org.onap.pomba.contextbuilder.networkdiscovery.exception.DiscoveryException; + +public interface SpringService { + + public ModelContext getContext(HttpServletRequest req, + String partnerName, + String authorization, + String transactionId, + String serviceInstanceId, + String modelVersionId, + String modelInvariantId) throws DiscoveryException; + + public void validateBasicAuth(String authorization) throws DiscoveryException; +} diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/SpringServiceImpl.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/SpringServiceImpl.java new file mode 100644 index 0000000..0a7fc5d --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/SpringServiceImpl.java @@ -0,0 +1,309 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * 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.pomba.contextbuilder.networkdiscovery.service; + +import java.net.InetAddress; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.UUID; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.client.Client; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import org.json.JSONArray; +import org.json.JSONObject; +import org.onap.pomba.common.datatypes.ModelContext; +import org.onap.pomba.common.datatypes.Service; +import org.onap.pomba.common.datatypes.VF; +import org.onap.pomba.common.datatypes.VFModule; +import org.onap.pomba.common.datatypes.VNFC; +import org.onap.pomba.contextbuilder.networkdiscovery.exception.DiscoveryException; +import org.onap.pomba.contextbuilder.networkdiscovery.service.rs.RestService; +import org.onap.pomba.contextbuilder.networkdiscovery.util.RestUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.springframework.beans.factory.annotation.Autowired; + + +@org.springframework.stereotype.Service +public class SpringServiceImpl implements SpringService { + private static Logger log = LoggerFactory.getLogger(RestService.class); + public static String APP_NAME = "NetworkDiscoveryContextBuilder"; + + public static String MDC_REQUEST_ID = "RequestId"; + public static String MDC_SERVER_FQDN = "ServerFQDN"; + public static String MDC_SERVICE_NAME = "ServiceName"; + public static String MDC_PARTNER_NAME = "PartnerName"; + public static String MDC_START_TIME = "StartTime"; + public static String MDC_SERVICE_INSTANCE_ID = "ServiceInstanceId"; + public static String MDC_INVOCATION_ID = "InvocationID"; + public static String MDC_CLIENT_ADDRESS = "ClientAddress"; + public static String MDC_STATUS_CODE = "StatusCode"; + public static String MDC_RESPONSE_CODE = "ResponseCode"; + public static String MDC_INSTANCE_UUID = "InstanceUUID"; + + private static final String ENTITY_GENERIC_VNFS = "generic-vnfs"; + private static final String ENTITY_L3_NETWORK = "l3-network"; + private static final String ENTITY_MODEL_INVARIANT_ID = "model-invariant-id"; + private static final String ENTITY_NETWORK_ID = "network-id"; + private static final String ENTITY_NETWORK_NAME = "network-name"; + private static final String ENTITY_SERVICE_INSTANCE_ID = "service-instance-id"; + private static final String ENTITY_SERVICE_INSTANCE_NAME = "service-instance-name"; + private static final String ENTITY_VF_MODULE = "vf-module"; + private static final String ENTITY_VF_MODULES = "vf-modules"; + private static final String ENTITY_VF_MODULE_ID = "vf-module-id"; + private static final String ENTITY_VNF_ID = "vnf-id"; + private static final String ENTITY_VNF_NAME = "vnf-name"; + private static final String ENTITY_VNF_TYPE = "vnf-type"; + private static final String ENTITY_VSERVER = "vserver"; + private static final String ENTITY_VSERVER_NAME = "vserver-name"; + private static final String ENTITY_VSERVER_ID = "vserver-id"; + + private static UUID instanceUUID = UUID.randomUUID(); + + @Autowired + private String serviceDecompositionBaseUrl; + + @Autowired + private String networkDiscoveryCtxBuilderBasicAuthorization; + + @Autowired + private String serviceDecompositionBasicAuthorization; + + @Autowired + private Client jerseyClient; + + @Override + public ModelContext getContext(HttpServletRequest req, + String partnerName, + String authorization, + String requestId, + String serviceInstanceId, + String modelVersionId, + String modelInvariantId) throws DiscoveryException { + + String remoteAddress = req.getRemoteAddr() != null ? req.getRemoteAddr() : null; + initMDC(requestId, partnerName, serviceInstanceId, remoteAddress); + + try { + RestUtil.validateServiceInstanceId(serviceInstanceId); + RestUtil.validatePartnerName(partnerName); + validateBasicAuth(authorization); + return getServiceDeomposition(serviceInstanceId, partnerName, requestId); + } catch (Exception x) { + MDC.put(MDC_RESPONSE_CODE, String.valueOf(Status.INTERNAL_SERVER_ERROR.getStatusCode())); + MDC.put(MDC_STATUS_CODE, "ERROR"); + log.error(x.getMessage()); + throw new DiscoveryException(x.getMessage(), x); + } finally { + MDC.clear(); + } + } + + + private void initMDC(String requestId, String partnerName, String serviceInstanceId, String remoteAddress) { + MDC.clear(); + MDC.put(MDC_REQUEST_ID, requestId); + MDC.put(MDC_SERVICE_NAME, APP_NAME); + MDC.put(MDC_SERVICE_INSTANCE_ID, serviceInstanceId); + MDC.put(MDC_PARTNER_NAME, partnerName); + MDC.put(MDC_CLIENT_ADDRESS, remoteAddress); + MDC.put(MDC_START_TIME, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(new Date())); + MDC.put(MDC_INVOCATION_ID, UUID.randomUUID().toString()); + MDC.put(MDC_INSTANCE_UUID, instanceUUID.toString()); + + try { + MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getCanonicalHostName()); + } catch (Exception e) { + // If, for some reason we are unable to get the canonical host name, we + // just want to leave the field null. + } + } + + /** + * Given a service instance ID, GET the resources from Service Decompostion. + */ + private ModelContext getServiceDeomposition(String serviceInstanceId, String partnerName, String requestId) throws DiscoveryException { + if (serviceInstanceId == null) { + return null; + } + + log.info("Querying Service Decomposition for service instance " + serviceInstanceId); + + String urlStr = getUrl(serviceInstanceId); + + try { + Response response = + jerseyClient.target(urlStr) + .request() + .header("Accept", "application/json") + .header("Authorization", getSdBasicAuthorization()) + .header("X-ONAP-PartnerName", partnerName) + .header("X-ONAP-RequestID", requestId) + .get(); + + String reply = null; + JSONObject jObject = null; + if (response.getStatus() != 200) { + MDC.put(MDC_RESPONSE_CODE, String.valueOf(response.getStatus())); + MDC.put(MDC_STATUS_CODE, "ERROR"); + throw new DiscoveryException(response.getStatusInfo().toString(), Response.Status.fromStatusCode(response.getStatus())); + } + else { + MDC.put(MDC_RESPONSE_CODE, String.valueOf(response.getStatus())); + MDC.put(MDC_STATUS_CODE, "COMPLETE"); + reply = response.readEntity(String.class); + log.info("Response from GET " + urlStr + " " + reply); + jObject = new JSONObject(reply); + } + return parseServiceDecomposition(jObject); + } catch (Exception x) { + throw new DiscoveryException(x.getMessage(), x); + } + } + + private String getUrl (String serviceInstanceId) throws DiscoveryException { + return new String(serviceDecompositionBaseUrl + "?serviceInstanceId=" + serviceInstanceId); + } + + private String getSdBasicAuthorization() { + return serviceDecompositionBasicAuthorization; + } + + private ModelContext parseServiceDecomposition(JSONObject jObject) { + + ModelContext response = new ModelContext(); + // Get Service Instance Data + Service service = new Service(); + + if (jObject.has(ENTITY_SERVICE_INSTANCE_NAME)) { + service.setName(jObject.getString(ENTITY_SERVICE_INSTANCE_NAME)); + } + + if (jObject.has(ENTITY_SERVICE_INSTANCE_ID)) { + service.setUuid(jObject.getString(ENTITY_SERVICE_INSTANCE_ID)); + } + + if (jObject.has(ENTITY_MODEL_INVARIANT_ID)) { + service.setInvariantUuid(jObject.getString(ENTITY_MODEL_INVARIANT_ID)); + } + + response.setService(service); + + // Find generic-vnfs + if (jObject.has(ENTITY_GENERIC_VNFS)) { + JSONArray genericVnfs = jObject.getJSONArray(ENTITY_GENERIC_VNFS); + for (int i = 0; i < genericVnfs.length(); i++ ) { + VF vf = new VF(); + JSONObject genericVnfInst = genericVnfs.getJSONObject(i); + + if (genericVnfInst.has(ENTITY_VNF_NAME)) { + vf.setName(genericVnfInst.getString(ENTITY_VNF_NAME)); + } + if (genericVnfInst.has(ENTITY_VNF_TYPE)) { + vf.setType(genericVnfInst.getString(ENTITY_VNF_TYPE)); + } + if (genericVnfInst.has(ENTITY_VNF_ID)) { + vf.setUuid(genericVnfInst.getString(ENTITY_VNF_ID)); + } + + if (genericVnfInst.has(ENTITY_MODEL_INVARIANT_ID)) { + vf.setInvariantUuid(genericVnfInst.getString(ENTITY_MODEL_INVARIANT_ID)); + } + + // find vf-modules + if (genericVnfInst.has(ENTITY_VF_MODULES)) { + JSONObject vfModules = genericVnfInst.getJSONObject(ENTITY_VF_MODULES); + if (vfModules.has(ENTITY_VF_MODULE)) { + JSONArray vfModuleList = vfModules.getJSONArray(ENTITY_VF_MODULE); + for (int j = 0; j < vfModuleList.length(); j++) { + VFModule vfModule = new VFModule(); + JSONObject vfModuleInst = vfModuleList.getJSONObject(j); + if (vfModuleInst.has(ENTITY_VF_MODULE_ID)) { + vfModule.setUuid(vfModuleInst.getString(ENTITY_VF_MODULE_ID)); + } + if (vfModuleInst.has(ENTITY_MODEL_INVARIANT_ID)) { + vfModule.setInvariantUuid(vfModuleInst.getString(ENTITY_MODEL_INVARIANT_ID)); + } + vf.addVfModule(vfModule); + } + } + } + + // Find vservers + if (genericVnfInst.has(ENTITY_VSERVER)) { + JSONArray vservers = genericVnfInst.getJSONArray(ENTITY_VSERVER); + for (int j = 0; j < vservers.length(); j++) { + VNFC vserver = new VNFC(); + JSONObject vserversInst = vservers.getJSONObject(j); + if (vserversInst.has(ENTITY_VSERVER_NAME)) { + vserver.setName(vserversInst.getString(ENTITY_VSERVER_NAME)); + } + if (vserversInst.has(ENTITY_VSERVER_ID)) { + vserver.setUuid(vserversInst.getString(ENTITY_VSERVER_ID)); + } + if (vserversInst.has(ENTITY_MODEL_INVARIANT_ID)) { + vserver.setInvariantUuid(vserversInst.getString(ENTITY_MODEL_INVARIANT_ID)); + } + vf.addVnfc(vserver); + } + } + + // Find l3 networks + if (genericVnfInst.has(ENTITY_L3_NETWORK)) { + JSONArray l3Networks = genericVnfInst.getJSONArray(ENTITY_L3_NETWORK); + for (int j = 0; j < l3Networks.length(); j++) { + VNFC l3Network = new VNFC(); + JSONObject l3NetworkInst = l3Networks.getJSONObject(j); + if (l3NetworkInst.has(ENTITY_NETWORK_NAME)) { + l3Network.setName(l3NetworkInst.getString(ENTITY_NETWORK_NAME)); + } + if (l3NetworkInst.has(ENTITY_NETWORK_ID)) { + l3Network.setUuid(l3NetworkInst.getString(ENTITY_NETWORK_ID)); + } + if (l3NetworkInst.has(ENTITY_MODEL_INVARIANT_ID)) { + l3Network.setInvariantUuid(l3NetworkInst.getString(ENTITY_MODEL_INVARIANT_ID)); + } + vf.addVnfc(l3Network); + } + } + response.addVf(vf); + } + } + + return response; + } + + /** + * Validates the Basic authorization header as admin:admin. + * + * @throws DiscoveryException if there is missing parameter + */ + public void validateBasicAuth(String authorization) throws DiscoveryException { + if (authorization != null && !authorization.trim().isEmpty() && authorization.startsWith("Basic")) { + if (!authorization.equals(networkDiscoveryCtxBuilderBasicAuthorization)) { + throw new DiscoveryException("Authorization Failed", Status.UNAUTHORIZED); + }; + } else { + throw new DiscoveryException("Missing Authorization: " +(authorization==null ? "null" : authorization.toString()), Status.UNAUTHORIZED); + } + } + + +}
\ No newline at end of file diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestService.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestService.java new file mode 100644 index 0000000..7377c18 --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestService.java @@ -0,0 +1,64 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * 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.pomba.contextbuilder.networkdiscovery.service.rs; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.onap.pomba.contextbuilder.networkdiscovery.exception.DiscoveryException; + + +@Api +@Path("/service") +@Produces(MediaType.APPLICATION_JSON) +public interface RestService { + + @GET + @Path("/context") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + value = "Fetch network info for service", + notes = "Returns a JSON object which represents the Context model data", + response = Response.class + ) + @ApiResponses( + value = { + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 400, message = "Bad Request"), + @ApiResponse(code = 404, message = "Service not available"), + @ApiResponse(code = 500, message = "Unexpected Runtime error") + }) + public Response getContext( + @Context HttpServletRequest req, + @HeaderParam("Authorization") String authorization, + @HeaderParam("X-ONAP-PartnerName") String xpartnerName, + @HeaderParam("X-ONAP-RequestID") String xRequestId, + @QueryParam("serviceInstanceId") String serviceInstanceId, + @QueryParam("modelVersionId") String modelVersionId, + @QueryParam("modelInvariantId") String modelInvariantId) throws DiscoveryException; +}
\ No newline at end of file diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestServiceImpl.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestServiceImpl.java new file mode 100644 index 0000000..6c79545 --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestServiceImpl.java @@ -0,0 +1,84 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * 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.pomba.contextbuilder.networkdiscovery.service.rs; + +import java.util.UUID; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import org.onap.pomba.common.datatypes.ModelContext; +import org.onap.pomba.contextbuilder.networkdiscovery.exception.DiscoveryException; +import org.onap.pomba.contextbuilder.networkdiscovery.exception.ErrorMessage; + +import org.onap.pomba.contextbuilder.networkdiscovery.service.SpringService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +@Component +public class RestServiceImpl implements RestService { + private static Logger log = LoggerFactory.getLogger(RestService.class); + private static final String EMPTY_JSON_OBJECT = "{}"; + private static final String HEADER_REQUEST_ID = "X-ONAP-RequestID"; + + @Autowired + private SpringService service; + + @Override + public Response getContext(HttpServletRequest req, + String authorization, + String partnerName, + String requestId, + String serviceInstanceId, + String modelVersionId, + String modelInvariantId) throws DiscoveryException { + + // Do some validation on Http headers and URL parameters + + if (requestId == null || requestId.isEmpty()) { + requestId = UUID.randomUUID().toString(); + log.debug(HEADER_REQUEST_ID + " is missing; using newly generated value: " + requestId); + } + + try { + ModelContext sdContext = service.getContext(req, partnerName, authorization, requestId, serviceInstanceId, modelVersionId, modelInvariantId); + if (sdContext == null) { + // Return empty JSON + return Response.ok().entity(EMPTY_JSON_OBJECT).build(); + } else { + Gson gson = new GsonBuilder().create(); + return Response.ok().entity(gson.toJson(sdContext)).build(); + } + } catch (DiscoveryException x) { + log.error(ErrorMessage.CONTEXT_BUILDER_FAILED); + return Response.status(x.getHttpStatus()).entity(x.getMessage()).build(); + + } catch (Exception x) { + log.error(ErrorMessage.CONTEXT_BUILDER_FAILED, x); + return Response.status(Status.INTERNAL_SERVER_ERROR).entity(x.getMessage()).build(); + } + } + + +} diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/util/RestUtil.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/util/RestUtil.java new file mode 100644 index 0000000..a8f78fc --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/util/RestUtil.java @@ -0,0 +1,54 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * 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.pomba.contextbuilder.networkdiscovery.util; + +import javax.ws.rs.core.Response.Status; + +import org.onap.pomba.contextbuilder.networkdiscovery.exception.DiscoveryException; +import org.onap.pomba.contextbuilder.networkdiscovery.exception.ErrorMessage; + +public class RestUtil { + // Parameters for Query AAI Model Data API + private static final String SERVICE_INSTANCE_ID = "serviceInstanceId"; + private static final String HEADER_PARTNER_NAME = "X-ONAP-PartnerName"; + + /** + * Validates the URL parameter seriveInstanceId. + * + * @throws DiscoveryException if there is missing parameter + */ + public static void validateServiceInstanceId(String serviceInstanceId) throws DiscoveryException { + + if (serviceInstanceId == null || serviceInstanceId.trim().isEmpty()) + throw new DiscoveryException(ErrorMessage.INVALID_REQUEST_URL + ErrorMessage.MISSING_PARAMTER + SERVICE_INSTANCE_ID, Status.BAD_REQUEST); + } + + /** + * Validates the URL parameter X-ONAP-PartnerName. + * + * @throws DiscoveryException if there is missing parameter + */ + public static void validatePartnerName(String partnerName) throws DiscoveryException { + + if ((partnerName == null) || partnerName.trim().isEmpty()) { + throw new DiscoveryException(ErrorMessage.MISSING_PARAMTER + HEADER_PARTNER_NAME, Status.BAD_REQUEST); + } + } + + +} |