edafe8f045a1b424b53c3febe7653b44c195235f
[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.Function1;
23 import io.vavr.Function2;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Matcher;
27 import java.util.stream.Collectors;
28 import lombok.Getter;
29
30 @Getter
31 public class Keyword {
32
33     protected static final String LETTERS_REGEX = "([a-zA-Z]+)";
34     protected static final String NONLETTERS_REGEX = "([^a-zA-Z]+)";
35
36     protected static final Function1<String, String> OPTIONAL =
37             (regex) -> regex + "?";
38
39     private final String name;
40     private final List<String> meaningfulParts;
41
42     public static final Function2<Keyword, String, Boolean> IS_MATCHING_KEYWORD_NAME = (keyword, key) ->
43         keyword != null && keyword.getName() != null && keyword.getName().equals(key);
44
45     /**
46      * Returns list of independent parts inside the keyword. Current implementation assumes that customer can join keywords with integer values, so
47      * keyword is decomposed to parts then some parts of the keyword is skipped because of replacement process.
48      *
49      * @param matcher - Matcher to check find independent groups inside the keyword
50      * @param skipGroups Informs this method about which groups should be consider as part of the replacement process
51      * @return list of independent parts inside the keywords
52      */
53     static List<String> extractPartsFrom(Matcher matcher, List skipGroups) {
54         List<String> parts = new ArrayList<String>();
55         for (int i = 1; i <= matcher.groupCount(); i++) {
56             if (matcher.group(i) != null && !skipGroups.contains(i)) {
57                 parts.add(matcher.group(i));
58             }
59         }
60         return parts;
61     }
62
63     Keyword(String name, List<String> meaningfulParts) {
64         this.name = name;
65         this.meaningfulParts = meaningfulParts;
66     }
67
68     public String substituteKeyword(String substitution) {
69         return meaningfulParts.stream()
70             .map(part -> part.equals(name) ? substitution : part)
71             .collect(Collectors.joining());
72     }
73
74 }