| 1 | package de.uka.ipd.sdq.pcm.transformations.builder.connectors; |
| 2 | |
| 3 | import org.apache.log4j.Logger; |
| 4 | |
| 5 | import de.uka.ipd.sdq.featureconfig.FeatureConfig; |
| 6 | import de.uka.ipd.sdq.pcm.allocation.AllocationContext; |
| 7 | import de.uka.ipd.sdq.pcm.core.composition.AssemblyConnector; |
| 8 | import de.uka.ipd.sdq.pcm.core.composition.AssemblyContext; |
| 9 | import de.uka.ipd.sdq.pcm.core.composition.ComposedStructure; |
| 10 | import de.uka.ipd.sdq.pcm.core.composition.CompositionFactory; |
| 11 | import de.uka.ipd.sdq.pcm.repository.InfrastructureProvidedRole; |
| 12 | import de.uka.ipd.sdq.pcm.repository.Interface; |
| 13 | import de.uka.ipd.sdq.pcm.repository.OperationProvidedRole; |
| 14 | import de.uka.ipd.sdq.pcm.repository.OperationRequiredRole; |
| 15 | import de.uka.ipd.sdq.pcm.repository.ProvidedRole; |
| 16 | import de.uka.ipd.sdq.pcm.resourceenvironment.LinkingResource; |
| 17 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceContainer; |
| 18 | import de.uka.ipd.sdq.pcm.transformations.FeatureUtils; |
| 19 | import de.uka.ipd.sdq.pcm.transformations.builder.IBuilder; |
| 20 | import de.uka.ipd.sdq.pcm.transformations.builder.IComponentBuilder; |
| 21 | import de.uka.ipd.sdq.pcm.transformations.builder.resourceconsumer.LocalCommunicationComponentBuilder; |
| 22 | import de.uka.ipd.sdq.pcm.transformations.builder.resourceconsumer.NetworkLoadingComponentBuilder; |
| 23 | import de.uka.ipd.sdq.pcm.transformations.builder.util.PCMAndCompletionModelHolder; |
| 24 | |
| 25 | /** |
| 26 | * This builder replaces a given connector with a component built by the given component builder. |
| 27 | * @author Snowball |
| 28 | * |
| 29 | */ |
| 30 | public class ConnectorReplacingBuilder implements IBuilder { |
| 31 | |
| 32 | private Logger logger = Logger.getLogger(ConnectorReplacingBuilder.class); |
| 33 | |
| 34 | private AssemblyConnector connector; |
| 35 | private ComposedStructure parent; |
| 36 | private LinkingResource linkingRes; |
| 37 | private PCMAndCompletionModelHolder models; |
| 38 | private FeatureConfig featureConfig; |
| 39 | |
| 40 | public ConnectorReplacingBuilder(PCMAndCompletionModelHolder models, AssemblyConnector con, FeatureConfig featureConfig) { |
| 41 | this.models = models; |
| 42 | this.connector = con; |
| 43 | this.linkingRes = findLinkingResource(connector); |
| 44 | this.parent = con.getParentStructure__Connector(); |
| 45 | this.featureConfig = featureConfig; |
| 46 | } |
| 47 | |
| 48 | public void build() { |
| 49 | // Teste hier: Entweder sind es 2 Prozesse auf dem selben Rechner _oder_ zwei Prozesse auf verschiedenen Rechner. |
| 50 | // Im letzten Fall ist die Konfigurationsoption "SameAddressSpace" illegal und wird somit �bergangen... |
| 51 | if (FeatureUtils.hasFeature(featureConfig,"DifferentAddressSpace") || linkingRes != null) { |
| 52 | logger.info("Expanding a completion for remote connector "+connector.getEntityName()); |
| 53 | |
| 54 | IClientServerConnectorCompletionComponentBuilder componentBuilder = |
| 55 | configureCompletionComponentBuilder(); |
| 56 | |
| 57 | // First build the completion component |
| 58 | componentBuilder.build(); |
| 59 | |
| 60 | // Then integrate the component in the structure instead of the AssemblyConnector |
| 61 | parent.getAssemblyContexts__ComposedStructure().add(componentBuilder.getAssemblyContext()); |
| 62 | |
| 63 | embeddConnectorCompletionInApplication(componentBuilder); |
| 64 | connectConnectorCompletionWithMiddleware(componentBuilder); |
| 65 | |
| 66 | // Finally, remove the connector from the architecture |
| 67 | parent.getConnectors__ComposedStructure().remove(connector); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private IClientServerConnectorCompletionComponentBuilder configureCompletionComponentBuilder() { |
| 72 | IComponentBuilder builder = null; |
| 73 | IClientServerConnectorCompletionComponentBuilder result; |
| 74 | ResourceContainer clientContainer = null; |
| 75 | ResourceContainer serverContainer = null; |
| 76 | |
| 77 | if (linkingRes != null) { |
| 78 | //TODO |
| 79 | //Hauck: Changed code here because of changed metamodel. Please check. |
| 80 | if ((linkingRes.getConnectedResourceContainers_LinkingResource() != null) && (linkingRes.getConnectedResourceContainers_LinkingResource().size()>1)) { |
| 81 | clientContainer = linkingRes.getConnectedResourceContainers_LinkingResource().get(0); |
| 82 | serverContainer = linkingRes.getConnectedResourceContainers_LinkingResource().get(1); |
| 83 | //clientContainer = linkingRes.getFromResourceContainer_LinkingResource().get(0); |
| 84 | //serverContainer = linkingRes.getToResourceContainer_LinkingResource().get(0); |
| 85 | builder = new NetworkLoadingComponentBuilder(models, connector.getRequiredRole_AssemblyConnector().getRequiredInterface__OperationRequiredRole(),linkingRes); |
| 86 | } |
| 87 | } else { |
| 88 | clientContainer = findContainer(this.connector.getRequiringAssemblyContext_AssemblyConnector()); |
| 89 | serverContainer = clientContainer; |
| 90 | builder = new LocalCommunicationComponentBuilder(models, connector.getRequiredRole_AssemblyConnector().getRequiredInterface__OperationRequiredRole()); |
| 91 | } |
| 92 | /*************************************************************** |
| 93 | Lucka: Commented to avoid encryption and authentication effects on a connector per default --> will be moved in for this purpose designed completion |
| 94 | **************************************************************** |
| 95 | if (FeatureUtils.hasFeature(featureConfig,"Encryption")) |
| 96 | builder = new PairwiseMiddlewareInteractingInnerConnectorCompletionBuilder(models,connector,clientContainer,serverContainer,builder,"encrypt","decrypt"); |
| 97 | if (FeatureUtils.hasFeature(featureConfig,"Authentication")) |
| 98 | builder = new ConfigurableMiddlewareCallingConnectorCompletionBuilder(models,connector,clientContainer,serverContainer,builder,"createCredentials","checkCredentials",null,null); |
| 99 | ****************************************************************/ |
| 100 | result = new MarshallingConnectorCompletionBuilder(models,connector,clientContainer,serverContainer,builder); |
| 101 | |
| 102 | return result; |
| 103 | } |
| 104 | |
| 105 | private void connectConnectorCompletionWithMiddleware(IClientServerConnectorCompletionComponentBuilder componentBuilder) { |
| 106 | // Only support point-to-point connections |
| 107 | AllocationContext clientMWContext = findClientSideMiddlewareAllocationContext(); |
| 108 | assert (clientMWContext.getAssemblyContext_AllocationContext().getEncapsulatedComponent__AssemblyContext().getProvidedRoles_InterfaceProvidingEntity().get(0) instanceof OperationProvidedRole); |
| 109 | addAssemblyConnector(componentBuilder.getClientSideMiddlewareRole(), componentBuilder.getAssemblyContext(), |
| 110 | (OperationProvidedRole)clientMWContext.getAssemblyContext_AllocationContext().getEncapsulatedComponent__AssemblyContext().getProvidedRoles_InterfaceProvidingEntity().get(0), |
| 111 | clientMWContext.getAssemblyContext_AllocationContext()); |
| 112 | AllocationContext serverMWContext = findServerSideMiddlewareAllocationContext(); |
| 113 | assert (serverMWContext.getAssemblyContext_AllocationContext().getEncapsulatedComponent__AssemblyContext().getProvidedRoles_InterfaceProvidingEntity().get(0) instanceof OperationProvidedRole); |
| 114 | addAssemblyConnector(componentBuilder.getServerSideMiddlewareRole(), componentBuilder.getAssemblyContext(), |
| 115 | (OperationProvidedRole)serverMWContext.getAssemblyContext_AllocationContext().getEncapsulatedComponent__AssemblyContext().getProvidedRoles_InterfaceProvidingEntity().get(0), |
| 116 | serverMWContext.getAssemblyContext_AllocationContext()); |
| 117 | } |
| 118 | |
| 119 | private void embeddConnectorCompletionInApplication(IClientServerConnectorCompletionComponentBuilder componentBuilder) { |
| 120 | addAssemblyConnector(connector.getRequiredRole_AssemblyConnector(), |
| 121 | connector.getRequiringAssemblyContext_AssemblyConnector(), |
| 122 | componentBuilder.getOperationProvidedRole(), |
| 123 | componentBuilder.getAssemblyContext()); |
| 124 | addAssemblyConnector(componentBuilder.getOperationRequiredRole(), |
| 125 | componentBuilder.getAssemblyContext(), |
| 126 | connector.getProvidedRole_AssemblyConnector(), |
| 127 | connector.getProvidingAssemblyContext_AssemblyConnector()); |
| 128 | } |
| 129 | |
| 130 | private void addAssemblyConnector(OperationRequiredRole from, AssemblyContext fromContext, OperationProvidedRole to, AssemblyContext toContext){ |
| 131 | AssemblyConnector acon = CompositionFactory.eINSTANCE.createAssemblyConnector(); |
| 132 | acon.setParentStructure__Connector(parent); |
| 133 | acon.setRequiredRole_AssemblyConnector(from); |
| 134 | acon.setRequiringAssemblyContext_AssemblyConnector(fromContext); |
| 135 | acon.setProvidedRole_AssemblyConnector(to); |
| 136 | acon.setProvidingAssemblyContext_AssemblyConnector(toContext); |
| 137 | } |
| 138 | |
| 139 | private AllocationContext findClientSideMiddlewareAllocationContext() { |
| 140 | //Hauck: Changed code here because of changed metamodel. Please check. |
| 141 | ResourceContainer container = null; |
| 142 | if (linkingRes != null) { |
| 143 | if ((linkingRes.getConnectedResourceContainers_LinkingResource() != null) && (linkingRes.getConnectedResourceContainers_LinkingResource().size()>0)) { |
| 144 | linkingRes.getConnectedResourceContainers_LinkingResource().get(0); |
| 145 | } |
| 146 | } |
| 147 | if (container == null) { |
| 148 | container = findContainer(connector.getRequiringAssemblyContext_AssemblyConnector()); |
| 149 | } |
| 150 | return findAllocationContext(container,models.getMiddlewareRepository().getInterfaces__Repository().get(0)); |
| 151 | } |
| 152 | |
| 153 | private AllocationContext findServerSideMiddlewareAllocationContext(){ |
| 154 | ResourceContainer container = linkingRes == null ? findContainer(connector.getRequiringAssemblyContext_AssemblyConnector()) : linkingRes.getConnectedResourceContainers_LinkingResource().get(0) ; |
| 155 | // private AllocationContext findServerSideMiddlewareAllocationContext() { |
| 156 | // //Hauck: Changed code here because of changed metamodel. Please check. |
| 157 | // ResourceContainer container = null; |
| 158 | // if (linkingRes != null) { |
| 159 | // if ((linkingRes.getConnectedResourceContainers_LinkingResource() != null) && (linkingRes.getConnectedResourceContainers_LinkingResource().size()>1)) { |
| 160 | // linkingRes.getConnectedResourceContainers_LinkingResource().get(1); |
| 161 | // } |
| 162 | // } |
| 163 | // if (container == null) { |
| 164 | // findContainer(connector.getRequiringAssemblyContext_AssemblyConnector()); |
| 165 | // } |
| 166 | return findAllocationContext(container,models.getMiddlewareRepository().getInterfaces__Repository().get(0)); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Search through all allocation contexts of the given resource container |
| 171 | * to find the one which contains the component which is the one providing |
| 172 | * the given interface. |
| 173 | * If such an allocation context cannot be found, a RuntimeException is thrown. |
| 174 | * @param resourceContainer the resource container to be searched through |
| 175 | * @param interfaceToSearch the interface to be searched for |
| 176 | * @return the allocation context which has been found |
| 177 | */ |
| 178 | private AllocationContext findAllocationContext( |
| 179 | ResourceContainer resourceContainer, Interface interfaceToSearch) { |
| 180 | for (AllocationContext context : models.getAllocation().getAllocationContexts_Allocation()) { |
| 181 | if (context.getResourceContainer_AllocationContext() == resourceContainer |
| 182 | && context.getAssemblyContext_AllocationContext() |
| 183 | .getEncapsulatedComponent__AssemblyContext() |
| 184 | .getProvidedRoles_InterfaceProvidingEntity().size() > 0) { |
| 185 | ProvidedRole providedRole = context |
| 186 | .getAssemblyContext_AllocationContext() |
| 187 | .getEncapsulatedComponent__AssemblyContext() |
| 188 | .getProvidedRoles_InterfaceProvidingEntity().get(0); |
| 189 | if (providedRole instanceof OperationProvidedRole) { |
| 190 | OperationProvidedRole role = (OperationProvidedRole) providedRole; |
| 191 | if (role.getProvidedInterface__OperationProvidedRole() == interfaceToSearch) { |
| 192 | return context; |
| 193 | } |
| 194 | } else if (providedRole instanceof InfrastructureProvidedRole) { |
| 195 | InfrastructureProvidedRole role = (InfrastructureProvidedRole) providedRole; |
| 196 | if (role.getProvidedInterface__InfrastructureProvidedRole() == interfaceToSearch) { |
| 197 | return context; |
| 198 | } |
| 199 | } else { |
| 200 | throw new RuntimeException("Unsupported type of provided role found during transformation."); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | throw new RuntimeException("Model invalid, unable to find middleware component for resource container "+resourceContainer.getEntityName()); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Retrieve the linking resource for the given assembly connector |
| 209 | * @param con The connector to retrieve the linking resource for |
| 210 | * @return The linking resource on which the given connector is deployed |
| 211 | */ |
| 212 | private LinkingResource findLinkingResource(AssemblyConnector con) { |
| 213 | ResourceContainer requiredSide=findContainer(con.getRequiringAssemblyContext_AssemblyConnector()); |
| 214 | ResourceContainer providedSide=findContainer(con.getProvidingAssemblyContext_AssemblyConnector()); |
| 215 | |
| 216 | if(requiredSide==providedSide) //same container |
| 217 | return null; |
| 218 | |
| 219 | for (LinkingResource lr : models.getAllocation().getTargetResourceEnvironment_Allocation().getLinkingResources__ResourceEnvironment()){ |
| 220 | if (lr.getConnectedResourceContainers_LinkingResource().contains(requiredSide) && |
| 221 | lr.getConnectedResourceContainers_LinkingResource().contains(providedSide)) |
| 222 | |
| 223 | // //Hauck: Changed code here because of changed metamodel. Please check. |
| 224 | // for (LinkingResource lr : models.getAllocation().getTargetResourceEnvironment_Allocation().getLinkingResources__ResourceEnvironment()){ |
| 225 | // if (lr.getConnectedResourceContainers_LinkingResource().contains(findContainer(con.getRequiringAssemblyContext_AssemblyConnector())) && |
| 226 | // lr.getConnectedResourceContainers_LinkingResource().contains(findContainer(con.getProvidingAssemblyContext_AssemblyConnector()))) |
| 227 | |
| 228 | return lr; |
| 229 | } |
| 230 | if (findContainer(con.getRequiringAssemblyContext_AssemblyConnector()) != findContainer(con.getProvidingAssemblyContext_AssemblyConnector())) |
| 231 | throw new RuntimeException("AssemblyConnector "+con.getEntityName()+" links different ResourceContainer, but there is no linking resource between the containers!"); |
| 232 | return null; |
| 233 | } |
| 234 | |
| 235 | private ResourceContainer findContainer( |
| 236 | AssemblyContext requiringAssemblyContext_AssemblyConnector) { |
| 237 | for(AllocationContext ac : this.models.getAllocation().getAllocationContexts_Allocation()) { |
| 238 | if (ac.getAssemblyContext_AllocationContext().getId().equals(requiringAssemblyContext_AssemblyConnector.getId())) |
| 239 | return ac.getResourceContainer_AllocationContext(); |
| 240 | } |
| 241 | return null; |
| 242 | } |
| 243 | } |