package org.opennaas.extensions.roadm.wonesys.commandsets.commands;
/*
* #%L
* OpenNaaS :: ROADM :: W-Onesys Actionset
* %%
* Copyright (C) 2007 - 2014 FundaciĆ³ Privada i2CAT, Internet i InnovaciĆ³ a Catalunya
* %%
* 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.
* #L%
*/
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.opennaas.core.resources.command.CommandException;
import org.opennaas.core.resources.command.Response;
import org.opennaas.extensions.roadm.wonesys.commandsets.WonesysCommand;
import org.opennaas.extensions.roadm.wonesys.commandsets.WonesysResponse;
import org.opennaas.extensions.router.model.opticalSwitch.dwdm.proteus.ProteusOpticalSwitch;
import org.opennaas.extensions.router.model.opticalSwitch.dwdm.proteus.cards.ProteusOpticalSwitchCard;
import org.opennaas.extensions.router.model.utils.OpticalSwitchCardFactory;
public class GetInventoryCommand extends WonesysCommand {
Log log = LogFactory.getLog(WonesysCommand.class);
private static final String COMMAND_ID = "ff01";
private static final String DATA_LENGTH = "0000";
@Override
public void parseResponse(Object response, Object model) throws CommandException {
WonesysResponse commandResponse = (WonesysResponse) response;
if (commandResponse.getStatus().equals(Response.Status.ERROR)) {
if (commandResponse.getErrors().size() > 0)
throw new CommandException(commandResponse.getErrors().get(0));
else
throw new CommandException("Command Failed");
}
OpticalSwitchCardFactory factory;
try {
factory = new OpticalSwitchCardFactory();
} catch (IOException e) {
throw new CommandException("Failed to load received data into model. Error loading card profiles file:", e);
}
String responseData = commandResponse.getInformation();
// ID (2B) tipo (1B) subtipo (1B) = 4B per result, 2chars x Byte
int resultLength = 4 * 2;
int resultsCount = responseData.length() / resultLength;
String[] results = new String[resultsCount];
for (int i = 0; i < resultsCount; i++) {
results[i] = responseData.substring(i * resultLength, (i + 1) * resultLength);
String schasis = results[i].substring(0, 2);
String sslot = results[i].substring(2, 4);
String stype = results[i].substring(4, 6);
String ssubtype = results[i].substring(6, 8);
int chasis = Integer.parseInt(schasis, 16);
int slot = Integer.parseInt(sslot, 16);
int type = Integer.parseInt(stype, 16);
int subtype = Integer.parseInt(ssubtype, 16);
log.info("Chasis:" + chasis + " Slot:" + slot + " Type:" + type + " SubType:" + subtype);
ProteusOpticalSwitchCard card = factory.createCard(chasis, slot, type, subtype);
((ProteusOpticalSwitch) model).addLogicalDevice(card);
}
factory.addHardcodedCardConnections((ProteusOpticalSwitch) model);
}
@Override
protected String getWonesysCommandDeviceId() {
return MONITOR_DEVICE_ID;
}
@Override
protected String getWonesysCommandId() {
return COMMAND_ID;
}
@Override
protected String getWonesysCommandRequiredDataLength() {
return DATA_LENGTH;
}
@Override
protected String getWonesysCommandData() {
return "";
}
}