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
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Huawei. All rights reserved.
* ================================================================================
* 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.
* ============LICENSE_END=========================================================
*/
package org.onap.policy.sdnc;
import com.google.gson.JsonSyntaxException;
import java.util.HashMap;
import java.util.Map;
import org.drools.core.WorkingMemory;
import org.onap.policy.drools.system.PolicyEngine;
import org.onap.policy.rest.RESTManager;
import org.onap.policy.rest.RESTManager.Pair;
import org.onap.policy.sdnc.util.Serialization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class SdncManager implements Runnable {
private static final String SYSTEM_LS = System.lineSeparator();
private String sdncUrlBase;
private String username;
private String password;
private SdncRequest sdncRequest;
private WorkingMemory workingMem;
private static final Logger logger = LoggerFactory.getLogger(SdncManager.class);
private static final Logger netLogger =
LoggerFactory.getLogger(org.onap.policy.common.endpoints.event.comm.Topic.NETWORK_LOGGER);
// The REST manager used for processing REST calls for this Sdnc manager
private RESTManager restManager;
/**
* Constructor.
*
* @param wm Drools working memory
* @param request request
*/
public SdncManager(WorkingMemory wm, SdncRequest request) {
if (wm == null || request == null) {
throw new IllegalArgumentException(
"the parameters \"wm\" and \"request\" on the SdncManager constructor may not be null"
);
}
workingMem = wm;
sdncRequest = request;
restManager = new RESTManager();
setSdncParams(getPeManagerEnvProperty("sdnc.url"), getPeManagerEnvProperty("sdnc.username"),
getPeManagerEnvProperty("sdnc.password"));
}
/**
* Set the parameters.
*
* @param baseUrl base URL
* @param name username
* @param pwd password
*/
public void setSdncParams(String baseUrl, String name, String pwd) {
sdncUrlBase = baseUrl;
username = name;
password = pwd;
}
@Override
public void run() {
Map<String, String> headers = new HashMap<>();
Pair<Integer, String> httpDetails;
SdncResponse responseError = new SdncResponse();
SdncResponseOutput responseOutput = new SdncResponseOutput();
responseOutput.setResponseCode("404");
responseError.setResponseOutput(responseOutput);
headers.put("Accept", "application/json");
String sdncUrl = sdncUrlBase + "/GENERIC-RESOURCE-API:network-topology-operation";
try {
String sdncRequestJson = Serialization.gsonPretty.toJson(sdncRequest);
netLogger.info("[OUT|{}|{}|]{}{}", "Sdnc", sdncUrl, SYSTEM_LS, sdncRequestJson);
logger.info("[OUT|{}|{}|]{}{}", "Sdnc", sdncUrl, SYSTEM_LS, sdncRequestJson);
httpDetails = restManager.post(sdncUrl, username, password, headers, "application/json",
sdncRequestJson);
} catch (Exception e) {
logger.info(e.getMessage(), e);
workingMem.insert(responseError);
return;
}
if (httpDetails == null) {
workingMem.insert(responseError);
return;
}
try {
SdncResponse response = Serialization.gsonPretty.fromJson(httpDetails.second, SdncResponse.class);
netLogger.info("[IN|{}|{}|]{}{}", "Sdnc", sdncUrl, SYSTEM_LS, httpDetails.second);
logger.info("[IN|{}|{}|]{}{}", "Sdnc", sdncUrl, SYSTEM_LS, httpDetails.second);
String body = Serialization.gsonPretty.toJson(response);
logger.info("Response to Sdnc Heal post:");
logger.info(body);
response.setRequestId(sdncRequest.getRequestId().toString());
if (!response.getResponseOutput().getResponseCode().equals("200")) {
logger.info(
"Sdnc Heal Restcall failed with http error code {} {}", httpDetails.first, httpDetails.second
);
}
workingMem.insert(response);
} catch (JsonSyntaxException e) {
logger.info("Failed to deserialize into SdncResponse {}", e.getLocalizedMessage(), e);
} catch (Exception e) {
logger.info("Unknown error deserializing into SdncResponse {}", e.getLocalizedMessage(), e);
}
}
/**
* Protected setter for rest manager to allow mocked rest manager to be used for testing.
* @param restManager the test REST manager
*/
protected void setRestManager(final RESTManager restManager) {
this.restManager = restManager;
}
/**
* This method reads and validates environmental properties coming from the policy engine. Null properties cause
* an {@link IllegalArgumentException} runtime exception to be thrown
* @param enginePropertyName name of the parameter to retrieve
* @return the property value
*/
private String getPeManagerEnvProperty(String enginePropertyName) {
String enginePropertyValue = PolicyEngine.manager.getEnvironmentProperty(enginePropertyName);
if (enginePropertyValue == null) {
throw new IllegalArgumentException(
"The value of policy engine manager environment property \""
+ enginePropertyName + "\" may not be null"
);
}
return enginePropertyValue;
}
}
|