Open Kilda Java Documentation
OfeMessageUtils.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.wfm;
17 
18 import static org.openkilda.messaging.Utils.MAPPER;
19 import static org.openkilda.messaging.Utils.PAYLOAD;
20 
30 
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 
33 import java.io.IOException;
34 import java.util.Collections;
35 import java.util.Map;
36 
71 public final class OfeMessageUtils {
72 
73  public static final String FIELD_SWITCH_ID = "switch_id";
74  public static final String FIELD_PORT_ID = "port_no";
75  public static final String FIELD_STATE = "state";
76 
77  public static final String SWITCH_UP = "ACTIVATED";
78  public static final String SWITCH_DOWN = "DEACTIVATED";
79  public static final String PORT_UP = "UP";
80  public static final String PORT_ADD = "ADD";
81  public static final String PORT_DOWN = "DOWN";
82  public static final String LINK_UP = "DISCOVERED";
83  public static final String LINK_DOWN = "FAILED";
84 
85  // TODO: We should use the actual message class, not build from scratch and reference the class
86  public static final String MT_SWITCH = "org.openkilda.messaging.info.event.SwitchInfoData";
87  public static final String MT_PORT = "org.openkilda.messaging.info.event.PortInfoData";
88 
89  // ============== ============== ============== ============== ==============
90  // Parsing Routines
91  // ============== ============== ============== ============== ==============
92  private static final ObjectMapper _mapper = new ObjectMapper();
93 
94  private OfeMessageUtils() {
95  throw new UnsupportedOperationException();
96  }
97 
105  public static String createSwitchInfoMessage(String switchId, String state) {
106  return createInfoMessage(true, switchId, null, state);
107  }
108 
117  public static String createPortInfoMessage(String switchId, String portId, String state) {
118  return createInfoMessage(false, switchId, portId, state);
119  }
120 
130  public static String createInfoMessage(boolean isSwitch, String switchId, String portId, String
131  state) {
132  // TODO: we don't use "type" anymore .. rewrite and leverage proper message class
133  StringBuffer sb = new StringBuffer("{'type': 'INFO', ");
134  sb.append("'timestamp': ").append(System.currentTimeMillis()).append(", ");
135  String type = (isSwitch) ? MT_SWITCH : MT_PORT;
136  sb.append("'payload': ").append(createDataMessage(type, state, switchId, portId));
137  sb.append("}");
138  return sb.toString().replace("'", "\"");
139  }
140 
141  public static String createSwitchDataMessage(String state, String switchId) {
142  return createDataMessage(MT_SWITCH, state, switchId, null);
143  }
144 
145  public static String createPortDataMessage(String state, String switchId, String portId) {
146  return createDataMessage(MT_PORT, state, switchId, portId);
147  }
148 
154  public static String createDataMessage(String type, String state, String switchId, String
155  portId) {
156  StringBuffer sb = new StringBuffer();
157  sb.append("{'clazz': '").append(type).append("', ");
158  sb.append("'switch_id': '").append(switchId).append("', ");
159  if (portId != null && portId.length() > 0) {
160  sb.append("'port_no': ").append(portId).append(", ");
161  }
162  sb.append("'state': '").append(state).append("'}");
163 
164  return sb.toString().replace("'", "\"");
165  }
166 
167  public static Map<String, ?> fromJson(String json) throws IOException {
168  return _mapper.readValue(json, Map.class);
169  }
170 
171  public static String toJson(Map<String, ?> map) throws IOException {
172  return _mapper.writeValueAsString(map);
173  }
174 
182  public static Map<String, ?> getData(String json) throws IOException {
183  Map<String, ?> root = OfeMessageUtils.fromJson(json);
184  if (root.containsKey("type")) {
185  root = (Map<String, ?>) root.get(PAYLOAD);
186  }
187  return root;
188  }
189 
199  public static String createIslFail(SwitchId switchId, int portId, String correlationId) throws IOException {
200  PathNode node = new PathNode(switchId, portId, 0, 0L);
201  InfoData data = new IslInfoData(0L, Collections.singletonList(node), 0L, IslChangeType.FAILED, 0L);
202  InfoMessage message = new InfoMessage(data, System.currentTimeMillis(), correlationId);
203  return MAPPER.writeValueAsString(message);
204  }
205 
206  // ============== ============== ============== ============== ==============
207  // ISL Discovery
208  // ============== ============== ============== ============== ==============
209 
215  public static String createIslDiscovery(SwitchId switchId, int portId, String correlationId) throws IOException {
216  CommandMessage message = new CommandMessage(
217  new DiscoverIslCommandData(switchId, Integer.valueOf(portId)), // Payload
218  System.currentTimeMillis(),
219  correlationId, Destination.CONTROLLER
220  );
221  return MAPPER.writeValueAsString(message);
222  }
223 }
root
Definition: setup.py:12
static final String PAYLOAD
Definition: Utils.java:57
static final ObjectMapper MAPPER
Definition: Utils.java:31
static String createSwitchDataMessage(String state, String switchId)
static String createPortDataMessage(String state, String switchId, String portId)
static String createDataMessage(String type, String state, String switchId, String portId)
static String createIslFail(SwitchId switchId, int portId, String correlationId)
static String createInfoMessage(boolean isSwitch, String switchId, String portId, String state)
static String toJson(Map< String, ?> map)
static String createSwitchInfoMessage(String switchId, String state)
static Map< String, ?> getData(String json)
static Map< String, ?> fromJson(String json)
static String createIslDiscovery(SwitchId switchId, int portId, String correlationId)
static String createPortInfoMessage(String switchId, String portId, String state)