private MetadataEntity.Builder appendHelper(MetadataEntity.Builder builder, @Nullable String entityType, String key, String value) { if (entityType == null || entityType.isEmpty()) { // if a type is not provided then keep appending as type to update the type on every append return builder.appendAsType(key, value); } else { if (entityType.equalsIgnoreCase(key)) { // if a type was provided and this key is the type then appendAsType return builder.appendAsType(key, value); } else { return builder.append(key, value); } } }
private MetadataEntity.Builder appendHelper(MetadataEntity.Builder builder, @Nullable String entityType, String key, String value) { if (entityType == null || entityType.isEmpty()) { // if a type is not provided then keep appending as type to update the type on every append return builder.appendAsType(key, value); } else { if (entityType.equalsIgnoreCase(key)) { // if a type was provided and this key is the type then appendAsType return builder.appendAsType(key, value); } else { return builder.append(key, value); } } }
/** * If the MetadataEntity Builder key-value represent an application or artifact which is versioned in CDAP i.e. their * metadata entity representation ends with 'version' rather than 'application' or 'artifact' and the type is also * set to 'version' then for backward compatibility of rest end points return an updated builder which has the * correct type. This is only needed in 5.0 for backward compatibility of the rest endpoint. * From 5.0 and later the rest end point must be called with a query parameter which specify the type of the * metadata entity if the type is not the last key. (CDAP-13678) */ @VisibleForTesting static MetadataEntity.Builder makeBackwardCompatible(MetadataEntity.Builder builder) { MetadataEntity entity = builder.build(); List<MetadataEntity.KeyValue> entityKeyValues = StreamSupport.stream(entity.spliterator(), false) .collect(Collectors.toList()); if (entityKeyValues.size() == 3 && entity.getType().equals(MetadataEntity.VERSION) && (entityKeyValues.get(1).getKey().equals(MetadataEntity.ARTIFACT) || entityKeyValues.get(1).getKey().equals(MetadataEntity.APPLICATION))) { // this is artifact or application so update the builder MetadataEntity.Builder actualEntityBuilder = MetadataEntity.builder(); // namespace actualEntityBuilder.append(entityKeyValues.get(0).getKey(), entityKeyValues.get(0).getValue()); // application or artifact (so append as type) actualEntityBuilder.appendAsType(entityKeyValues.get(1).getKey(), entityKeyValues.get(1).getValue()); // version detail actualEntityBuilder.append(entityKeyValues.get(2).getKey(), entityKeyValues.get(2).getValue()); return actualEntityBuilder; } return builder; }
@Test public void testMakeBackwardCompatible() { MetadataEntity.Builder entity = MetadataEntity.builder().append(MetadataEntity.NAMESPACE, "ns") .append(MetadataEntity.APPLICATION, "app") .append(MetadataEntity.VERSION, "ver"); MetadataEntity expected = MetadataEntity.builder().append(MetadataEntity.NAMESPACE, "ns") .appendAsType(MetadataEntity.APPLICATION, "app") .append(MetadataEntity.VERSION, "ver").build(); Assert.assertEquals(expected, actual.build()); entity = MetadataEntity.builder().append(MetadataEntity.NAMESPACE, "ns") .append(MetadataEntity.ARTIFACT, "art") .append(MetadataEntity.VERSION, "ver"); expected = MetadataEntity.builder().append(MetadataEntity.NAMESPACE, "ns") .appendAsType(MetadataEntity.ARTIFACT, "art") .append(MetadataEntity.VERSION, "ver").build(); Assert.assertEquals(expected, actual.build()); entity = MetadataEntity.builder().append(MetadataEntity.NAMESPACE, "ns") .append(MetadataEntity.APPLICATION, "app"); actual = MetadataHttpHandler.makeBackwardCompatible(entity); expected = MetadataEntity.builder().append(MetadataEntity.NAMESPACE, "ns") .appendAsType(MetadataEntity.APPLICATION, "app").build(); Assert.assertEquals(expected, actual.build());
private static MetadataEntity fromCliString(String cliString) { String[] keyValues = cliString.split(METADATA_ENTITY_PARTS_SEPARATOR); int lastKeyValueIndex = keyValues.length - 1; MetadataEntity.Builder builder = MetadataEntity.builder(); String customType = null; if (getKeyValue(keyValues[lastKeyValueIndex])[0].equalsIgnoreCase(METADATA_ENTITY_TYPE)) { // if a type is specified then store it to call appendAsType later customType = getKeyValue(keyValues[lastKeyValueIndex])[1]; lastKeyValueIndex -= 1; } for (int i = 0; i <= lastKeyValueIndex; i++) { String[] part = getKeyValue(keyValues[i]); if (part[0].equals(customType)) { builder.appendAsType(part[0], part[1]); } else { builder.append(part[0], part[1]); } } return builder.build(); }
private static MetadataEntity fromCliString(String cliString) { String[] keyValues = cliString.split(METADATA_ENTITY_PARTS_SEPARATOR); int lastKeyValueIndex = keyValues.length - 1; MetadataEntity.Builder builder = MetadataEntity.builder(); String customType = null; if (getKeyValue(keyValues[lastKeyValueIndex])[0].equalsIgnoreCase(METADATA_ENTITY_TYPE)) { // if a type is specified then store it to call appendAsType later customType = getKeyValue(keyValues[lastKeyValueIndex])[1]; lastKeyValueIndex -= 1; } for (int i = 0; i <= lastKeyValueIndex; i++) { String[] part = getKeyValue(keyValues[i]); if (part[0].equals(customType)) { builder.appendAsType(part[0], part[1]); } else { builder.append(part[0], part[1]); } } return builder.build(); }
/** * Creates a {@link MetadataEntity} representing the given datasetName in the specified namespace. * * @param namespace the name of the namespace * @param datasetName the name of the dataset * @return {@link MetadataEntity} representing the dataset name * @throws IllegalArgumentException if the key is a CDAP entity and the MetadataEntity is not correct to represent * the CDAP entity */ public static MetadataEntity ofDataset(String namespace, String datasetName) { return builder().append(MetadataEntity.NAMESPACE, namespace) .appendAsType(MetadataEntity.DATASET, datasetName).build(); }
@Override public MetadataEntity toMetadataEntity() { return MetadataEntity.builder().append(MetadataEntity.NAMESPACE, namespace) .append(MetadataEntity.APPLICATION, application).append(MetadataEntity.VERSION, version) .append(MetadataEntity.FLOW, flow).appendAsType(MetadataEntity.FLOWLET, flowlet) .build(); }
@Override public MetadataEntity toMetadataEntity() { return MetadataEntity.builder().append(MetadataEntity.NAMESPACE, namespace) .append(MetadataEntity.APPLICATION, application) .append(MetadataEntity.VERSION, version).append(MetadataEntity.TYPE, type.getPrettyName()) .appendAsType(MetadataEntity.PROGRAM, program) .build(); }
@Override public MetadataEntity toMetadataEntity() { return MetadataEntity.builder().append(MetadataEntity.NAMESPACE, namespace) .append(MetadataEntity.APPLICATION, application) .append(MetadataEntity.VERSION, version).append(MetadataEntity.TYPE, type.getPrettyName()) .appendAsType(MetadataEntity.PROGRAM, program) .build(); }
@Override public MetadataEntity toMetadataEntity() { return MetadataEntity.builder().append(MetadataEntity.NAMESPACE, namespace) .appendAsType(MetadataEntity.STREAM, stream) .build(); }
@Override public MetadataEntity toMetadataEntity() { return MetadataEntity.builder().append(MetadataEntity.NAMESPACE, namespace) .append(MetadataEntity.APPLICATION, application) .append(MetadataEntity.VERSION, version).append(MetadataEntity.TYPE, type.getPrettyName()) .append(MetadataEntity.PROGRAM, program) .appendAsType(MetadataEntity.PROGRAM_RUN, run) .build(); }
@Override public MetadataEntity toMetadataEntity() { return MetadataEntity.builder().append(MetadataEntity.NAMESPACE, namespace) .append(MetadataEntity.APPLICATION, application) .append(MetadataEntity.VERSION, version).append(MetadataEntity.TYPE, type.getPrettyName()) .append(MetadataEntity.PROGRAM, program) .appendAsType(MetadataEntity.PROGRAM_RUN, run) .build(); }