diff options
Diffstat (limited to 'src/main/java/org/onap/avcnmanager/message/processing')
11 files changed, 555 insertions, 0 deletions
diff --git a/src/main/java/org/onap/avcnmanager/message/processing/NetconfTextProcessor.java b/src/main/java/org/onap/avcnmanager/message/processing/NetconfTextProcessor.java new file mode 100644 index 0000000..2900c59 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/processing/NetconfTextProcessor.java @@ -0,0 +1,113 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * 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.avcnmanager.message.processing; + +import org.onap.avcnmanager.message.data.ChangePack; +import org.onap.avcnmanager.message.processing.targets.MapTarget; +import org.onap.avcnmanager.message.processing.targets.StringBuilderTarget; +import org.onap.avcnmanager.message.processing.targets.TargetContainer; +import org.onap.avcnmanager.message.processing.tokens.ContainerToken; +import org.onap.avcnmanager.message.processing.tokens.ListToken; +import org.onap.avcnmanager.message.processing.tokens.Token; +import org.onap.avcnmanager.message.processing.tokens.ValueToken; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +@Component +public final class NetconfTextProcessor implements TextProcessor { + private static final Pattern PATTERN_PATH = Pattern.compile("/+"); + private static final String DELIMITER = " , "; + + @Override + public ParsingResult process(String key, ChangePack message) { + StringBuilderTarget dn = new StringBuilderTarget(new StringBuilder(), DELIMITER); + MapTarget attributesMap = new MapTarget(new HashMap<>()); + LinkedList<Token> dequeOfTokens = new LinkedList<>(); + String newPath = message.getNew().getPath(); + String value = message.getNew().getValue(); + if(!value.isEmpty()) { + newPath += " = " +value; + } + + List<String> strings = splitToStrings(newPath, PATTERN_PATH); + convertToTypedTokens(strings, dequeOfTokens, 0); + removeConsecutiveDuplicatesFrom(dequeOfTokens); + dumpTokensIntoRespectiveContainers(dequeOfTokens, dn, attributesMap); + + return emitResult(dn, attributesMap); + } + + private static List<String> splitToStrings(String newPath, Pattern patternPath) { + return patternPath.splitAsStream(newPath) + .filter(s -> !s.isEmpty()) + .collect(Collectors.toList()); + } + + private static void removeConsecutiveDuplicatesFrom(LinkedList<Token> dequeOfTokens) { + List<Token> toRemove = new LinkedList<>(); + for (int i = 1; i < dequeOfTokens.size(); i++) { + if(ListToken.class.equals(dequeOfTokens.get(i-1).getClass()) + && ListToken.class.equals(dequeOfTokens.get(i).getClass())) { + toRemove.add(dequeOfTokens.get(i-1)); + } + } + dequeOfTokens.removeAll(toRemove); + } + + private static void convertToTypedTokens(List<String> strings, LinkedList<Token> dequeOfTokens, int startIndex) { + if (strings.size() > startIndex) { + String str = strings.get(startIndex); + Token token = determineToken(str); + dequeOfTokens.add(token); + + convertToTypedTokens(strings,dequeOfTokens, startIndex + 1); + } + } + + private static Token determineToken(String str) { + if (str.endsWith("]")) { + return new ListToken(str); + } else if (str.contains("=")) { + return new ValueToken(str); + } else { + return new ContainerToken(str); + } + } + + private static void dumpTokensIntoRespectiveContainers(LinkedList<Token> dequeOfTokens, + TargetContainer<String> dnBuilder, + TargetContainer<String> attributesBuilder) { + dequeOfTokens.forEach(t -> { + t.dump(dnBuilder); + t.dump(attributesBuilder); + }); + } + + private static ParsingResult emitResult(StringBuilderTarget dnSb, MapTarget attributesMap) { + dnSb.trimLastDelimiter(); + return new ParsingResult(dnSb.stringValue(), attributesMap.mapValue()); + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/processing/ParsingResult.java b/src/main/java/org/onap/avcnmanager/message/processing/ParsingResult.java new file mode 100644 index 0000000..d924a35 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/processing/ParsingResult.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * 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.avcnmanager.message.processing; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Collections; +import java.util.Map; +import java.util.Objects; + +public class ParsingResult { + private final String dn; + private final Map<String,String> attributesList; + + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) + public ParsingResult(@JsonProperty("dn") String dn, @JsonProperty("attributesList") Map<String, String> attributesList) { + this.dn = dn; + this.attributesList = attributesList; + } + + public String getDn() { + return dn; + } + + public Map<String, String> getAttributesList() { + return Collections.unmodifiableMap(attributesList); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ParsingResult that = (ParsingResult) o; + return Objects.equals(getDn(), that.getDn()) && + Objects.equals(getAttributesList(), that.getAttributesList()); + } + + @Override + public int hashCode() { + return Objects.hash(getDn(), getAttributesList()); + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/processing/TextProcessor.java b/src/main/java/org/onap/avcnmanager/message/processing/TextProcessor.java new file mode 100644 index 0000000..d4ad6b9 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/processing/TextProcessor.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * 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.avcnmanager.message.processing; + +import org.onap.avcnmanager.message.data.ChangePack; + +public interface TextProcessor { + ParsingResult process(String key, ChangePack message); +} diff --git a/src/main/java/org/onap/avcnmanager/message/processing/targets/MapTarget.java b/src/main/java/org/onap/avcnmanager/message/processing/targets/MapTarget.java new file mode 100644 index 0000000..c9c1ae8 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/processing/targets/MapTarget.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * 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.avcnmanager.message.processing.targets; + +import java.util.Collections; +import java.util.Map; + +public class MapTarget implements TargetContainer<String> { + private final Map<String,String> map; + + public MapTarget(Map<String, String> map) { + this.map = map; + } + + @Override + public void acceptOne(String argument) { + //no op + } + + @Override + public void acceptPair(String first, String second) { + map.put(first, second); + } + + public Map<String,String> mapValue() { + return Collections.unmodifiableMap(map); + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/processing/targets/StringBuilderTarget.java b/src/main/java/org/onap/avcnmanager/message/processing/targets/StringBuilderTarget.java new file mode 100644 index 0000000..6b04da0 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/processing/targets/StringBuilderTarget.java @@ -0,0 +1,51 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * 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.avcnmanager.message.processing.targets; + +public class StringBuilderTarget implements TargetContainer<String> { + private final StringBuilder stringBuilder; + private final String delimiter; + + public StringBuilderTarget(StringBuilder stringBuilder, String delimiter) { + this.stringBuilder = stringBuilder; + this.delimiter = delimiter; + } + + @Override + public void acceptOne(String argument) { + stringBuilder.append(argument).append(delimiter); + } + + @Override + public void acceptPair(String first, String second) { + //no op + } + + public String stringValue() { + return stringBuilder.toString(); + } + + public void trimLastDelimiter() { + if(this.stringBuilder.lastIndexOf(delimiter) == this.stringBuilder.length() - delimiter.length()) { + this.stringBuilder.setLength(this.stringBuilder.length() - delimiter.length()); + } + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/processing/targets/TargetContainer.java b/src/main/java/org/onap/avcnmanager/message/processing/targets/TargetContainer.java new file mode 100644 index 0000000..8357ac2 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/processing/targets/TargetContainer.java @@ -0,0 +1,26 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * 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.avcnmanager.message.processing.targets; + +public interface TargetContainer<T> { + void acceptOne(T argument); + void acceptPair(T first, T second); +} diff --git a/src/main/java/org/onap/avcnmanager/message/processing/tokens/BaseToken.java b/src/main/java/org/onap/avcnmanager/message/processing/tokens/BaseToken.java new file mode 100644 index 0000000..11e79bf --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/processing/tokens/BaseToken.java @@ -0,0 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * 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.avcnmanager.message.processing.tokens; + +public abstract class BaseToken implements Token { + private final String value; + BaseToken(String value) { + this.value = value; + } + + public String getValue() { + return value; + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/processing/tokens/ContainerToken.java b/src/main/java/org/onap/avcnmanager/message/processing/tokens/ContainerToken.java new file mode 100644 index 0000000..1d56889 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/processing/tokens/ContainerToken.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * 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.avcnmanager.message.processing.tokens; + +import org.onap.avcnmanager.message.processing.targets.TargetContainer; + +import java.util.AbstractMap; +import java.util.Map; + +public class ContainerToken extends BaseToken { + private static final Map.Entry<String,String> EMPTY_ENTRY = new AbstractMap.SimpleEntry<>("",""); + + public ContainerToken(String value) { + super(value); + } + + @Override + public String stringValue() { + return getValue() + "= " + getValue(); + } + + @Override + public Map.Entry<String, String> pairValue() { + return EMPTY_ENTRY; + } + + @Override + public void dump(TargetContainer<String> targetContainer) { + targetContainer.acceptOne(stringValue()); + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/processing/tokens/ListToken.java b/src/main/java/org/onap/avcnmanager/message/processing/tokens/ListToken.java new file mode 100644 index 0000000..7046de7 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/processing/tokens/ListToken.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * 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.avcnmanager.message.processing.tokens; + +import org.onap.avcnmanager.message.processing.targets.TargetContainer; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ListToken extends BaseToken { + private static final Map.Entry<String,String> EMPTY_ENTRY = new AbstractMap.SimpleEntry<>("",""); + private static final Pattern PATTERN_LIST = Pattern.compile("(.*)?\\[(.*)?='(.*)?'\\]"); + + public ListToken(String value) { + super(value); + } + + @Override + public String stringValue() { + StringBuilder sb = new StringBuilder(); + Matcher m = PATTERN_LIST.matcher(getValue()); + if (m.find()) { + String listName = m.group(1); + String value = m.group(3); + sb.append(listName).append("=").append(value); + } + return sb.toString(); + } + + @Override + public Map.Entry<String, String> pairValue() { + return EMPTY_ENTRY; + } + + @Override + public void dump(TargetContainer<String> targetContainer) { + targetContainer.acceptOne(stringValue()); + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/processing/tokens/Token.java b/src/main/java/org/onap/avcnmanager/message/processing/tokens/Token.java new file mode 100644 index 0000000..26720e5 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/processing/tokens/Token.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * 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.avcnmanager.message.processing.tokens; + +import org.onap.avcnmanager.message.processing.targets.TargetContainer; + +import java.util.Map; + +public interface Token { + String stringValue(); + Map.Entry<String,String> pairValue(); + void dump(TargetContainer<String> targetContainer); +} diff --git a/src/main/java/org/onap/avcnmanager/message/processing/tokens/ValueToken.java b/src/main/java/org/onap/avcnmanager/message/processing/tokens/ValueToken.java new file mode 100644 index 0000000..336de21 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/processing/tokens/ValueToken.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2021 Nokia. All rights reserved. + * ================================================================================ + * 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.avcnmanager.message.processing.tokens; + +import org.onap.avcnmanager.message.processing.targets.TargetContainer; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ValueToken extends BaseToken { + private static final Map.Entry<String,String> EMPTY_ENTRY = new AbstractMap.SimpleEntry<>("",""); + private static final Pattern PATTERN_VALUE = Pattern.compile("(.*)=(.*)"); + + public ValueToken(String value) { + super(value); + } + + @Override + public String stringValue() { + return ""; + } + + @Override + public Map.Entry<String, String> pairValue() { + Map.Entry<String,String> entry = EMPTY_ENTRY; + Matcher m = PATTERN_VALUE.matcher(getValue()); + if (m.find()) { + String paramName = m.group(1).trim(); + String paramValue = m.group(2).trim(); + entry = new AbstractMap.SimpleEntry<>(paramName, paramValue); + } + return entry; + } + + @Override + public void dump(TargetContainer<String> targetContainer) { + Map.Entry<String,String> entry = pairValue(); + targetContainer.acceptPair(entry.getKey(), entry.getValue()); + } +} |