6fecfa63b7077915996c9bd5d02c64366e9f1a92
[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.keywords;
21
22 import io.vavr.Tuple;
23 import io.vavr.Tuple1;
24 import io.vavr.match.annotation.Patterns;
25 import io.vavr.match.annotation.Unapply;
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.regex.Pattern;
29 import lombok.Getter;
30 import lombok.Setter;
31 import lombok.val;
32
33 @Patterns
34 @Getter
35 @Setter
36 public class TwoParameterKeyword extends Keyword {
37
38     public static final int ADDITIONAL_PARAMETER_1_GROUP = 3;
39     public static final int ADDITIONAL_PARAMETER_2_GROUP = 4;
40     public static final int KEYWORD_NAME_GROUP = 2;
41     protected static final List<Integer> ADDITIONAL_PARAMETERS_GROUPS = Arrays.asList(ADDITIONAL_PARAMETER_1_GROUP, ADDITIONAL_PARAMETER_2_GROUP);
42
43     private static final String NON_LIMITED_NUMBER_REGEX = "\\((\\d+)";
44     private static final String COLON_REGEX = "\\s?,\\s?";
45     private static final String OPTIONAL_NUMBER_PARAM_REGEX = "(\\d+)\\)";
46
47     private static final String KEYWORD_REGEX = OPTIONAL.apply(NONLETTERS_REGEX)
48         + "#"
49         + LETTERS_REGEX
50         + NON_LIMITED_NUMBER_REGEX
51         + COLON_REGEX
52         + OPTIONAL_NUMBER_PARAM_REGEX
53         + OPTIONAL.apply(NONLETTERS_REGEX);
54
55     private Integer additionalParameter1;
56     private Integer additionalParameter2;
57
58     private TwoParameterKeyword(String name, List<String> meaningfulParts, Integer additionalParameter1,
59         Integer additionalParameter2) {
60         super(name, meaningfulParts);
61         this.additionalParameter1 = additionalParameter1;
62         this.additionalParameter2 = additionalParameter2;
63     }
64
65     @Unapply
66     static Tuple1<TwoParameterKeyword> twoParameterKeyword(String keyword) {
67         val matcher = Pattern.compile(KEYWORD_REGEX).matcher(keyword);
68         TwoParameterKeyword tpk = null;
69         if (matcher.find()) {
70             tpk = new TwoParameterKeyword(
71                 matcher.group(KEYWORD_NAME_GROUP),
72                 extractPartsFrom(matcher, ADDITIONAL_PARAMETERS_GROUPS),
73                 Integer.parseInt(matcher.group(ADDITIONAL_PARAMETER_1_GROUP)),
74                 Integer.parseInt(matcher.group(ADDITIONAL_PARAMETER_2_GROUP))
75             );
76         }
77         return Tuple.of(tpk);
78     }
79
80 }