23c383f3721af1acaec363b9cbfb9a7a66f8b587
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.pnfsimulator.simulator;
21
22 import static io.vavr.API.$;
23 import static io.vavr.API.Case;
24 import static io.vavr.API.Match;
25 import static org.onap.pnfsimulator.simulator.KeywordsValueProvider.getEpochSecond;
26 import static org.onap.pnfsimulator.simulator.KeywordsValueProvider.getRandomLimitedInteger;
27 import static org.onap.pnfsimulator.simulator.KeywordsValueProvider.getRandomInteger;
28 import static org.onap.pnfsimulator.simulator.KeywordsValueProvider.getRandomLimitedString;
29 import static org.onap.pnfsimulator.simulator.KeywordsValueProvider.getRandomString;
30 import static org.onap.pnfsimulator.simulator.KeywordsValueProvider.getRandomPrimitiveInteger;
31 import static org.onap.pnfsimulator.simulator.KeywordsValueProvider.getTimestampPrimitive;
32 import static org.onap.pnfsimulator.simulator.keywords.NonParameterKeywordPatterns.$nonParameterKeyword;
33 import static org.onap.pnfsimulator.simulator.keywords.SingleParameterKeywordPatterns.$singleParameterKeyword;
34 import static org.onap.pnfsimulator.simulator.keywords.TwoParameterKeywordPatterns.$twoParameterKeyword;
35 import io.vavr.API.Match.Pattern1;
36 import org.onap.pnfsimulator.simulator.keywords.Keyword;
37 import org.onap.pnfsimulator.simulator.keywords.NonParameterKeyword;
38 import org.onap.pnfsimulator.simulator.keywords.SingleParameterKeyword;
39 import org.onap.pnfsimulator.simulator.keywords.TwoParameterKeyword;
40 import org.springframework.stereotype.Component;
41
42 @Component
43 public class KeywordsExtractor {
44
45     String substituteStringKeyword(String text, int increment) {
46         return Match(text).of(
47                 Case(isRandomStringParamKeyword(),
48                         spk -> spk.substituteKeyword(getRandomString().apply(spk.getAdditionalParameter()))),
49                 Case(isRandomStringNonParamKeyword(),
50                         npk -> npk.substituteKeyword(getRandomLimitedString().apply())),
51                 Case(isRandomIntegerParamKeyword(),
52                         tpk -> tpk.substituteKeyword(getRandomInteger().apply(tpk.getAdditionalParameter1(), tpk.getAdditionalParameter2()))),
53                 Case(isRandomIntegerNonParamKeyword(),
54                         npk -> npk.substituteKeyword(getRandomLimitedInteger().apply())),
55                 Case(isIncrementKeyword(),
56                         ik -> ik.substituteKeyword(String.valueOf(increment))),
57                 Case(isTimestampNonParamKeyword(),
58                         npk -> npk.substituteKeyword(getEpochSecond().apply())),
59                 Case(
60                         $(),
61                         () -> text
62                 ));
63     }
64
65     Long substitutePrimitiveKeyword(String text) {
66         return Match(text).of(
67                 Case(isRandomPrimitiveIntegerParamKeyword(),
68                         tpk ->
69                                 getRandomPrimitiveInteger().apply(tpk.getAdditionalParameter1(), tpk.getAdditionalParameter2())),
70                 Case(isTimestampPrimitiveNonParamKeyword(),
71                         tpk ->
72                                 getTimestampPrimitive().apply()),
73                 Case(
74                         $(),
75                         () -> 0L
76                 ));
77     }
78
79     boolean isPrimitive(String text) {
80         return Match(text).of(
81                 Case(isRandomPrimitiveIntegerParamKeyword(), () -> true),
82                 Case(isTimestampPrimitiveNonParamKeyword(), () -> true),
83                 Case($(), () -> false));
84     }
85
86     private Pattern1<String, SingleParameterKeyword> isRandomStringParamKeyword() {
87         return $singleParameterKeyword($(spk -> Keyword.IS_MATCHING_KEYWORD_NAME.apply(spk, "RandomString")));
88     }
89
90     private Pattern1<String, NonParameterKeyword> isRandomStringNonParamKeyword() {
91         return $nonParameterKeyword($(npk -> Keyword.IS_MATCHING_KEYWORD_NAME.apply(npk, "RandomString")));
92     }
93
94     private Pattern1<String, NonParameterKeyword> isIncrementKeyword() {
95         return $nonParameterKeyword($(npk -> Keyword.IS_MATCHING_KEYWORD_NAME.apply(npk, "Increment")));
96     }
97
98     private Pattern1<String, TwoParameterKeyword> isRandomIntegerParamKeyword() {
99         return $twoParameterKeyword($(tpk -> Keyword.IS_MATCHING_KEYWORD_NAME.apply(tpk, "RandomInteger")));
100     }
101
102     private Pattern1<String, TwoParameterKeyword> isRandomPrimitiveIntegerParamKeyword() {
103         return $twoParameterKeyword($(tpk -> Keyword.IS_MATCHING_KEYWORD_NAME.apply(tpk, "RandomPrimitiveInteger")));
104     }
105
106     private Pattern1<String, NonParameterKeyword> isTimestampPrimitiveNonParamKeyword() {
107         return $nonParameterKeyword($(npk -> Keyword.IS_MATCHING_KEYWORD_NAME.apply(npk, "TimestampPrimitive")));
108     }
109
110     private Pattern1<String, NonParameterKeyword> isRandomIntegerNonParamKeyword() {
111         return $nonParameterKeyword($(npk -> Keyword.IS_MATCHING_KEYWORD_NAME.apply(npk, "RandomInteger")));
112     }
113
114     private Pattern1<String, NonParameterKeyword> isTimestampNonParamKeyword() {
115         return $nonParameterKeyword($(npk -> Keyword.IS_MATCHING_KEYWORD_NAME.apply(npk, "Timestamp")));
116     }
117
118 }