aboutsummaryrefslogtreecommitdiffstats
path: root/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/msb/MsbRegistration.java
blob: 1278c2ee752b955b16980f7e346e8b32e733b280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright 2016 Huawei Technologies Co., Ltd.
 *
 * 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.
 */

package org.onap.vnfsdk.marketplace.msb;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.onap.vnfsdk.marketplace.common.CommonConstant;
import org.onap.vnfsdk.marketplace.common.JsonUtil;
import org.onap.vnfsdk.marketplace.rest.RestResponse;
import org.onap.vnfsdk.marketplace.rest.RestfulClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class MsbRegistration {
    private static final Logger LOGGER = LoggerFactory.getLogger(MsbRegistration.class);

    private static MsbRegistration oMsbRegistration = new MsbRegistration();

    private static final String MSB_REGISTION_FILE = "etc/microservice/marketplace_rest.json";
    private static final String MSB_REGISTION_URL = "/openoapi/microservices/v1/services?createOrUpdate=false";
    private static final String MSB_UN_REGISTION_URL = "/openoapi/microservices/v1/services/{0}/version/{1}/nodes/{2}/{3}";
    private static final String NODES = "nodes";
    private static final String IP = "ip";
    private static final String PORT = "port";
    private static final String SERVICE_NAME = "serviceName";
    private static final String VERSION = "version";

    private boolean bRegistrationStatus = false;

    private MsbRegistration () {
    }

    public static MsbRegistration getInstance()
    {
        return oMsbRegistration;
    }
    /**
     * Interface to handle MSB Registration
     * @return
     */
    public int register()
    {
        File file = new File(MSB_REGISTION_FILE);
        if(!file.exists())
        {
            LOGGER.info("Stop registering as can't find msb registion file:" + file.getAbsolutePath());
            return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FILE_NOT_EXISTS;
        }

        Map<?, ?> msbRegistionBodyMap = getMsbRegistrationData();
        if(null == msbRegistionBodyMap)
        {
            LOGGER.info("Reading data from msb registion file failed:" + file.getAbsolutePath());
            return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FILE_NOT_EXISTS;
        }

        LOGGER.info("Registering body: " + JsonUtil.toJson(msbRegistionBodyMap));

        bRegistrationStatus = sendRequest(msbRegistionBodyMap);

        return bRegistrationStatus
                ? CommonConstant.MsbRegisterCode.MSDB_REGISTER_SUCESS
                        : CommonConstant.MsbRegisterCode.MSDB_REGISTER_FAILED;
    }

    private Map<?, ?> getMsbRegistrationData()
    {
        Map<?, ?> msbRegistionBodyMap = null;
        try
        {
            ObjectMapper mapper = new ObjectMapper();
            byte[] bytes = Files.readAllBytes(Paths.get(MSB_REGISTION_FILE));
            msbRegistionBodyMap = mapper.readValue(bytes, Map.class);

            replaceLocalIp(msbRegistionBodyMap);
        }
        catch(IOException e)
        {
            LOGGER.error("Failed to get microservice bus registration body, " + e);
        }
        return msbRegistionBodyMap;
    }

    /**
     * Send MSB Registration request
     * @param msbRegistionBodyMap
     * @return
     */
    private boolean sendRequest(Map<?, ?> msbRegistionBodyMap)
    {
        LOGGER.info("Start registering to microservice bus");
        String rawData = JsonUtil.toJson(msbRegistionBodyMap);
        MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
        if(null == oMsbDetails) {
            LOGGER.info("MSB Details is NULL , Registration Failed !!!");
            return false;
        }
        RestResponse oResponse = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
                                oMsbDetails.getDefaultServer().getPort(),
                                MSB_REGISTION_URL, rawData);

        if(null == oResponse){
            LOGGER.info("Null Unregister Response for  " + MSB_REGISTION_URL);
            return false;
        }
        LOGGER.info("Response Code Received for MBS Registration:" + oResponse.getStatusCode());
        return isSuccess(oResponse.getStatusCode()) ? true : false;
    }

    public int unRegister()
    {
        if(!bRegistrationStatus){
            return CommonConstant.MsbRegisterCode.MSDB_REGISTER_SUCESS;
        }

        MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
        if(null == oMsbDetails){
            LOGGER.info("MSB Details is NULL , Registration Failed !!!");
            return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FAILED;
        }

        File file = new File(MSB_REGISTION_FILE);
        if(!file.exists()){
            LOGGER.info("Stop registering as can't find msb registion file:" + file.getAbsolutePath());
            return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FILE_NOT_EXISTS;
        }

        try {
            Map<?, ?> msbRegistionBodyMap = getMsbRegistrationData();

            String serviceName = (String)msbRegistionBodyMap.get(SERVICE_NAME);
            String version = (String)msbRegistionBodyMap.get(VERSION);

            @SuppressWarnings("unchecked")
            List<Map<String, String>> nodes = (List<Map<String, String>>)msbRegistionBodyMap.get(NODES);

            Map<String, String> node = nodes.get(0);
            String ip = node.get(IP);
            String port = node.get(PORT);

            String url = MessageFormat.format(MSB_UN_REGISTION_URL, serviceName, version, ip, port);
            LOGGER.info("Start Unregister to microservice bus, url: " + url);

            RestResponse oResponse = RestfulClient.delete(oMsbDetails.getDefaultServer().getHost(),
                                        Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),url);

            if(null == oResponse) {
                LOGGER.info("Null Unregister Response for  " + url);
                return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FAILED;
            }
            LOGGER.info("Unregister Response " + oResponse.getStatusCode());
            return isSuccess(oResponse.getStatusCode()) ?
                    CommonConstant.MsbRegisterCode.MSDB_REGISTER_SUCESS :
                        CommonConstant.MsbRegisterCode.MSDB_REGISTER_FAILED;
        } catch(NullPointerException e) {
            LOGGER.info("Readed data is Invalid from msb registion file:" + file.getAbsolutePath(), e);
            return CommonConstant.MsbRegisterCode.MSDB_REGISTER_FILE_NOT_EXISTS;
        }
    }

    @SuppressWarnings("unchecked")
    private void replaceLocalIp(Map<?, ?> msbRegistionBodyMap)
    {
        List<Map<String, String>> nodes = (List<Map<String, String>>)msbRegistionBodyMap.get(NODES);
        Map<String, String> node = nodes.get(0);
        if(StringUtils.isNotEmpty(node.get(IP))) {
            return;
        }

        try
        {
            InetAddress addr = InetAddress.getLocalHost();
            String ipAddress = addr.getHostAddress();
            node.put(IP, ipAddress);

            LOGGER.info("Local ip: " + ipAddress);
        }
        catch(UnknownHostException e)
        {
            LOGGER.error("Unable to get IP address, " + e);
        }
    }

    private boolean isSuccess(int httpCode)
    {
        return (httpCode == 200 || httpCode == 201) ? true : false;
    }
}