Operation convertOperation()

in tools/code-generation/generator/src/main/java/com/amazonaws/util/awsclientgenerator/transform/C2jModelToGeneratorModelTransformer.java [577:744]


    Operation convertOperation(C2jOperation c2jOperation) {
        Operation operation = new Operation();

        // name
        operation.setName(c2jOperation.getName());

        operation.setEndpointOperation(c2jOperation.isEndpointoperation());
        operation.setHasEndpointDiscoveryTrait(c2jOperation.getEndpointdiscovery() == null ? false :true);
        operation.setRequireEndpointDiscovery(operation.isHasEndpointDiscoveryTrait() ? c2jOperation.getEndpointdiscovery().isRequired() : false);

        if (operation.isEndpointOperation()) {
            endpointOperationName = operation.getName();
        }
        if (operation.isHasEndpointDiscoveryTrait()) {
            hasEndpointDiscoveryTrait = true;
        }
        if (operation.isRequireEndpointDiscovery()) {
            requireEndpointDiscovery = true;
        }
        // Documentation
        String crossLinkedShapeDocs =
                addDocCrossLinks(c2jOperation.getDocumentation(), c2jServiceModel.getMetadata().getUid(), c2jOperation.getName());

        operation.setDocumentation(formatDocumentation(crossLinkedShapeDocs, 9));
        operation.setAuthtype(c2jOperation.getAuthtype());
        operation.setAuthorizer(c2jOperation.getAuthorizer());
        if (c2jOperation.getEndpoint() != null) {
            operation.setEndpoint(convertEndpoint(c2jOperation.getEndpoint()));
            hasEndpointTrait = true;
            operation.setHasEndpointTrait(true);
        }

        if(operation.getAuthtype() == null) {
            if(c2jServiceModel.getMetadata().getSignatureVersion() != null &&
                    c2jServiceModel.getMetadata().getSignatureVersion().equals("bearer")) {
                operation.setSignerName("Aws::Auth::BEARER_SIGNER");
            } else {
                operation.setSignerName("Aws::Auth::SIGV4_SIGNER");
            }
        } else if (operation.getAuthtype().equals("v4-unsigned-body")) {
            operation.setSignerName("Aws::Auth::SIGV4_SIGNER");
        } else if (operation.getAuthtype().equals("bearer")) {
            operation.setSignerName("Aws::Auth::BEARER_SIGNER");
        } else if (operation.getAuthtype().equals("custom")) {
            operation.setSignerName("\"" + operation.getAuthorizer() + "\"");
        } else {
            operation.setSignerName("Aws::Auth::NULL_SIGNER");
        }
        //set operation context params
        operation.setStaticContextParams(c2jOperation.getStaticContextParams());
        // input
        if (c2jOperation.getInput() != null) {
            Shape requestShape = renameShape(shapes.get(c2jOperation.getInput().getShape()), c2jOperation.getName(), SHAPE_SDK_REQUEST_SUFFIX);

            requestShape.setRequest(true);
            requestShape.setReferenced(true);
            requestShape.getReferencedBy().add(c2jOperation.getName());
            requestShape.setLocationName(c2jOperation.getInput().getLocationName());
            if (c2jOperation.getInput().getXmlNamespace() != null) {
                XmlNamespace xmlns = new XmlNamespace();
                xmlns.setUri(c2jOperation.getInput().getXmlNamespace().getUri());
                xmlns.setPrefix(c2jOperation.getInput().getXmlNamespace().getPrefix());
                requestShape.setXmlNamespace(xmlns);
            }
            if(requestShape.getLocationName() != null && requestShape.getLocationName().length() > 0 &&
                    (requestShape.getPayload() == null || requestShape.getPayload().length() == 0) ) {
                requestShape.setPayload(requestShape.getName());
            }

            requestShape.setSignBody(true);
            if (operation.getAuthtype() != null && operation.getAuthtype().equals("v4-unsigned-body")) {
                requestShape.setSignBody(false);
            }

            if(c2jOperation.isHttpChecksumRequired()) {
                requestShape.setComputeContentMd5(true);
            }

            requestShape.getMembers().values().stream().filter(member -> member.getShape().isEventStream()).forEach(member -> member.getShape().setOutgoingEventStream(true));

            ShapeMember requestMember = new ShapeMember();
            requestMember.setShape(requestShape);
            requestMember.setDocumentation(formatDocumentation(c2jOperation.getInput().getDocumentation(), 3));

            // verify that member-refs in hostPrefix are all required members.
            operation.setRequest(requestMember);
            if (operation.getEndpoint() != null) {
                List<String> requiredMembers = operation.getEndpoint().getMemberReferences();
                for (int i = 0; i < requiredMembers.size(); i++) {
                    if (!requestShape.getMembers().get(requiredMembers.get(i)).isRequired()) {
                        throw new RuntimeException("HostPrefix member-ref should be Required. Operation: " + operation.getName() + ", member in Request: " + requiredMembers.get(i));
                    }
                    if (!requestShape.getMembers().get(requiredMembers.get(i)).isHostLabel()) {
                        throw new RuntimeException("HostPrefix member-ref should be marked with 'hostLabel'. Operation: " + operation.getName() + ", member in Request: " + requiredMembers.get(i));
                    }
                }
            }
            Map<String, Map<String, String>> operationContextParams = c2jOperation.getOperationContextParams();
            if (operationContextParams != null )
            {
                Map<String, List<String>> operationContextParamMap = new HashMap<>();
                //find first element in nested map with key "path"
                operationContextParams.entrySet().stream().filter(entry -> entry.getValue().containsKey("path"))
                .forEach(entry -> {
                    Optional<Map.Entry<String, String>> firstEntry = entry.getValue().entrySet().stream().filter(innerMap -> "path".equals(innerMap.getKey())).findFirst();
                    if (firstEntry.isPresent()) {
                        OperationContextCppCodeGenerator ctxt = new OperationContextCppCodeGenerator();
                        JmespathExpression.parse(firstEntry.get().getValue()).accept(new CppEndpointsJmesPathVisitor(ctxt, requestShape));
                        operationContextParamMap.put(entry.getKey() ,Arrays.asList(ctxt.getCppCode().toString().split("\n")) );
                    }
                });
                operation.setOperationContextParamsCode(operationContextParamMap);
            }
        }
        // output
        if (c2jOperation.getOutput() != null) {
            Shape resultShape = renameShape(shapes.get(c2jOperation.getOutput().getShape()), c2jOperation.getName(), SHAPE_SDK_RESULT_SUFFIX);
            resultShape.setResult(true);
            resultShape.setReferenced(true);
            resultShape.getReferencedBy().add(c2jOperation.getName());
            ShapeMember resultMember = new ShapeMember();
            resultMember.setShape(resultShape);
            resultMember.setDocumentation(formatDocumentation(c2jOperation.getOutput().getDocumentation(), 3));
            operation.setResult(resultMember);
        }
        // http
        operation.setHttp(convertHttp(c2jOperation.getHttp()));

        // http checksum
        if (c2jOperation.getHttpChecksum() != null) {
            C2jHttpChecksum c2jHttpChecksum = c2jOperation.getHttpChecksum();

            operation.setRequestChecksumRequired(c2jHttpChecksum.isRequestChecksumRequired());
            operation.setRequestAlgorithmMember(c2jHttpChecksum.getRequestAlgorithmMember());
            operation.setRequestValidationModeMember(c2jHttpChecksum.getRequestValidationModeMember());
            if (c2jHttpChecksum.getResponseAlgorithms() != null) {
                operation.setResponseAlgorithms(new ArrayList<>(c2jHttpChecksum.getResponseAlgorithms()));
            }
        }

        //RequestCompression
        if (c2jOperation.getRequestcompression() != null) {
            C2jRequestCompression c2jRequestCompression = c2jOperation.getRequestcompression();
            // Supporting only Gzip for now.
            if (c2jRequestCompression.getEncodings().isEmpty()) {
                throw new RuntimeException("When Request Compression is requested, at least 1 algorithm needs to be declared");
            }
            if (c2jRequestCompression.getEncodings().size() > 1 || !c2jRequestCompression.getEncodings().get(0).equals("gzip")) {
                throw new RuntimeException("Request Compression only supports usage of gzip algorithm");
            }
            operation.setRequestCompressionRequired(true); //General support for RequestCompression should be emitted
            operation.setRequestCompressionRequiredGzip(true); //GZip specific support for RequestCompression should be emitted
            Shape operationShape = operation.getRequest().getShape();
            operationShape.setRequestCompressionRequired(true);
            operationShape.setRequestCompressionRequiredGzip(true);
        }

        // errors

        List<Error> operationErrors = new ArrayList<>();
        if (c2jOperation.getErrors() != null) {
            operationErrors.addAll(c2jOperation.getErrors().stream().map(c2jError -> convertError(c2jError, c2jOperation.getName())).collect(Collectors.toList()));
        }

        operation.setErrors(operationErrors);

        return operation;
    }