diff options
Diffstat (limited to 'src/main/java')
24 files changed, 1282 insertions, 0 deletions
diff --git a/src/main/java/org/onap/avcnmanager/Main.java b/src/main/java/org/onap/avcnmanager/Main.java new file mode 100644 index 0000000..c0c1af9 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/Main.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; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Main { + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } +} diff --git a/src/main/java/org/onap/avcnmanager/config/AVCNConfig.java b/src/main/java/org/onap/avcnmanager/config/AVCNConfig.java new file mode 100644 index 0000000..35273ff --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/config/AVCNConfig.java @@ -0,0 +1,89 @@ +/*- + * ============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.config; + +import org.apache.kafka.common.serialization.Serdes; +import org.apache.kafka.streams.KafkaStreams; +import org.apache.kafka.streams.StreamsBuilder; +import org.apache.kafka.streams.StreamsConfig; +import org.apache.kafka.streams.Topology; +import org.apache.kafka.streams.kstream.KStream; +import org.onap.avcnmanager.message.handlers.MessageHandler; +import org.onap.avcnmanager.message.data.ChangePack; +import org.onap.avcnmanager.message.serializers.ChangePackDeserializer; +import org.onap.avcnmanager.message.serializers.ChangePackSerializer; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +import java.util.Properties; + +@Configuration +public class AVCNConfig { + + @Bean(name = "AVCNProperties") + public Properties getKafkaStreamProperties( + @Value("${kafka.bootstrap-servers}") String bootstrapServer, + @Value("${kafka.application.id}") String applicationId + ) { + Properties properties = new Properties(); + properties.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId); + properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer); + properties.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName()); + properties.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, ChangePackSerde.class.getName()); + return properties; + } + + @Bean(name = "AVCNTopology") + public Topology getKafkaStreamTopology( + @Value("${kafka.source.topic}") String inTopic, + MessageHandler messageHandler + ) { + StreamsBuilder builder = new StreamsBuilder(); + KStream<String, ChangePack> stream = builder.stream(inTopic); + stream.foreach(messageHandler::handleMessage); + return builder.build(); + } + + + @Bean + public KafkaStreams getKafkaStream( + @Qualifier("AVCNTopology") Topology topology, + @Qualifier("AVCNProperties") Properties properties + ) throws InterruptedException { + Thread.sleep(10000); + return new KafkaStreams(topology, properties); + } + + + @Bean + public RestTemplate getRestTemplate() { + return new RestTemplate(); + } + + static public final class ChangePackSerde extends Serdes.WrapperSerde<ChangePack> { + public ChangePackSerde() { + super(new ChangePackSerializer(), new ChangePackDeserializer()); + } + } +}
\ No newline at end of file diff --git a/src/main/java/org/onap/avcnmanager/kafka/stream/AVCNKafkaStream.java b/src/main/java/org/onap/avcnmanager/kafka/stream/AVCNKafkaStream.java new file mode 100644 index 0000000..dfc7912 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/kafka/stream/AVCNKafkaStream.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.kafka.stream; + +import org.apache.kafka.streams.KafkaStreams; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + + +@Component +public class AVCNKafkaStream { + + private static final Logger LOGGER = LoggerFactory.getLogger(AVCNKafkaStream.class); + + private final KafkaStreams streams; + + @Autowired + AVCNKafkaStream(KafkaStreams streams) { + this.streams = streams; + streams.setUncaughtExceptionHandler(this::handleExceptionInStreams); + } + + @PostConstruct + void startKafkaStream() { + streams.start(); + } + + private void handleExceptionInStreams(Thread thread,Throwable throwable) { + LOGGER.warn("Exception occurred int kafka stream: " + thread); + LOGGER.debug(throwable.getMessage()); + if(!streams.state().isRunning()) { + LOGGER.error("Kafka stream stop running, state: " + streams.state()); + streams.close(); + System.exit(1); + } + } +} + + diff --git a/src/main/java/org/onap/avcnmanager/message/data/Change.java b/src/main/java/org/onap/avcnmanager/message/data/Change.java new file mode 100644 index 0000000..5c6402c --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/data/Change.java @@ -0,0 +1,68 @@ +/*- + * ============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.data; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Objects; + +public class Change { + private final String path; + private final String value; + + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) + public Change(@JsonProperty("path") String path, + @JsonProperty("value") String value) { + this.path = path; + this.value = value; + } + + public String getPath() { + return path; + } + + public String getValue() { + return value; + } + + @Override + public String toString() { + return "{" + + "path='" + path + '\'' + + ", value='" + value + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Change change = (Change) o; + return Objects.equals(getPath(), change.getPath()) && + Objects.equals(getValue(), change.getValue()); + } + + @Override + public int hashCode() { + return Objects.hash(getPath(), getValue()); + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/data/ChangePack.java b/src/main/java/org/onap/avcnmanager/message/data/ChangePack.java new file mode 100644 index 0000000..f7d4c3a --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/data/ChangePack.java @@ -0,0 +1,79 @@ +/*- + * ============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.data; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Objects; + +public class ChangePack { + + private final Change aNew; + private final Change old; + + private final String type; + + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) + public ChangePack(@JsonProperty("new") Change aNew, + @JsonProperty("old") Change old, + @JsonProperty("type") String type) { + this.aNew = aNew; + this.old = old; + this.type = type; + } + + public Change getNew() { + return aNew; + } + + public Change getOld() { + return old; + } + + public String getType() { + return type; + } + + @Override + public String toString() { + return "{" + + "new=" + aNew + + ", old=" + old + + ", type='" + type + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ChangePack that = (ChangePack) o; + return Objects.equals(aNew, that.aNew) && + Objects.equals(getOld(), that.getOld()) && + Objects.equals(getType(), that.getType()); + } + + @Override + public int hashCode() { + return Objects.hash(aNew, getOld(), getType()); + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/forwarders/Forwarder.java b/src/main/java/org/onap/avcnmanager/message/forwarders/Forwarder.java new file mode 100644 index 0000000..fdf28ee --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/forwarders/Forwarder.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.forwarders; + +import org.onap.avcnmanager.message.processing.ParsingResult; + +public interface Forwarder { + void send(ParsingResult processed); +} diff --git a/src/main/java/org/onap/avcnmanager/message/forwarders/RestForwarder.java b/src/main/java/org/onap/avcnmanager/message/forwarders/RestForwarder.java new file mode 100644 index 0000000..6f1e6a8 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/forwarders/RestForwarder.java @@ -0,0 +1,86 @@ +/*- + * ============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.forwarders; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.onap.avcnmanager.message.processing.ParsingResult; +import org.onap.avcnmanager.utils.JsonUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +@Component +public class RestForwarder implements Forwarder { + private static final Logger LOGGER = LoggerFactory.getLogger(RestForwarder.class); + private static final String PNF_PAYLOAD_TEMPLATE = "pnfsim_payload_notification.json"; + private final String pnfSimulatorEndpoint; + private final RestTemplate restTemplate; + private final String vesEndpoint; + private final HttpHeaders headers; + private final JsonNode pnfSimJson; + private final JsonUtils jsonUtils; + + public RestForwarder(@Value("${rest.client.pnfsimulator.endpoint}") String pnfSimulatorEndpoint, + @Value("${rest.client.ves.endpoint}") String vesEndpoint, + RestTemplate restTemplate, + JsonUtils jsonUtils) { + this.pnfSimulatorEndpoint = pnfSimulatorEndpoint; + this.restTemplate = restTemplate; + this.vesEndpoint = vesEndpoint; + this.jsonUtils = jsonUtils; + this.headers = new HttpHeaders(); + this.pnfSimJson = jsonUtils.jsonNodeFromResourceFile(PNF_PAYLOAD_TEMPLATE); + headers.setContentType(MediaType.APPLICATION_JSON); + } + + @Override + public void send(ParsingResult message) { + String resultAsJson = jsonUtils.convertToJsonString(message); + JsonNode node = jsonUtils.convertToJsonNode(resultAsJson); + String payload = fillSimulatorRequest(node); + LOGGER.info("Will send payload: " + payload); + sendPayload(payload); + } + + + private String fillSimulatorRequest(JsonNode node) { + ObjectNode root = pnfSimJson.deepCopy(); + ((ObjectNode)root.get("simulatorParams")).put("vesServerUrl", vesEndpoint); + root.set("variables", node); + return root.toString(); + } + + private void sendPayload(String payload) { + try { + HttpEntity<String> requestEntity = new HttpEntity<>(payload, headers); + restTemplate.postForObject(pnfSimulatorEndpoint, requestEntity, String.class); + } catch (Throwable e) { + LOGGER.error("Exception occurred: " + e.getMessage() + ", while sending: " + payload + ", the payload is skipped"); + LOGGER.debug("Exception details: ", e); + } + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/handlers/MessageHandler.java b/src/main/java/org/onap/avcnmanager/message/handlers/MessageHandler.java new file mode 100644 index 0000000..fcc0691 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/handlers/MessageHandler.java @@ -0,0 +1,28 @@ +/*- + * ============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.handlers; + +import org.onap.avcnmanager.message.data.ChangePack; + +public interface MessageHandler { + + void handleMessage(String key, ChangePack massage); +} diff --git a/src/main/java/org/onap/avcnmanager/message/handlers/NewMessageHandler.java b/src/main/java/org/onap/avcnmanager/message/handlers/NewMessageHandler.java new file mode 100644 index 0000000..9a8fb7c --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/handlers/NewMessageHandler.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.handlers; + +import java.util.Objects; +import org.onap.avcnmanager.message.forwarders.Forwarder; +import org.onap.avcnmanager.message.data.ChangePack; +import org.onap.avcnmanager.message.processing.TextProcessor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.util.stream.Stream; + +@Component +public class NewMessageHandler implements MessageHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(NewMessageHandler.class); + + private final TextProcessor processor; + private final Forwarder forwarder; + + public NewMessageHandler(TextProcessor processor, Forwarder forwarder) { + this.processor = processor; + this.forwarder = forwarder; + } + + @Override + public void handleMessage(String key, ChangePack message) { + LOGGER.info("Handling message: " + message.toString()); + stream(message) + .filter(c -> !Objects.isNull(c)) + .filter(c -> !Objects.isNull(c.getNew())) + .filter(c -> !Objects.isNull(c.getNew().getPath())) + .filter(c -> !c.getNew().getPath().isEmpty()) + .map(c -> processor.process(key, message)) + .forEach(forwarder::send); + } + + private Stream<ChangePack> stream(ChangePack changePack) { + return Stream.of(changePack); + } + +} 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()); + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/serializers/ChangePackDeserializer.java b/src/main/java/org/onap/avcnmanager/message/serializers/ChangePackDeserializer.java new file mode 100644 index 0000000..9b7feb6 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/serializers/ChangePackDeserializer.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.serializers; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.kafka.common.serialization.Deserializer; +import org.onap.avcnmanager.message.data.ChangePack; + +import java.io.IOException; +import java.util.Map; + +public class ChangePackDeserializer implements Deserializer<ChangePack> { + private ObjectMapper objectMapper; + + @Override + public void configure(Map<String, ?> configs, boolean isKey) { + objectMapper = new ObjectMapper(); + } + + @Override + public ChangePack deserialize(String s, byte[] bytes) { + try { + return objectMapper.readValue(bytes, ChangePack.class); + } catch (IOException e) { + throw new RuntimeException("Can not deserialize ChangePack from bytes: " + e.getMessage()); + } + } +} diff --git a/src/main/java/org/onap/avcnmanager/message/serializers/ChangePackSerializer.java b/src/main/java/org/onap/avcnmanager/message/serializers/ChangePackSerializer.java new file mode 100644 index 0000000..38988c4 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/message/serializers/ChangePackSerializer.java @@ -0,0 +1,45 @@ +/*- + * ============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.serializers; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.kafka.common.serialization.Serializer; +import org.onap.avcnmanager.message.data.ChangePack; + +import java.util.Map; + +public class ChangePackSerializer implements Serializer<ChangePack> { + private ObjectMapper objectMapper; + @Override public void configure(Map<String, ?> configs, boolean isKey) { + objectMapper = new ObjectMapper(); + } + @Override + public byte[] serialize(String s, ChangePack changePack) { + byte[] retVal; + try { + retVal = objectMapper.writeValueAsBytes(changePack); + } catch (JsonProcessingException e) { + throw new RuntimeException("Can not serialize ChangePack instance: " + changePack + "\nReason: " + e); + } + return retVal; + } +} diff --git a/src/main/java/org/onap/avcnmanager/utils/FileUtils.java b/src/main/java/org/onap/avcnmanager/utils/FileUtils.java new file mode 100644 index 0000000..990fdf3 --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/utils/FileUtils.java @@ -0,0 +1,39 @@ +/*- + * ============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.utils; + +import org.apache.commons.io.IOUtils; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; + +@Component +public class FileUtils { + public String readStringFromResourceFile(String pnfPayloadTemplate) { + try(InputStream inputStream = getClass().getClassLoader().getResourceAsStream(pnfPayloadTemplate)) { + return IOUtils.toString(inputStream, Charset.defaultCharset()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/org/onap/avcnmanager/utils/JsonUtils.java b/src/main/java/org/onap/avcnmanager/utils/JsonUtils.java new file mode 100644 index 0000000..7ee0dce --- /dev/null +++ b/src/main/java/org/onap/avcnmanager/utils/JsonUtils.java @@ -0,0 +1,67 @@ +/*- + * ============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.utils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.onap.avcnmanager.message.processing.ParsingResult; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +@Component +public class JsonUtils { + private final ObjectMapper objectMapper; + private final FileUtils ioUtils; + + public JsonUtils(FileUtils ioUtils) { + this.objectMapper = new ObjectMapper(); + this.ioUtils = ioUtils; + } + + public JsonNode convertToJsonNode(String json) { + try { + return objectMapper.readTree(json); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public String convertToJsonString(ParsingResult message) { + try { + return objectMapper.writeValueAsString(message); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + public ObjectNode objectNodeFromJsonNode(JsonNode jsonNode) { + return (ObjectNode) jsonNode; + } + + public JsonNode jsonNodeFromResourceFile(String fileName) { + String json = ioUtils.readStringFromResourceFile(fileName); + return convertToJsonNode(json); + } + +} |