Commit a519a063 authored by Kostis Trantzas's avatar Kostis Trantzas
Browse files

Merge branch 'generic_controller_enhancement' into 'develop'

Generic controller enhancement

See merge request !48
parents 14aacb62 19e4611b
Loading
Loading
Loading
Loading
Loading
+14 −14
Original line number Diff line number Diff line
@@ -46,17 +46,17 @@ public class ResourceActivationApiRouteBuilder extends RouteBuilder {

	private static final transient Log logger = LogFactory.getLog(ResourceActivationApiRouteBuilder.class.getName());

	@Value("${CATALOG_ADD_RESOURCE}")
	private String CATALOG_ADD_RESOURCE = "";
	@Value("${CATALOG_ADD_RESOURCEACTIVATION}")
	private String CATALOG_ADD_RESOURCEACTIVATION = "";

	@Value("${CATALOG_UPD_RESOURCE}")
	private String CATALOG_UPD_RESOURCE = "";
	@Value("${CATALOG_UPD_RESOURCEACTIVATION}")
	private String CATALOG_UPD_RESOURCEACTIVATION = "";
	
	@Value("${CATALOG_UPDADD_RESOURCE}")
	private String CATALOG_UPDADD_RESOURCE = "";
	@Value("${CATALOG_UPDADD_RESOURCEACTIVATION}")
	private String CATALOG_UPDADD_RESOURCEACTIVATION = "";

	@Value("${CATALOG_GET_RESOURCE_BY_ID}")
	private String CATALOG_GET_RESOURCE_BY_ID = "";	
	@Value("${CATALOG_GET_RESOURCEACTIVATION_BY_ID}")
	private String CATALOG_GET_RESOURCEACTIVATION_BY_ID = "";	

	@Autowired
	private ProducerTemplate template;
@@ -67,8 +67,8 @@ public class ResourceActivationApiRouteBuilder extends RouteBuilder {
	@Override
	public void configure() throws Exception {
		
		from( CATALOG_ADD_RESOURCE )
		.log(LoggingLevel.INFO, log, CATALOG_ADD_RESOURCE + " message received!")
		from( CATALOG_ADD_RESOURCEACTIVATION )
		.log(LoggingLevel.INFO, log, CATALOG_ADD_RESOURCEACTIVATION + " message received!")
		.to("log:DEBUG?showBody=true&showHeaders=true")
		.unmarshal().json( JsonLibrary.Jackson, ResourceCreate.class, true)
		.bean( resourceRepoService, "addResource(${body})")
@@ -76,16 +76,16 @@ public class ResourceActivationApiRouteBuilder extends RouteBuilder {
		.json( JsonLibrary.Jackson)
		.convertBodyTo( String.class );
				
		from( CATALOG_UPD_RESOURCE )
		.log(LoggingLevel.INFO, log, CATALOG_UPD_RESOURCE + " message received!")
		from( CATALOG_UPD_RESOURCEACTIVATION )
		.log(LoggingLevel.INFO, log, CATALOG_UPD_RESOURCEACTIVATION + " message received!")
		.to("log:DEBUG?showBody=true&showHeaders=true")
		.unmarshal().json( JsonLibrary.Jackson, ResourceUpdate.class, true)
		.bean( resourceRepoService, "updateResource(${header.resourceId}, ${body}, ${header.triggerServiceActionQueue} )")
		.marshal().json( JsonLibrary.Jackson)
		.convertBodyTo( String.class );
		
		from( CATALOG_UPDADD_RESOURCE )
		.log(LoggingLevel.INFO, log, CATALOG_UPDADD_RESOURCE + " message received!")
		from( CATALOG_UPDADD_RESOURCEACTIVATION )
		.log(LoggingLevel.INFO, log, CATALOG_UPDADD_RESOURCEACTIVATION + " message received!")
		.to("log:DEBUG?showBody=true&showHeaders=true")
		.unmarshal().json( JsonLibrary.Jackson, ResourceCreate.class, true)
		.bean( resourceRepoService, "addOrUpdateResourceByNameCategoryVersion(${header.aname},${header.acategory}, ${header.aversion}, ${body})")
+0 −112
Original line number Diff line number Diff line
/*-
 * ========================LICENSE_START=================================
 * org.etsi.osl.tmf.api
 * %%
 * Copyright (C) 2024 openslice.io
 * %%
 * 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.etsi.osl.tmf.ram702.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.etsi.osl.tmf.common.model.Notification;
import org.etsi.osl.tmf.ri639.model.ResourceAttributeValueChangeNotification;
import org.etsi.osl.tmf.ri639.model.ResourceCreateNotification;
import org.etsi.osl.tmf.ri639.model.ResourceStateChangeNotification;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
// @RefreshScope
@Component
public class ResourceActivationApiRouteBuilderEvents extends RouteBuilder {

  private static final transient Log logger =
      LogFactory.getLog(ResourceActivationApiRouteBuilderEvents.class.getName());



  @Value("${EVENT_RESOURCE_CREATE}")
  private String EVENT_RESOURCE_CREATE = "";

  @Value("${EVENT_RESOURCE_STATE_CHANGED}")
  private String EVENT_RESOURCE_STATE_CHANGED = "";

  @Value("${EVENT_RESOURCE_DELETE}")
  private String EVENT_RESOURCE_DELETE = "";

  @Value("${EVENT_RESOURCE_ATTRIBUTE_VALUE_CHANGED}")
  private String EVENT_RESOURCE_ATTRIBUTE_VALUE_CHANGED = "";


  @Autowired
  private ProducerTemplate template;


  @Override
  public void configure() throws Exception {

  }


  /**
   * @param n
   */
  public void publishEvent(final Notification n, final String objId) {
    n.setEventType(n.getClass().getName());
    logger.info("will send Event topic for type " + n.getEventType());
    try {
      String msgtopic = "";

      if (n instanceof ResourceCreateNotification) {
        msgtopic = EVENT_RESOURCE_CREATE;
      } else if (n instanceof ResourceAttributeValueChangeNotification) {
        msgtopic = EVENT_RESOURCE_ATTRIBUTE_VALUE_CHANGED;
      }else if (n instanceof ResourceStateChangeNotification ) {
        msgtopic = EVENT_RESOURCE_STATE_CHANGED;
      }

      Map<String, Object> map = new HashMap<>();
      map.put("eventid", n.getEventId());
      map.put("objId", objId);

      template.sendBodyAndHeaders(msgtopic, toJsonString(n), map);

    } catch (Exception e) {
      logger.error("Cannot send Event . " + e.getStackTrace());
    }
  }

  static String toJsonString(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper.writeValueAsString(object);
  }

}
+6 −1
Original line number Diff line number Diff line
@@ -1450,7 +1450,12 @@ public class ServiceSpecificationRepoService {
		serviceSpecCharacteristicItem.setValidFor( sourceChar.getValidFor() );
		for (ResourceSpecificationCharacteristicValue cv : sourceChar.getResourceSpecCharacteristicValue()) {
			ServiceSpecCharacteristicValue serviceSpecCharacteristicValueItem = new ServiceSpecCharacteristicValue();
			if ( cv.getValue()!=null && cv.getValue().getValue()!=null) {
	            serviceSpecCharacteristicValueItem.setValue( new Any( cv.getValue().getValue(), cv.getValue().getAlias()));			 
			} else {
			  serviceSpecCharacteristicValueItem.setValue( new Any( "", ""));
			}
			
			serviceSpecCharacteristicValueItem.isDefault( cv.isIsDefault() );
			serviceSpecCharacteristicValueItem.setUnitOfMeasure( cv.getUnitOfMeasure() );		
			serviceSpecCharacteristicItem.addServiceSpecCharacteristicValueItem(serviceSpecCharacteristicValueItem );
+71 −58
Original line number Diff line number Diff line
@@ -578,11 +578,19 @@ public class ServiceRepoService {
        }
		
		if (serviceCharacteristicChanged) {
          
          Characteristic noteCheck = service.getServiceCharacteristicByName("_DETAILED_NOTES_");
          if ( noteCheck!= null 
              && noteCheck.getValue() != null
              && noteCheck.getValue().getValue() != null
              && !noteCheck.getValue().getValue().equals("")) {
            Note noteItem = new Note();
            noteItem.setText("Service Characteristic changed: " + charChangedForNotes );
			noteItem.setAuthor("API");
            noteItem.setAuthor("SIM638-API");
            noteItem.setDate(OffsetDateTime.now(ZoneOffset.UTC) );
            service.addNoteItem(noteItem);  
            
          }	
		}
		
			
@@ -639,6 +647,11 @@ public class ServiceRepoService {
			this.addServiceActionQueueItem(saqi);
		}
		
		
		
		
		
		
        /*
         * Update any parent service
         */
@@ -946,6 +959,8 @@ public class ServiceRepoService {
	public ServiceActionQueueItem  addServiceActionQueueItem(@Valid ServiceActionQueueItem item) {
		logger.debug("Will add ServiceActionQueueItem ServiceRefId: " + item.getServiceRefId() );
		
		
		
		//find any similar action inqueue and delete them, so to keep this one as the most recent
		List<ServiceActionQueueItem> result = this.serviceActionQueueRepo.findByServiceRefIdAndAction(item.getServiceRefId(), item.getAction());
        logger.debug("Will add ServiceActionQueueItem ServiceRefId result: " +result.size() );
@@ -1114,20 +1129,37 @@ public class ServiceRepoService {
	public void  updateServicesHavingThisSupportingResource(@Valid Resource res) {
      try {
        
        logger.info("Will update services related to this resource with id = " + res.getId() );
        logger.debug("Will update services related to this resource with id = " + res.getId() );
        
        var aservices = findServicesHavingThisSupportingResourceID(  res.getId() );

        logger.debug("services.found = " + aservices.size() );
        
        for (Service as : aservices) {
            
              Service aService = findByUuid(as.getId()); 
              
            //if ( aService.getState().equals( ServiceStateType.ACTIVE )  ) {
              List<Resource> rlist = new ArrayList<Resource>();
              for (ResourceRef rref : aService.getSupportingResource()) {
                Optional<Resource> result = resourceRepo.findByUuid(rref.getId());
                if (result.isPresent()) {
                  rlist.add( result.get() );
                }
              }
  
              rlist.add(res); //add also this one
              
              ServiceStateType nextState = aService.findNextStateBasedOnSupportingResources(rlist);
  
                ServiceUpdate supd = new ServiceUpdate();
                supd.setState(nextState);
                String stateText="";
                if ( !aService.getState().equals(nextState)) {
                  stateText = "State changed from " + aService.getState() + " to " + nextState + ".";
                }
                
                //copy characteristics from resource to service
                
                //copy characteristics, from resource to service
                
                for (org.etsi.osl.tmf.ri639.model.Characteristic rChar : res.getResourceCharacteristic()) {
                    Characteristic cNew = new Characteristic();
@@ -1136,15 +1168,20 @@ public class ServiceRepoService {
                    supd.addServiceCharacteristicItem( cNew );  
                }
                
                
                Characteristic noteCheck = as.getServiceCharacteristicByName("_DETAILED_NOTES_");
                if ( noteCheck!= null 
                    && noteCheck.getValue() != null
                    && noteCheck.getValue().getValue() != null
                    && !noteCheck.getValue().getValue().equals("")) {
                  Note n = new Note();
                n.setText("Supporting Resource Attribute Changed with id: " + res.getId());
                  n.setText(stateText + "Supporting Resource changed with id: " + res.getId());
                  n.setAuthor( "SIM638-API" );
                  n.setDate( OffsetDateTime.now(ZoneOffset.UTC).toString() );
                  supd.addNoteItem( n );                  
                }                  
                
                this.updateService( aService.getId(), supd , true, null, null); //update the service 
            //}

        }
      

@@ -1185,47 +1222,23 @@ public class ServiceRepoService {
    @Transactional  
    private void updateServiceFromresourceChange(Resource res) {

      logger.info("Will update services related to this resource with id = " + res.getId() );
      var aservices = findServicesHavingThisSupportingResourceID(res.getId());
      updateServicesHavingThisSupportingResource(res);

      for (Service as : aservices) {

        Service aService = findByUuid(as.getId());


        List<Resource> rlist = new ArrayList<Resource>();
        for (ResourceRef rref : aService.getSupportingResource()) {
          Optional<Resource> result = resourceRepo.findByUuid(rref.getId());
          if (result.isPresent()) {
            rlist.add( result.get() );
          }
        }

        rlist.add(res); //add also this one
        
        ServiceStateType nextState = aService.findNextStateBasedOnSupportingResources(rlist);

        ServiceUpdate supd = new ServiceUpdate();
        supd.setState(nextState);
        Note n = new Note();
        n.setText("Supporting Resource " + res.getId() + " State Changed with status: "
            + res.getResourceStatus() + ".Next state is " + nextState);
        n.setAuthor("SIM638-API");
        n.setDate(OffsetDateTime.now(ZoneOffset.UTC).toString());
        supd.addNoteItem(n);

        this.updateService(aService.getId(), supd, true, null, null); // update the service
      }


      updateResourceFromKubernetesLabel(res);
      addAnyNewRelatedResourcesFromKubernetesLabel(res);


    }

    /**
     * This function will try to identify if the resource contains
     * a characteristic called "org.etsi.osl.serviceId" and will check if there is a related service.
     * If it is not it's add the resource back to the service. This is useful in kubernetes deployments,
     * in cases of new resources in a namespace that are related to this service
     * @param res
     */
    @Transactional  
    private void updateResourceFromKubernetesLabel(Resource res) {
      logger.debug("updateResourceFromKubernetesLabel for: " + res.getName()); 
    private void addAnyNewRelatedResourcesFromKubernetesLabel(Resource res) {
      logger.debug("updateResourceFromKubernetesLabel for: " + res.getName() + ", version" + res.getResourceVersion()); 
      
      if (res.getResourceCharacteristicByName("org.etsi.osl.serviceId") != null) {

+7 −0
Original line number Diff line number Diff line
@@ -175,6 +175,13 @@ EVENT_RESOURCE_DELETE: "jms:topic:EVENT.SERVICE.RESOURCE"
EVENT_RESOURCE_ATTRIBUTE_VALUE_CHANGED: "jms:topic:EVENT.RESOURCE.ATTRCHANGED"
CATALOG_RESOURCES_OF_PARTNERS: "jms:queue:CATALOG.GET.SERVICESOFPARTNERS"

#RESOURCE_ACTIVATION
CATALOG_ADD_RESOURCEACTIVATION: "jms:queue:CATALOG.ADD.RESOURCEACTIVATION"
CATALOG_UPD_RESOURCEACTIVATION: "jms:queue:CATALOG.UPD.RESOURCEACTIVATION"
CATALOG_UPDADD_RESOURCEACTIVATION: "jms:queue:CATALOG.UPDADD.RESOURCEACTIVATION"
CATALOG_GET_RESOURCEACTIVATION_BY_ID: "jms:queue:CATALOG.GET.RESOURCEACTIVATION"


#LCM MESSAGES
CATALOG_GET_LCMRULE_BY_ID: "jms:queue:CATALOG.GET.LCMRULE"
CATALOG_GET_LCMRULES_BY_SPECID_PHASE: "jms:queue:CATALOG.GET.LCMRULES_BY_SPECID_PHASE"
Loading