Merge "Reorder modifiers"
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / openecomp / mso / adapters / sdnc / sdncrest / SDNCAdapterUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.openecomp.mso.adapters.sdnc.sdncrest;
22
23 import org.springframework.web.util.UriUtils;
24 import org.w3c.dom.Element;
25 import org.w3c.dom.Node;
26 import org.w3c.dom.NodeList;
27
28 import java.io.UnsupportedEncodingException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import org.openecomp.mso.logger.MsoLogger;
32
33 /**
34  * Utility methods used by SDNCAdapterRest.
35  */
36 public final class SDNCAdapterUtils {
37     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
38     /**
39      * Instantiation is not allowed.
40      */
41     private SDNCAdapterUtils() {
42     }
43     
44     /**
45          * Returns a node's child elements in a list.
46          */
47         public static List<Element> childElements(Node node) {
48                 List<Element> elements = new ArrayList<>();
49
50                 NodeList nodeList = node.getChildNodes();
51                 for (int i = 0; i < nodeList.getLength(); i++) {
52                         Node child = nodeList.item(i);
53                         if (child.getNodeType() == Node.ELEMENT_NODE) {
54                                 elements.add((Element) child);
55                         }
56                 }
57
58                 return elements;
59         }
60
61         /**
62          * Encodes a URL path segment according to RFC 3986 Section 2.
63          * @param pathSegment the path segment to encode
64          * @return the encoded path segment
65          */
66         public static String encodeURLPathSegment(String pathSegment) {
67                 try {
68                         return UriUtils.encodePathSegment(pathSegment, "UTF-8");
69                 } catch (UnsupportedEncodingException e) {
70                     LOGGER.debug("Exception:", e);
71                         throw new RuntimeException("UTF-8 encoding is not supported");
72                 }
73         }
74 }